knn 进行手写数字识别

数据集使用了Keras的datasets

from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import classification_report
from keras.datasets import mnist

(x_train_tmp, y_train_tmp), (x_test_tmp, y_test_tmp) = mnist.load_data()
x_test_new = x_test_tmp.reshape(x_test_tmp.shape[0], -1)
total_num = x_test_new.shape[0]
test_num = int(total_num / 10)
x_test = x_test_new[:test_num]
y_test = y_test_tmp[:test_num]
x_train = x_test_new[test_num:]
y_train = y_test_tmp[test_num:]

classifier = KNeighborsClassifier(n_neighbors=5)
classifier.fit(x_train, y_train)

y_pred = classifier.predict(x_test)
print(classification_report(y_test, y_pred))

打印结果如下:

              precision    recall  f1-score   support

           0       0.94      0.99      0.97        85
           1       0.91      1.00      0.95       126
           2       0.97      0.91      0.94       116
           3       0.93      0.93      0.93       107
           4       0.91      0.91      0.91       110
           5       0.95      0.92      0.94        87
           6       0.97      0.97      0.97        87
           7       0.89      0.91      0.90        99
           8       0.95      0.83      0.89        89
           9       0.86      0.87      0.87        94

    accuracy                           0.93      1000
   macro avg       0.93      0.92      0.93      1000
weighted avg       0.93      0.93      0.93      1000

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值