赞
踩
# 1.读取数据
import pandas as pd
df = pd.read_excel('./手写字体识别.xlsx')
df.head()
X = df.iloc[:,1:]
y = df.iloc[:,0]
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=27)
X_train.shape, X_test.shape
y_train.shape, y_test.shape
((1547, 1024), (387, 1024))
((1547,), (387,))
from sklearn.neighbors import KNeighborsClassifier as KNN
knn = KNN(n_neighbors=5)
knn.fit(X_train, y_train)
KNeighborsClassifier(algorithm=‘auto’, leaf_size=30, metric=‘minkowski’,
metric_params=None, n_jobs=None, n_neighbors=5, p=2,
weights=‘uniform’)
y_pred = knn.predict(X_test)
y_pred[:100]
array([5, 3, 7, 8, 9, 2, 1, 4, 5, 8, 9, 5, 9, 3, 3, 2, 3, 7, 9, 1, 0, 0,
7, 6, 6, 7, 0, 9, 6, 9, 1, 8, 6, 9, 2, 5, 2, 4, 5, 8, 3, 6, 9, 4,
9, 2, 7, 3, 4, 9, 5, 6, 7, 3, 3, 8, 3, 1, 5, 3, 6, 7, 5, 0, 3, 7,
1, 4, 9, 1, 5, 1, 2, 6, 9, 1, 9, 5, 5, 9, 2, 8, 8, 4, 4, 9, 4, 3,
9, 8, 0, 3, 4, 3, 6, 8, 5, 2, 9, 0], dtype=int64)
arr = pd.DataFrame({'实际值': list(y_test), '预测值': list(y_pred)})
arr.head(10)
from sklearn.metrics import accuracy_score
score = accuracy_score(y_pred, y_test)
score
0.979328165374677
score = knn.score(X_test, y_test)
score
0.979328165374677
from PIL import Image
img = Image.open('./数字5.jpg')
img = img.resize((32, 32))
# img.show()
img
img = img.convert('L')
# img.show()
img
import numpy as np
img_new = img.point(lambda x:0 if x>128 else 1)
arr = np.array(img_new)
for i in range(arr.shape[1]):
print(arr[i])
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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