使用sklearn数字数据集预测数字 - 错误的预测

时间:2018-02-08 10:44:22

标签: python pandas scikit-learn image-recognition

我想构建一个简单的数字预测模型。

因此我:

  1. 加载sklearn数据集
  2. 将数字大小从8 * 8扩展为32 * 32
  3. 使用sklearn数字
  4. 教授SVM
  5. 预测新图片。
  6. - >对于大多数测试图像,模型返回8或1。我在代码中有误吗?

    图片如下:

    enter image description here

    我使用的代码是:

    def predictimage(file):
    
    import matplotlib.pyplot as plt
    from skimage import transform
    from PIL import Image
    import pandas as pd
    import numpy as np
    from sklearn import svm
    from sklearn import datasets
    import PIL.ImageOps
    
    
    #Load in the query instance
    
    
    img= Image.open(file)
    img=img.convert("L")
    img=PIL.ImageOps.invert(img)
    img=img.resize((32,32),Image.ANTIALIAS)
    imgplot=plt.imshow(img)
    
    
    query=np.array(img).flatten()
    query=(query/16).round()
    
    
    
    #Plot query digit
    plt.imshow(query.reshape((32,32)))
    

    enter image description here

    #Load in the training dataset
    
    digits=datasets.load_digits()
    features=digits.data
    targets=digits.target
    
    
    
    
    
    #Expand 8*8 image to a 32*32 image (64 to 1024)
    newfeatures=[transform.resize(features[i].reshape(8,8),(32,32))for i in range(len(features))]
    newfeatures=np.array(newfeatures).reshape((1797,1024)).round()
    
    #Plot expanded image with 32*32 pixels
    for l in range(9):
        ax[1+l].imshow(newfeatures[100+l].reshape((32,32)).round())
    
    
    
    #Instantiate, Train and predict    
    clf=svm.SVC(gamma=0.001,C=100)
    clf.fit(newfeatures,targets)
    
    prediction=clf.predict(query)
    
    plt.show()
    return prediction
    
    
    
    predictimage(r"C:\...\digit.jpg")
    
      

    阵列([8])

1 个答案:

答案 0 :(得分:0)

您需要缩进代码:

from matplotlib import pyplot as plt 
from skimage import transform
from PIL import Image
import pandas as pd
import numpy as np
from sklearn import svm
from sklearn import datasets
import PIL.ImageOps

def predictimage(file):

    #Load in the query instance
    img = Image.open(file)
    img =img.convert("L")
    img =PIL.ImageOps.invert(img)
    img =img.resize((32,32),Image.ANTIALIAS)
    imgplot =plt.imshow(img)


    query=np.array(img).flatten()
    query=(query/16).round()



    #Plot query digit
    plt.imshow(query.reshape((32,32)))

    #Load in the training dataset

    digits=datasets.load_digits()
    features=digits.data
    targets=digits.target





    #Expand 8*8 image to a 32*32 image (64 to 1024)
    newfeatures=[transform.resize(features[i].reshape(8,8),(32,32))for i in range(len(features))]
    newfeatures=np.array(newfeatures).reshape((1797,1024)).round()

    #Plot expanded image with 32*32 pixels
    for l in range(9):
        ax[1+l].imshow(newfeatures[100+l].reshape((32,32)).round())



    #Instantiate, Train and predict    
    clf=svm.SVC(gamma=0.001,C=100)
    clf.fit(newfeatures,targets)

    prediction=clf.predict(query)

    plt.show()
    return prediction