Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python error 'int' object has no attribute 'shape'

Using the example below, sklearn with 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))

I get the error following error. I don't understand why I received this error, could someone explain?

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'
like image 335
Prometheus Avatar asked Dec 08 '25 16:12

Prometheus


1 Answers

If you read the docs for DecisionTreeClassifier.predict, you can see you are passing the wrong data:

predict(X, check_input=True)

Predict class or regression value for X. For a classification model, the predicted class for each sample in X is returned. For a regression model, the predicted value based on X is returned. Parameters:

X : array-like or sparse matrix of shape = [n_samples, n_features]
like image 52
Padraic Cunningham Avatar answered Dec 11 '25 12:12

Padraic Cunningham



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!