using x.reshape on a 1D array in sklearn
<p>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</p>

<p>This is the simple code that I used:</p>

<pre><code>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])
</code></pre>

<p>You are reshaping the wrong thing. Reshape the data your are predicting on, not your labels.</p>

<pre><code>&gt;&gt;&gt; clf.predict(np.array([150,0]).reshape(1,-1))
array([1])
</code></pre>

<p>Your labels have to align with your training data (features) ,so the length of both arrays should be the same. If labels is reshaped, you are right, it is a list of lists with a length of 1 and not equal to the length of your features.</p>

<p>You have to reshape your test data because prediction needs an array that looks like your training data. i.e. each index needs to be a training example with the same number of features as in training. You'll see that the following two commands return a list of lists and just a list respectively.</p>

<pre><code>&gt;&gt;&gt; np.array([150,0]).reshape(1,-1)
array([[150,   0]])
&gt;&gt;&gt; np.array([150,0])
array([150,   0])
</code></pre>

python scikit-learn