Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit cf5048a

Browse files
committed
savez knn_data.npz
1 parent 2797421 commit cf5048a

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

‎ch46-机器学习-K近邻/2-使用kNN对手写数字OCR.py‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,18 @@
4343
accuracy = correct * 100.0 / result.size
4444
print('准确率', accuracy) # 准确率91%
4545

46-
#
46+
''''''
4747
# save the data
48-
np.savez('knn_data.npz', train=train, train_labels=train_labels)
48+
np.savez('knn_data.npz', train=train, train_labels=train_labels,test=test,test_labels=test_labels)
4949
# Now load the data
5050
with np.load('knn_data.npz') as data:
5151
print(data.files)
5252
train = data['train']
5353
train_labels = data['train_labels']
54+
test = data['test']
55+
test_labels = data['test_labels']
5456

5557

5658
#TODO 怎样预测数字?
59+
# knn.predict?
60+
# Docstring: predict(samples[, results[, flags]]) -> retval, results
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# -*- coding: utf-8 -*-
2+
# @Time : 2017年8月8日 11:57
3+
# @Author : play4fun
4+
# @File : 预测手写数字1.py
5+
# @Software: PyCharm
6+
7+
"""
8+
预测手写数字1.py:
9+
"""
10+
11+
import numpy as np
12+
import cv2
13+
from matplotlib import pyplot as plt
14+
15+
with np.load('knn_data.npz') as data:
16+
print(data.files)
17+
train = data['train']
18+
train_labels = data['train_labels']
19+
test = data['test']
20+
test_labels = data['test_labels']
21+
22+
knn = cv2.ml.KNearest_create()
23+
knn.train(train, cv2.ml.ROW_SAMPLE, train_labels)
24+
ret, result, neighbours, dist = knn.findNearest(test, k=5)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /