DeprecationWarning while using knn algorithm in scikit-learn
<p>I am trying my hands on scikit-learn library. I imported the iris dataset, and tried to train knn algorithm to predict some outcomes. Here is the code:</p>

<pre><code>from sklearn import datasets
from sklearn.neighbors import KNeighborsClassifier

iris = datasets.load_iris()

knn = KNeighborsClassifier(n_neighbors=1)

X = iris.data
y = iris.target

print X.shape
print y.shape

#training the model
knn.fit(X, y)

knn.predict([3, 4, 5, 2])
</code></pre>

<p>But I get the following error:</p>

<pre><code>(150L, 4L)
(150L,)
DeprecationWarning: Passing 1d arrays as data is deprecated in 0.17 and willraise ValueError in 0.19. Reshape your data either using X.reshape(-1, 1) if your data has a single feature or X.reshape(1, -1) if it contains a single sample.
  DeprecationWarning)
</code></pre>

<p>I searched on google and found some workarounds. I tried using <code>X = X.reshape(-1, 1)</code> and also <code>X = X.reshape(1, -1)</code>, but then I get the following error:</p>

<pre><code>Traceback (most recent call last):
  File "E:/Analytics Practice/Social Media Analytics/Python Services/DataAnalysis/sk-learn-dir/test.py", line 13, in &lt;module&gt;
    knn.fit(X, y)
  File "C:\python-venv-test-2.7.10\lib\site-packages\sklearn\neighbors\base.py", line 778, in fit
    X, y = check_X_y(X, y, "csr", multi_output=True)
  File "C:\python-venv-test-2.7.10\lib\site-packages\sklearn\utils\validation.py", line 520, in check_X_y
    check_consistent_length(X, y)
  File "C:\python-venv-test-2.7.10\lib\site-packages\sklearn\utils\validation.py", line 176, in check_consistent_length
    "%s" % str(uniques))
ValueError: Found arrays with inconsistent numbers of samples: [150 600]
</code></pre>

<p>What is the correct format of dimensions that the knn algorithm requires to be trained in scikit-learn?</p>

<p>Thank you @tttthomasssss for the help. Here is what I was doing wrong:</p>

<p>When I write <code>[3, 4, 5, 2]</code>, python interprets it as an array of dimensions 4X1, but when I write <code>[[3, 4, 5, 2]]</code>, python interprets it as 1X4 array. Since it is one data point having 4 different values for different features, I will have to input the predict model with <code>[[3, 4, 5, 2]]</code>. Here is the code which helped me to figure out the dimensions of both the arrays:</p>

<pre><code>predict_array = [3, 4, 5, 2]
predict_array = np.asarray(predict_array)
print predict_array.shape

predict_array = [[3, 4, 5, 2]]
predict_array = np.asarray(predict_array)
print predict_array.shape
</code></pre>

<p>And here is the output:</p>

<pre><code>(4L,)
(1L, 4L)
</code></pre>

python scikit-learn knn