机器学习 August 20, 2018

2-3 测试算法准确性

Words count 2.8k Reading time 3 mins. Read count 0

import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from sklearn import datasets
digits = datasets.load_digits()
digits.keys()
dict_keys(['data', 'target', 'target_names', 'images', 'DESCR'])
X = digits.data
X.shape
(1797, 64)
y = digits.target
y.shape
(1797,)
y[:100]
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1,
       2, 3, 4, 5, 6, 7, 8, 9, 0, 9, 5, 5, 6, 5, 0, 9, 8, 9, 8, 4, 1, 7,
       7, 3, 5, 1, 0, 0, 2, 2, 7, 8, 2, 0, 1, 2, 6, 3, 3, 7, 3, 3, 4, 6,
       6, 6, 4, 9, 1, 5, 0, 9, 5, 2, 8, 2, 0, 0, 1, 7, 6, 3, 2, 1, 7, 4,
       6, 3, 1, 3, 9, 1, 7, 6, 8, 4, 3, 1])
some_digit = X[666]
y[666]
0
some_digit_image = some_digit.reshape(8,8)
plt.imshow(some_digit_image,cmap=matplotlib.cm.binary)
plt.show()

# 使用knn进行分类
from script.kNN_function.model_selection import train_test_split
X_train,x_test,y_tran,y_test = train_test_split(X,y,0.2) 
from script.kNN_function.kNN import kNNClassifier
---------------------------------------------------------------------------

ModuleNotFoundError                       Traceback (most recent call last)

<ipython-input-20-1671e0c3f70e> in <module>()
----> 1 from script.kNN_function.kNN import kNNClassifier


~/Desktop/MachineLearn/script/kNN_function/kNN.py in <module>()
      3 from collections import Counter
      4 
----> 5 from kNN_function.metrics import accuracy_score
      6 
      7 


ModuleNotFoundError: No module named 'kNN_function'
my_knn_clf = kNNClassifier(3)
my_knn_clf.fit(X_train,y_tran)
y_predict = my_knn_clf.predict(x_test)
y_predict
sum(y_predict == y_test)/len(y_test)
from script.metrics import accuracy_score
accuracy_score(y_predict,y_test)
0%