kevbuntu - 6 months ago 60

Python Question

I tried to use sklearn to use a simple decision tree classifier, and it complained that using a 1D array is now depricated and must use X.reshape(1,-1). So I did but it has turned my labels list to a list of lists with only one element so number of labels and samples do not match now. Another words my list of labels=[0,0,1,1] turns into [[0 0 1 1]]. Thanks

This is the simple code that I used:

`from sklearn import tree`

import numpy as np

features =[[140,1],[130,1],[150,0],[170,0]]

labels=[0,0,1,1]

labels = np.array(labels).reshape(1,-1)

clf = tree.DecisionTreeClassifier()

clf = clf.fit(features,labels)

print clf.predict([150,0])

Answer

You are reshaping the wrong thing. Reshape the data your are predicting on, not your labels.

```
>>> clf.predict(np.array([150,0]).reshape(1,-1))
array([1])
```