标签:pclt dac fftw shuff aof main opd app nbsp
import numpy as np
# bmp 图片后缀
import matplotlib.pyplot as plt
%matplotlib inline
from sklearn.neighbors import KNeighborsClassifier
img_arr = plt.imread(‘./data/3/3_100.bmp‘)
plt.imshow(img_arr)
#./data/3/3_100.bmp
feature = []
target = []
for i in range(0,10):
for j in range(1,501):
img_path = ‘./data/‘+str(i)+‘/‘+str(i)+‘_‘+str(j)+‘.bmp‘
img_arr = plt.imread(img_path)
feature.append(img_arr)
target.append(i)
len(feature)
feature = np.array(feature)
feature.shape
# 特征数据必须保证是二维的
# feature是一个三维数组(执行将维操作)
feature = feature.reshape(5000,28*28)
feature.shape
target = np.array(target)
np.random.seed(3)
np.random.shuffle(feature)
np.random.seed(3)
np.random.shuffle(target)
x_train = feature[:4950]
y_train = target[:4950]
x_test = feature[-50:]
y_test = target[-50:]
knn = KNeighborsClassifier(n_neighbors=30)
knn.fit(x_train,y_train)
knn.score(x_train,y_train)
print(‘预测分类:‘,knn.predict(x_test))
print(‘真实数据:‘,y_test)
from sklearn.externals import joblib
joblib.dump(knn,‘./digist.m‘)
knn = joblib.load(‘./digist.m‘)
#外部图片的识别
img_arr = plt.imread(‘./数字.jpg‘)
plt.imshow(img_arr)
five_arr = img_arr[90:155,80:135]
plt.imshow(five_arr)
five_arr.shape
#five数组是三维的,需要进行降维,舍弃第三个表示颜色的维度
five_arr = five_arr.mean(axis=2)
five_arr.shape
import scipy.ndimage as ndimage
five = ndimage.zoom(five_arr,zoom = (28/65,28/55))
five.shape
# 转换为(1,784)形式
knn.predict(five.reshape(1,784))
标签:pclt dac fftw shuff aof main opd app nbsp
原文地址:https://www.cnblogs.com/bilx/p/11647989.html