<jupyter_start><jupyter_text>**Importing the necessary libraries**<jupyter_code>import matplotlib.pyplot as plt
import seaborn as sns

from sklearn import svm
from sklearn.model_selection import GridSearchCV

from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA as sklearnPCA

from matplotlib.pyplot import imshow

from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder

import cv2
%matplotlib inline<jupyter_output><empty_output><jupyter_text>**Loading the testing and training data**<jupyter_code>train_data=pd.read_csv("/kaggle/input/digit-recognizer/train.csv")
test_data=pd.read_csv("/kaggle/input/digit-recognizer/test.csv")
print(train_data.shape)
print(test_data.shape)
##Viewing the training data
train_data.head()
#viewing the number of unique values in the label column of train_data
train_data.label.unique()<jupyter_output><empty_output><jupyter_text>**Viewing some example images**<jupyter_code>rows = 3
cols = 3
axes=[]

fig=plt.figure()
fig.set_size_inches(8,10)
for i in range(rows*cols):
    sample=np.reshape(train_data[train_data.columns[1:]].iloc[i].values/255,(28,28))
    axes.append(fig.add_subplot(rows,cols,i+1))
    plt.title("Label : {}".format(train_data["label"].iloc[i]))
    plt.imshow(sample, 'gray')
plt.show()
plt.figure(figsize=(6,7))
ax=sns.countplot(x='label', data=train_data)

plt.title("Records per label")

for i in ax.patches:
    xp = f'{100 * i.get_height() / len(train_data.label):.1f}%\n'
    x = i.get_x() + i.get_width() / 2
    y = i.get_height()
    ax.annotate(xp, (x, y), ha='center', va='center')<jupyter_output><empty_output><jupyter_text>As we can see almost all the labels have more or less same frequency in the training data with '1' having the highest frequency and '5' having the lowest frequency<jupyter_code>feats=train_data.drop('label',axis=1)
labels=train_data['label']

print(feats.shape)
print(labels.shape)<jupyter_output><empty_output><jupyter_text>**Preprocessing the data**<jupyter_code>feats=feats/255
test_data=test_data/255
x_train, x_test, y_train, y_test=train_test_split(feats, labels, test_size=0.2,random_state=1)
print("Train Data Shape: ",x_train.shape)
print("Train Label Shape: ",y_train.shape)
print("Test Data Shape: ",x_test.shape)
print("Test Label Shape: ",y_test.shape)
bx_train=x_train
bx_test=x_test
bx_train[bx_train>0]=1
bx_test[bx_test>0]=1
#Making the model
clf = svm.SVC(random_state=42)
print(clf.get_params)<jupyter_output><empty_output><jupyter_text>**Converting the images to binary images and viewing it**<jupyter_code>for x in range(0,4):
    train_0=bx_train[y_train==x]
    data_new=[]
    for idx in train_0.index:
        val=train_0.loc[idx].values.reshape(28,28)
        data_new.append(val)
    plt.figure(figsize=(25,25))   
    for x in range(1,5):
        ax1=plt.subplot(1, 20, x)
        ax1.imshow(data_new[x],cmap='binary')
clf.fit(bx_train, y_train.values.ravel())
score=clf.score(bx_test,y_test)
print("Accuracy for binary: ",score)<jupyter_output><empty_output><jupyter_text>**Standardizing the data**<jupyter_code>#standardized data
sc = StandardScaler().fit(bx_train)
X_std_train = sc.transform(bx_train)
X_std_test = sc.transform(bx_test)

sklearn_pca = sklearnPCA().fit(X_std_train)

var_per = sklearn_pca.explained_variance_ratio_
cum_var_per = sklearn_pca.explained_variance_ratio_.cumsum()

plt.figure(figsize=(30,10))
ind = np.arange(len(var_per)) 
plt.bar(ind,var_per)
plt.xlabel('total components')
plt.ylabel('variance')
n_comp=len(cum_var_per[cum_var_per <= 0.90])
print("Keeping 90% Info with ",n_comp," components")
sklearn_pca = sklearnPCA(n_components=n_comp)
train_pca_b = sklearn_pca.fit_transform(X_std_train)
test_pca_b = sklearn_pca.transform(X_std_test)
print("Shape before PCA for Train: ",X_std_train.shape)
print("Shape after PCA for Train: ",train_pca_b.shape)
print("Shape before PCA for Test: ",X_std_test.shape)
print("Shape after PCA for Test: ",test_pca_b.shape)
clf.fit(train_pca_b, y_train.values.ravel())
score=clf.score(test_pca_b,y_test)
print("Accuracy for grayscale: ",score)<jupyter_output><empty_output><jupyter_text>**Retraining the data**<jupyter_code>feats[feats>0]=1
test_data[test_data>0]=1
#standardize data
sc = StandardScaler().fit(feats)
X_std_train = sc.transform(feats)
X_std_test = sc.transform(test_data)

#If n_components is not set then all components are stored 
sklearn_pca = sklearnPCA().fit(X_std_train)

#Percentage of variance explained by each of the selected components.
#If n_components is not set then all components are stored and the sum of the ratios is equal to 1.0.
var_per = sklearn_pca.explained_variance_ratio_
cum_var_per = sklearn_pca.explained_variance_ratio_.cumsum()

n_comp=len(cum_var_per[cum_var_per <= 0.90])
print("Keeping 90% Info with ",n_comp," components")
sklearn_pca = sklearnPCA(n_components=n_comp)
train_pca_b = sklearn_pca.fit_transform(X_std_train)
test_pca_b = sklearn_pca.transform(X_std_test)
print("Shape before PCA for Train: ",X_std_train.shape)
print("Shape after PCA for Train: ",train_pca_b.shape)
print("Shape before PCA for Test: ",X_std_test.shape)
print("Shape after PCA for Test: ",test_pca_b.shape)
clf.fit(train_pca_b, labels.values.ravel())
answer=clf.predict(test_pca_b)
submission = pd.DataFrame({'ImageId':test_data.index.values+1,'Label':answer})
submission.index=submission['ImageId'].values
submission.to_csv('fin.csv', index = False)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12992700729927006, 'fraction_numerical': 0.014781021897810219, 'mean_word_length': 7.3551829268292686, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '633402', 'license': 'MIT', 'n_tokens_mistral': 2161, 'n_tokens_neox': 2062, 'n_words': 389, 'path': 'Digit Recognizer/mnist-digit-recognizer-svm-pca.ipynb', 'repo_name': 'mohit-1501/Machine-learning-Projects', 'size': '5480'}