【问题标题】:Scikit learn image classifierScikit 学习图像分类器
【发布时间】:2016-05-06 12:23:21
【问题描述】:

我写了这段代码:

# Import datasets, classifiers and performance metrics
from sklearn import datasets, svm, metrics
import matplotlib.image as mpimg

imgs=[[mpimg.imread('sci/img/1.jpg'),mpimg.imread('sci/img/2.jpg')],[mpimg.imread('sci/img/3.jpg'),mpimg.imread('sci/img/4.jpg')]]
targ=[1,2]

# To apply a classifier on this data, we need to flatten the image, to
# turn the data in a (samples, feature) matrix:
n_samples = len(imgs)
data = imgs.reshape((n_samples, -1))

# Create a classifier: a support vector classifier
classifier = svm.SVC(gamma=0.001)

# We learn the digits on the first half of the digits
classifier.fit(data, targ)

# Now predict the value of the digit on the second half:
expected = targ
predicted = classifier.predict(data)

print("Classification report for classifier %s:\n%s\n"
      % (classifier, metrics.classification_report(expected, predicted)))
print("Confusion matrix:\n%s" % metrics.confusion_matrix(expected, predicted))

我读到了这个错误:

AttributeError: 'list' 对象没有属性 'reshape'

我认为构建图像数组是错误的,因为它已解决?

【问题讨论】:

    标签: python scikit-learn classification


    【解决方案1】:
    data = imgs.reshape((n_samples, -1))
    

    在这里,您尝试在 Python 列表上应用方法 reshape

    但是,imgs 应该是 numpy array。因此,您应该替换

    imgs = [[mpimg.imread('sci/img/1.jpg'), mpimg.imread('sci/img/2.jpg')],[mpimg.imread('sci/img/3.jpg'), mpimg.imread('sci/img/4.jpg')]]
    

    import numpy as np 
    imgs = np.array([[mpimg.imread('sci/img/1.jpg'), mpimg.imread('sci/img/2.jpg')], [mpimg.imread('sci/img/3.jpg'), mpimg.imread('sci/img/4.jpg')]])
    

    【讨论】:

      猜你喜欢
      • 2018-08-24
      • 2023-04-05
      • 1970-01-01
      • 2016-12-24
      • 1970-01-01
      • 2014-08-19
      • 2021-10-06
      • 2014-03-15
      • 2015-06-03
      相关资源
      最近更新 更多