我想构建一个简单的数字预测模型。
因此我:
- >对于大多数测试图像,模型返回8或1。我在代码中有误吗?
图片如下:
我使用的代码是:
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)))
#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])
答案 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