sklearn SVM fit()“ValueError:使用序列设置数组元素”

时间:2016-03-20 15:10:02

标签: python numpy multidimensional-array svm sklearn-pandas

我正在使用sklearn在我自己的图像集上应用svm。图像放在数据框中。 我传递给fit函数一个具有2D列表的numpy数组,这些2D列表代表图像,我传递给函数的第二个输入是目标列表(目标是数字)。 我总是得到这个错误“ValueError:设置一个带序列的数组元素”。

trainingImages = images.ix[images.partID <=9]
trainingTargets = images.clustNo.ix[images.partID<=9]
trainingImages.reset_index(inplace=True,drop=True)
trainingTargets.reset_index(inplace=True,drop=True)

classifier = svm.SVC(gamma=0.001)
classifier.fit(trainingImages.image.values,trainingTargets.values.tolist())

错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-43-5336fbeca868> in <module>()
      8 classifier = svm.SVC(gamma=0.001)
      9 
---> 10 classifier.fit(trainingImages.image.values,trainingTargets.values.tolist())
     11 
     12 #classifier.fit(t, list(range(0,2899)))

/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/sklearn/svm/base.py in fit(self, X, y, sample_weight)
    148         self._sparse = sparse and not callable(self.kernel)
    149 
--> 150         X = check_array(X, accept_sparse='csr', dtype=np.float64, order='C')
    151         y = self._validate_targets(y)
    152 

/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/sklearn/utils/validation.py in check_array(array, accept_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, warn_on_dtype, estimator)
    371                                       force_all_finite)
    372     else:
--> 373         array = np.array(array, dtype=dtype, order=order, copy=copy)
    374 
    375         if ensure_2d:

ValueError: setting an array element with a sequence.

3 个答案:

答案 0 :(得分:5)

我有同样的错误,它是两种可能之一:

  

1-数据和标签的长度不同。

     

2-对于特定的特征向量,元素的数量不是   相等。

答案 1 :(得分:1)

这可能是因为“trainingImages.image.values”在其所有数组中没有相同数量的元素。在stackoverflow中检查一个类似的问题:

ValueError: setting an array element with a sequence. while using SVM in scikit-learn

答案 2 :(得分:1)

如果您确定尺寸正确,请参阅以下可能有用的代码/工作流程

import skimage.io as skio
import matplotlib.pyplot as plt
import numpy as np
from sklearn import svm
from sklearn.metrics import accuracy_score
from sklearn.metrics import precision_score
%matplotlib inline

# Load the data
trainingImages = skio.imread_collection('train/images/*.jpg',conserve_memory=True)

# cast to numpy arrays
trainingImages = np.asarray(trainingImages)

# reshape img array to vector
def reshape_image(img):
    return np.reshape(img,len(img)*len(img[0]))

img_reshape = np.zeros((len(trainingImages),len(trainingImages[0])*len(trainingImages[0][0])))

for i in range(0,len(trainingImages)):
    img_reshape[i] = reshape_image(trainingImages[i])

# SVM
clf = svm.SVC(gamma=0.01,C=10,kernel='poly')
clf.fit(img_reshape,trainingTargets.values.tolist())