【问题标题】:Bad array shape in pythonpython中的错误数组形状
【发布时间】:2016-07-21 15:47:55
【问题描述】:

我正在尝试为我在网上找到的数据集实现 SVM。 features_test、features_train、labels_test、labels_train 是元组的 python 列表。我做了以下将其转换为 numpy 数组。但是 clf.fit 给了我以下错误。

File "ebola.py", line 47, in <module>
clf.fit(features_train_numpy,labels_train_numpy)
File "/usr/lib64/python2.7/site-packages/sklearn/svm/base.py", line 151, in fit
y = self._validate_targets(y)
File "/usr/lib64/python2.7/site-packages/sklearn/svm/base.py", line 514, in _validate_targets
y_ = column_or_1d(y, warn=True)
File "/usr/lib64/python2.7/site-packages/sklearn/utils/validation.py", line 551, in column_or_1d
raise ValueError("bad input shape {0}".format(shape))
ValueError: bad input shape (2923, 9)

代码如下

features_train_numpy = np.asarray(features_train)
labels_train_numpy= np.asarray(labels_train)
features_test_numpy = np.asarray(features_test)
labels_test_numpy= np.asarray(labels_test)
from sklearn.svm import SVC
temp = 100
clf=SVC(C=temp,kernel="rbf")
clf.fit(features_train_numpy,labels_train_numpy)`

【问题讨论】:

  • 每个数组的形状是什么?通过print XXX.shape?
  • 这段代码给了我下面的错误 Traceback (最近一次调用最后一次): File "ebola.py", line 58, in print np.ndarray.shape(features_train_numpy) TypeError: ' getset_descriptor' object is not callable 代码是这个 features_train_numpy = np.asarray(features_train) labels_train_numpy= np.asarray(labels_train) features_test_numpy = np.asarray(features_test) labels_test_numpy= np.asarray(labels_test) print np.ndarray.shape(features_train_numpy )
  • 对不起我的评论风格,首先我是新来的,其次,我在评论中为 cpdes 和错误提供了 4 个空格,但它没有用

标签: python-2.7 numpy machine-learning scikit-learn svm


【解决方案1】:

即使从错误本身也很容易注意到您的标签矩阵是二维的,而它应该是一维向量。它应该包含在第 i 个位置 - 第 i 个示例的标签。在您的情况下,看起来每个样本都有 9 个标签,这是 sklearn SVM 不支持的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-04
    • 1970-01-01
    • 2020-12-14
    • 1970-01-01
    • 1970-01-01
    • 2018-04-11
    • 2021-06-20
    相关资源
    最近更新 更多