【问题标题】:Python error 'int' object has no attribute 'shape'Python错误'int'对象没有属性'shape'
【发布时间】:2016-07-31 01:07:00
【问题描述】:

使用下面的示例,sklearn 与 Python 3.5:

from sklearn import tree
features = [[140,1], [130,1], [150,0], [155,0]]
labels = [0,0,1,1]

clf = tree.DecisionTreeClassifier()
clf.fit(features, labels)
print(clf.predict(155,0))

我收到错误后的错误。我不明白为什么会收到此错误,有人可以解释一下吗?

Traceback (most recent call last):
  File "ml.py", line 7, in <module>
    print(clf.predict(155,0))
  File "/Users/user/Documents/hello/env/lib/python3.5/site-packages/sklearn/tree/tree.py", line 404, in predict
    X = self._validate_X_predict(X, check_input)
  File "/Users/user/Documents/hello/env/lib/python3.5/site-packages/sklearn/tree/tree.py", line 371, in _validate_X_predict
    n_features = X.shape[1]
AttributeError: 'int' object has no attribute 'shape'

【问题讨论】:

    标签: python python-3.x numpy scikit-learn


    【解决方案1】:

    如果您阅读 DecisionTreeClassifier.predict 的文档,您会发现您传递了错误的数据:

    预测(X, check_input=True)

    预测 X 的类或回归值。 对于分类模型,返回 X 中每个样本的预测类别。对于回归模型,返回基于 X 的预测值。 参数:

    X : array-like or sparse matrix of shape = [n_samples, n_features]
    

    【讨论】:

    • 对不起,是的,在再次查看后,我现在看到了。感谢您的帮助。
    猜你喜欢
    • 2019-11-18
    • 2021-11-02
    • 2014-03-30
    • 2020-05-25
    • 2018-12-02
    • 2021-07-22
    • 2022-11-26
    • 2020-06-20
    相关资源
    最近更新 更多