代码之家  ›  专栏  ›  技术社区  ›  Prometheus

Python错误“int”对象没有属性“shape”

  •  3
  • Prometheus  · 技术社区  · 10 年前

    使用下面的示例, 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'
    
    1 回复  |  直到 10 年前
        1
  •  3
  •   Padraic Cunningham    10 年前

    如果你阅读文档 DecisionTreeClassifier.predict ,您可以看到您传递了错误的数据:

    预测(X,check_input=真)

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

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