???????? ?????? ????
????????? ?????? ????????
?????: ?????? ???????? ???? ????????? ??????????? ??????. ?????? ?????? ???? ?????? ?? ??????? ???????? ?????? ??????? 8 ?????? ?????? ???????. ????? ????????? ?????????? ??????? ?????? ????????? ?????? ?????? ????????. ????????? ????????? ??????? ??????????????? ??? ?????????????? knn ????????? ??? ????? ????? ?????? ?????? ??????? ????????? ??????? ????????? ?????????. ????????? ?????? ???? ?????? 8 ????? ?????? ????????? ???????? ?????? ?????? ??????.
??????? ??????????:
Anaconda3
???? ?????? ???????????: 0-9 ?? ?????? ?????????? ???????? ????????? ???? ?????????? ????????? 5000 ???? ?????.
1. ????????? ??????? ?????? ????????? ????? ???????? ??????? ??????????? ????????? ?????????.
1 import matplotlib.pyplot as plt 2 import numpy as np 3 import pandas as pd
2.????? ???? ???????? ??????? ???? ???????.
???????? ?????????????? ??????? ?????? ???? ???????? ??? ?????? 28 ??????? ?????? ??? ???? ????? ??? ??????? numpy ?????? ??????? ?????? ????????????? ?????????.
1 img_arr = plt.imread('./data/3/3_33.bmp') 2 img_arr.shape 3 plt.imshow(img_arr)
1- ?????

3. ???????? ??????????.
3.1 ????? ???? ???????????? ???????????.
feature = [] # ??? ??? ???? ?????? ??????? target = [] # ??? ??? ???? ???????? ????
????? ??? ?????? ???? ???? ?????? ???????????? feature ???? ???????? ?????? ?????? ???????????? target ???? ???????? ????????.
???????? 10 ????? ???????? ?????? ????? ?????????? ??????????????? for ???????? ?????? ????? ?????? ????? ???????????? feature ????????? ?????? ?????? ?????? ???????????? target ???? ???????? ???????. ?????? ??? ??????????? ??? ???? ????????? ????.
1 feature = [] # ??? ??? ???? numpy ??????? 2 target = [] # ??? ??? ???? ???????? ???? 3 4 for i in range(10): 5 for j in range(1,501): 6 imgPath = './data/'+str(i)+'/'+str(i)+'_'+ str(j)+ '.bmp' 7 img_arr = plt.imread(imgPath) 8 feature.append(img_arr) 9 target.append(i)
??????? ???????????? ????? ??????????? ??? ???????? ?????? ???????? ????????. ??????? feature ???? ????????? ?????? 5000 ???? ???????? ??? ??????? numpy ??????? ????????? ?????? ?? target ???? ???????? ??? ?????????? ??????????? ?????? ????????? ?????????? ?????? ??? ???????. ????? ??????????? ??? ?????? ??????????? ????? ?????? ??????. ?????? ???????? ????? 0 ????? ??????? ???????? ?????? ?????? ?????????? ???????? ????? 0 ????? ???? ?????? ????? ????.
?????????? ????????????? ????????? ????????? ??????? ????? ???? ?????? ??? ??????? ?????????? ?????? ???????? ???????. ????? shape. ?????? ???????? ???????.
feature = np.array(feature) target = np.array(target) feature.shape
feature.shape ??? ???????? (5000, 28, 28) ????? ?????. ????? ???? ??? ??????? ?????? ??????? ?????? ?????????. ??????? nkk ??????????? ??????????? ??? ??????? ?????? ??????? ????? ??????? ????????? ??? ??????? ?????? ???????? ??? ??????? ?????? ????????? ????????????. ?????? feature ???? ?????????? ??? ???? ???? ???????? ??? ??????? ?????? ????????? ???? ??????? ???????? ????????????.
1 feature = feature.reshape((5000),784)
???? feature.shape ?????? ??? ???????? ??????? ?????? ???????? ?????? ??????? ???????? ???????????? ????????. ????????? ???????? (5000, 784) ??? ?????????. ????? ??? ??????? ???????? ?????? ???????? ?????. (5000 ????? ??? ??? 784 ?????? ????? ? ?? ?????? ????? ???????????? ?????? ????? ?????? (5000, 28, 28) ????? ??? ??????? ?????????? 5000 ??????? ??? ???? ???????? 28 ??????? 28 ??????? 784 ??? ???? ???????? ??????? ???? ??????? ??? ??????? ??????? ??????????? ??? ??????? ??????? ?????????? ???????????. ????? ???? ???? ????????? ??????? ?????? ?????? ??????? ??????????.)
??????? ???? ???????????? ???????? ?????? ??????????.
3.2 ???? ?????? ???????????? ??? ????? ?????? ???? ???????? ???????? ???????????? ???? ???? ??????? ?????????? ????????? ??????? ????????????.
???? ?????? ??????????? ??? ?????? ?????? ???????????? ????-???? ??? ????? ??????? ????? ??????? ??? ???????? ????????? ???? ??????. ?????? ?????? ??? ??????? ???????? ?????? ???????? ??????? ??????? ??????? ????? ????????? ????????. ??? ?????? ??????? ????? ????????? ????????? ??? ?????? ???? ?????? ???? ???? ?????? ????? ?????? ???? ????? ???????? ??????. ??? ????????? ????????? ???????? ??? ???????? ????????? ????????? ??? ????????? ???????????? ?????????? ?????? ??????????.
1 np.random.seed(10) # ????? ???????????? 2 np.random.shuffle(feature) 3 np.random.seed(10) # ????? ?????????????? ???????? ????????? 4 np.random.shuffle(target)
??? ????????? ??? ???????? ??????????? ????? ??????????? ??? ?????? ?????????????? ?????? ??????????? ?????? ???? ????? ??? ????? ?? ??????? ?????????????? ????????????. ??????? ??? ??????? ???????????? ????? ??? ????? ?????? ?????????? ??????????. ?????? ???????? ?????????? 777 - ?????? 5 ????? ??????? ?????? ?????? ?????? ????????? 777 - ????????? ??? 5 ????? ???? ????? ????.
4. ??? ???????? ???????? ??? ??? ???????? ??? ????? ???????.
??????? 5000 ??? ???? ?????????? 4980 ??????? ???????? ???????????? ??????????? ?? ?????? 20 ??????? ??????? ????????????. ?????? ??? ??? ?????????? ???????? ?????? ??? ????? ???????.
1 # ????????? ??????? ??????? ????? ??????. 2 x_train = feature[:4980] 3 y_train = target[:4980] 4 # ??????? ??????? ??????? ????? ??????. 5 x_test = feature[4980:] 6 y_test = target[4980:] 7 8 x_train.shape # ????????? ??????? ?????? ????????????? ???? 4980 ???? 9 x_test.shape # ??????? ??????? ?????? ????????????? ???? ???? 20 ???? ?????? ????.
3.3 ???????? ??????????
1 from sklearn.neighbors import KNeighborsClassifier # ??????? ??????? ?????? 2 knn = KNeighborsClassifier(n_neighbors=17) # ?????? ????????????? ?????? ???????? ???? ??????? ??????????? 17 ??? ????????. 3 knn.fit(x_train,y_train) # ???????? ??????????
????????? ????????????? ????? ???????????? ?????? ????????. ??? ???????? ????????????? ????????
KNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski',
metric_params=None, n_jobs=None, n_neighbors=17, p=2,
weights='uniform')
3.4 ????????? ???????? ????????
???? ????? ??????????? ?????? ????? ????? ?????? ?????????????? ????????? ???????? ????????? ????????? ???????? ???????.
1 print('???????? ????????:',knn.predict(x_test)) 2 print('?????? ???? :',y_test)
?????????? ?????? ???????? ????????????
????????? ????????: [6 6 4 4 0 8 5 8 8 2 4 3 3 9 4 1 1 2 9 2]
?????? ???? : [6 6 4 4 0 8 5 8 2 2 4 3 3 9 4 2 6 2 9 2]
???????? ???????? ??? ?????? ?????? ????????? ?????? ?????????????? ??? ????????? ??? ????? ????????? ????? ????????????? ?????? ????????? ????? ???? ????????? ?????????.
2-?????

4. ????????? ???????? ??????
???????? ??????????? ???????? ?????????? ?????? ??????? ?? ????????? ????? ??????????? ???? ??? ????? ??????????? ????? ???????? ???????? ?????? ??? ??????? ???????????? ????? ?????????? ?????? ???? ???? ??????????????? ??????.
1 from sklearn.externals import joblib # ???????? ???????? ??????? ?????? ?????? 2 joblib.dump(knn,'./knn_dwdar.m') # ???????? ??????? ??????
5. ???????? ?????????
???? ?????????????? ??????? ?????????? ???????? ?????? ??????? ???????. ????????? ????? ????????? ???????? ???????? ??????? ?????.
5.1 ?? ????? ?? ??????? ???????? ???????? ????????? ??????? ?????? ????????? ????????? ?????? ?????? ???????.
3- ?????

???? ??????? python ??? ????? ???????? ?????????? ?????? ??????? ??????.
1 ex_img_arr = plt.imread('./數字.jpg') 2 plt.imshow(ex_img_arr) 3 4 # ????????? ?????? ??????? ??????????. 5 ex_img_arr.shape 6 # ???????? (241, 257, 3) ?????? ?????? ????? ??? ???????? ??? ??????? ???? ?????? ?????????????? ???????.
5.2 ????????? 2 ????? ?????? ????? ??????
1 img_two_arr = ex_img_arr[0:75,130:185,:] 2 plt.imshow(img_two_arr) # ??????? ???? ???? 3 4 # ????????? ?????? ??????? ?????????. 5 img_two_arr.shape 6 # ?????????? ????????? ??????: (75, 55, 3)

5.3 ????????? ???????? ????????? ??? ??????? ?????? ????????? ??? ??????? ?????? ????????? ????????????. ?????? ???? ???????????? ?????? ??? ??????? ????? ??????????? ??????? ??????????????.
1 # ??????? ???????? ??? ???? ??? ???????????? ???????? 2 import scipy.ndimage as ndimage 3 img_two_arr = ndimage.zoom(img_two_arr,zoom = (28/75,28/55)) 4 # ??????????? ??????? ????? ????????? ???????. 5 img_two_arr.shape 6 # ????????: (28, 28) 7 8 # ?????????????? ??????? ??????? ???? ???????. 9 plt.imshow(img_two_arr) 10 # ???????? ????????? ???????? ??????? ???????????? ???????. 11 12 # ???? ??? ???? ???? ??? ???????? ???? ????????? ????? ????? ????????. 13 14 img_two_arr = img_two_arr.reshape((1,-1))
???????? ??????? ???????? 2 ????? ??????? ??????? ???????? ??????????? ?????????.
6. ??????? ??????? ??????? ??????? ???????
1 knn.predict(img_two_arr) 2 # ????????: array([2])
???????? ?????? ?????? ????????? ???? 2 ?????? ????????? ??????? ???????.
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/608.html
標籤:其他
下一篇:Markdown格式及語法小結
