From cf5048aaa4e5f30a2edd83fe96c8fca33e2cadb5 Mon Sep 17 00:00:00 2001 From: play4fun Date: Tue, 8 Aug 2017 12:02:04 +0800 Subject: [PATCH] savez knn_data.npz --- ...345\206\231\346\225\260\345\255\227OCR.py" | 8 +++++-- ...3\345\206\231\346\225\260\345\255\2271.py" | 24 +++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 "ch46-\346\234\272\345\231\250\345\255\246\344\271\240-K\350\277\221\351\202\273/\351\242\204\346\265\213\346\211\213\345\206\231\346\225\260\345\255\2271.py" diff --git "a/ch46-\346\234\272\345\231\250\345\255\246\344\271\240-K\350\277\221\351\202\273/2-\344\275\277\347\224\250kNN\345\257\271\346\211\213\345\206\231\346\225\260\345\255\227OCR.py" "b/ch46-\346\234\272\345\231\250\345\255\246\344\271\240-K\350\277\221\351\202\273/2-\344\275\277\347\224\250kNN\345\257\271\346\211\213\345\206\231\346\225\260\345\255\227OCR.py" index 0afcec1e..8bc20258 100644 --- "a/ch46-\346\234\272\345\231\250\345\255\246\344\271\240-K\350\277\221\351\202\273/2-\344\275\277\347\224\250kNN\345\257\271\346\211\213\345\206\231\346\225\260\345\255\227OCR.py" +++ "b/ch46-\346\234\272\345\231\250\345\255\246\344\271\240-K\350\277\221\351\202\273/2-\344\275\277\347\224\250kNN\345\257\271\346\211\213\345\206\231\346\225\260\345\255\227OCR.py" @@ -43,14 +43,18 @@ accuracy = correct * 100.0 / result.size print('准确率', accuracy) # 准确率91% -# +'''''' # save the data -np.savez('knn_data.npz', train=train, train_labels=train_labels) +np.savez('knn_data.npz', train=train, train_labels=train_labels,test=test,test_labels=test_labels) # Now load the data with np.load('knn_data.npz') as data: print(data.files) train = data['train'] train_labels = data['train_labels'] + test = data['test'] + test_labels = data['test_labels'] #TODO 怎样预测数字? +# knn.predict? +# Docstring: predict(samples[, results[, flags]]) -> retval, results diff --git "a/ch46-\346\234\272\345\231\250\345\255\246\344\271\240-K\350\277\221\351\202\273/\351\242\204\346\265\213\346\211\213\345\206\231\346\225\260\345\255\2271.py" "b/ch46-\346\234\272\345\231\250\345\255\246\344\271\240-K\350\277\221\351\202\273/\351\242\204\346\265\213\346\211\213\345\206\231\346\225\260\345\255\2271.py" new file mode 100644 index 00000000..d49a7adb --- /dev/null +++ "b/ch46-\346\234\272\345\231\250\345\255\246\344\271\240-K\350\277\221\351\202\273/\351\242\204\346\265\213\346\211\213\345\206\231\346\225\260\345\255\2271.py" @@ -0,0 +1,24 @@ +# -*- coding: utf-8 -*- +# @Time : 2017/8/8 11:57 +# @Author : play4fun +# @File : 预测手写数字1.py +# @Software: PyCharm + +""" +预测手写数字1.py: +""" + +import numpy as np +import cv2 +from matplotlib import pyplot as plt + +with np.load('knn_data.npz') as data: + print(data.files) + train = data['train'] + train_labels = data['train_labels'] + test = data['test'] + test_labels = data['test_labels'] + +knn = cv2.ml.KNearest_create() +knn.train(train, cv2.ml.ROW_SAMPLE, train_labels) +ret, result, neighbours, dist = knn.findNearest(test, k=5) \ No newline at end of file