使用 sklearn KNeighborsClassifier 的手写数字协调不会重新协调图像

Handwritten Digit Recongition using sklearn KNeighborsClassifier does not recongnize the image

提问人:엄윤상 提问时间:10/30/2023 最后编辑:petezurich엄윤상 更新时间:10/30/2023 访问量:21

问:

from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_test_split
from sklearn.datasets import fetch_openml
from sklearn.metrics import accuracy_score
import numpy as np
import pandas as pd
from PIL import Image
import numpy as np

def KNN(test_data):
    mnist = fetch_openml('mnist_784',parser="auto")

    X_train, X_test, Y_train, Y_test = train_test_split(mnist.data, mnist.target, test_size=0.2, random_state=1)

    #r(neighbor count)
    k=5
    knn_model = KNeighborsClassifier(n_neighbors=k)
    knn_model.fit(X_train.values,Y_train)

    result =  knn_model.predict(test_data)[0]
    print(result)# print result
    # y_pred = knn_model.predict(X_test.values)


image = Image.open("./zero.png")

# image.show()

bw_image = image.convert("L")#convert image to grayscale

resized_image = bw_image.resize((28,28))#resize image 28x28

image_array = np.array(resized_image)#convert image to nparray (ndim=2)

test_image = image_array.reshape(784)#change image_array to 1 dim array
test_image = test_image.astype('float32')/255#change value range to 0~1

#for i in range(0,784,1):
#    test_image[i] = 1 -(test_image[i]/255.0)

test_image = test_image.reshape(1,-1)#reshape array
print(test_image)

resized_image.show()
print(test_image.shape)
KNN(test_image)

该程序使用PIL转换与MNIST格式相同的格式 (28*28,灰度)

比使用 MNIST 数据集进行训练

enter image description here结果:1

打印结果:

[[1.         1.         1.         1.         1.         1.
  1.         1.         1.         1.         1.         1.
  1.         1.         1.         1.         1.         1.
  1.         1.         1.         1.         1.         1.
  1.         1.         1.         1.         1.         1.
  1.         1.         1.         1.         1.         1.
  1.         1.         1.         1.         1.         1.
  1.         1.         1.         1.         1.         1.
  1.         1.         1.         1.         1.         1.
  1.         1.         1.         1.         1.         1.
  1.         1.         1.         1.         1.         1.
  1.         1.         1.         1.         1.         1.
  1.         1.         1.         1.         1.         1.
  1.         1.         1.         1.         1.         1.
  1.         1.         1.         1.         1.         1.
  1.         1.         1.         1.         0.7176471  0.1254902
  0.         0.         0.         0.         0.         0.1254902
  0.5921569  1.         1.         1.         1.         1.
  1.         1.         1.         1.         1.         1.
  1.         1.         1.         1.         1.         1.
  1.         1.         0.1254902  0.         0.         0.
  0.         0.         0.         0.         0.         0.1254902
  0.62352943 1.         1.         1.         1.         1.
  1.         1.         1.         1.         1.         1.
  1.         1.         1.         1.         1.         0.7176471
  0.0627451  0.         0.         0.         0.         0.
  0.         0.         0.         0.         0.03137255 0.62352943
  1.         1.         1.         1.         1.         1.
  1.         1.         1.         1.         1.         1.
  1.         1.         0.62352943 0.0627451  0.         0.
  0.         0.         0.         0.         0.3764706  0.3764706
  0.         0.         0.         0.03137255 1.         1.
  1.         1.         1.         1.         1.         1.
  1.         1.         1.         1.         1.         0.49803922
  0.         0.         0.         0.         0.34509805 0.96862745
  1.         1.         1.         1.         0.5294118  0.
  0.         0.         0.5294118  1.         1.         1.
  1.         1.         1.         1.         1.         1.
  1.         1.         0.49803922 0.         0.         0.
  0.         0.40784314 1.         1.         1.         1.
  1.         1.         1.         0.4392157  0.         0.
  0.03137255 1.         1.         1.         1.         1.
  1.         1.         1.         1.         1.         0.49803922
  0.         0.         0.         0.28235295 0.96862745 1.
  1.         1.         1.         1.         1.         1.
  1.         0.96862745 0.         0.         0.         1.
  1.         1.         1.         1.         1.         1.
  1.         1.         0.5921569  0.         0.         0.
  0.28235295 0.96862745 1.         1.         1.         1.
  1.         1.         1.         1.         1.         1.
  0.         0.         0.         1.         1.         1.
  1.         1.         1.         1.         1.         1.
  0.09411765 0.         0.         0.28235295 0.96862745 1.
  1.         1.         1.         1.         1.         1.
  1.         1.         1.         1.         0.         0.
  0.         1.         1.         1.         1.         1.
  1.         1.         1.         1.         0.         0.
  0.         0.96862745 1.         1.         1.         1.
  1.         1.         1.         1.         1.         1.
  1.         1.         0.03137255 0.         0.         0.5294118
  1.         1.         1.         1.         1.         1.
  1.         0.5921569  0.         0.         0.         1.
  1.         1.         1.         1.         1.         1.
  1.         1.         1.         1.         1.         1.
  0.4392157  0.         0.         0.03137255 1.         1.
  1.         1.         1.         1.         1.         0.09411765
  0.         0.         0.34509805 1.         1.         1.
  1.         1.         1.         1.         1.         1.
  1.         1.         1.         1.         0.96862745 0.
  0.         0.         1.         1.         1.         1.
  1.         1.         1.         0.         0.         0.
  0.96862745 1.         1.         1.         1.         1.
  1.         1.         1.         1.         1.         1.
  1.         1.         0.5921569  0.         0.         0.
  1.         1.         1.         1.         1.         1.
  1.         0.         0.         0.         1.         1.
  1.         1.         1.         1.         1.         1.
  1.         1.         1.         1.         1.         1.
  0.09411765 0.         0.         0.34509805 1.         1.
  1.         1.         1.         1.         1.         0.
  0.         0.         0.62352943 1.         1.         1.
  1.         1.         1.         1.         1.         1.
  1.         1.         1.         1.         0.         0.
  0.         0.96862745 1.         1.         1.         1.
  1.         1.         1.         0.03137255 0.         0.
  0.03137255 1.         1.         1.         1.         1.
  1.         1.         1.         1.         1.         1.
  1.         1.         0.         0.         0.         1.
  1.         1.         1.         1.         1.         1.
  1.         0.5294118  0.         0.         0.         0.62352943
  1.         1.         1.         1.         1.         1.
  1.         1.         1.         1.         1.         0.5921569
  0.         0.         0.         1.         1.         1.
  1.         1.         1.         1.         1.         1.
  0.03137255 0.         0.         0.03137255 1.         1.
  1.         1.         1.         1.         1.         1.
  1.         1.         0.7176471  0.0627451  0.         0.
  0.34509805 1.         1.         1.         1.         1.
  1.         1.         1.         1.         0.5294118  0.
  0.         0.         0.49803922 1.         1.         1.
  1.         1.         1.         1.         0.7176471  0.1254902
  0.         0.         0.         0.         0.96862745 1.
  1.         1.         1.         1.         1.         1.
  1.         1.         1.         1.         1.         1.
  1.         1.         1.         1.         1.         1.
  1.         1.         1.         1.         1.         1.
  1.         1.         1.         1.        ]]
(1, 784)
1
蟒蛇 knn sklearn-pandas

评论

0赞 Community 10/31/2023
请澄清您的具体问题或提供其他详细信息以准确说明您的需求。正如目前所写的那样,很难确切地说出你在问什么。

答: 暂无答案