sklearn SVM fit()"ValueError:设置具有序列的数组元素" [英] sklearn SVM fit() "ValueError: setting an array element with a sequence"
问题描述
我正在使用sklearn将svm应用到我自己的一组图像上.图像放置在数据框中. 我将具有2D列表的numpy数组传递给fit函数,这些2D列表表示图像,我传递给函数的第二个输入是目标列表(目标是数字). 我总是收到此错误"ValueError:设置具有序列的数组元素".
I am using sklearn to apply svm on my own set of images. The images are put in a data frame. I pass to the fit function a numpy array that has 2D lists, these 2D lists represents images and the second input I pass to the function is the list of targets (The targets are numbers). I always get this error "ValueError: setting an array element with a sequence".
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.
推荐答案
我有相同的确切错误,这是两种可能性之一:
I had the same exact error, it's one of two possibilities:
1-数据和标签的长度不同.
1- Data and labels are not in the same length.
2-对于特定特征向量,元素数量不等于 相等.
2- For a specific feature vector, the number of elements are not equal.
这篇关于sklearn SVM fit()"ValueError:设置具有序列的数组元素"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!