<jupyter_start><jupyter_text>TP4 SVM for MNIST digit number recognition<jupyter_code>import numpy as np
from sklearn import decomposition
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score
from sklearn.metrics import confusion_matrix
from sklearn.model_selection import GridSearchCV
train_data=np.genfromtxt("dataset/mnist_train.csv",delimiter=",",dtype=np.uint8)
test_data=np.genfromtxt("dataset/mnist_train.csv",delimiter=",",dtype=np.uint8)
print(train_data.shape)
print(test_data.shape)
# ignore the header
x_train=train_data[1:,1:]
y_train=train_data[1:,0]
x_test=test_data[1:,1:]
y_test=test_data[1:,0]
print("{}/{}".format(x_train.shape,y_train.shape))
print("{}/{}".format(x_test.shape,y_test.shape))
pca=decomposition.PCA(n_components=100)
x_train=pca.fit_transform(x_train)
x_test=pca.fit_transform(x_test)
print(x_train.shape)
print(x_test.shape)
scaler = StandardScaler()
x_train=scaler.fit_transform(x_train)
x_test=scaler.fit_transform(x_test)
print(x_train.shape)
print(x_test.shape)
parameters = {'kernel':('linear', 'poly', 'rbf', 'sigmoid'), 'C':[1, 5, 10],'gamma':["auto","scale"]}
clf = GridSearchCV(SVC(), parameters)
clf.fit(x_train,y_train)
# best param C=10, rbf, auto
best_svc=SVC(kernel="rbf",C=10,gamma="auto")
best_svc.fit(x_train,y_train)
y_pred=best_svc.predict(x_test)
# evaluation
accuracy=accuracy_score(y_test,y_pred)
conf=confusion_matrix(y_test,y_pred)
print("accuracy score: {}".format(accuracy))
print("confusion matrix: {}".format(conf))<jupyter_output>accuracy score: 0.9983833333333333
confusion matrix: [[5920    0    0    0    1    1    0    0    1    0]
 [   0 6737    3    0    0    0    0    2    0    0]
 [   0    0 5946    3    1    1    1    3    1    2]
 [   0    0    4 6114    0    6    0    2    4    1]
 [   0    0    1    0 5837    0    0    2    0    2]
 [   0    0    0    4    1 5411    1    0    4    0]
 [   0    0    0    0    1    2 5915    0    0    0]
 [   0    1    3    0    3    1    0 6254    2    1]
 [   0    0    0    4    1    1    0    1 5844    0]
 [   2    0    1    2    4    1    0   11    3 5925]]
{'fraction_non_alphanumeric': 0.12208504801097393, 'fraction_numerical': 0.07773205304069501, 'mean_word_length': 3.223938223938224, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '348209', 'license': 'Apache-2.0', 'n_tokens_mistral': 918, 'n_tokens_neox': 867, 'n_words': 195, 'path': 'SVM/SVM.ipynb', 'repo_name': 'Pan-Mengyu/machine-learning-in-robots', 'size': '2187'}