A column-vector y was passed when a 1d array was expected A column-vector y was passed when a 1d array was expected python python

A column-vector y was passed when a 1d array was expected


Change this line:

model = forest.fit(train_fold, train_y)

to:

model = forest.fit(train_fold, train_y.values.ravel())

Edit:

.values will give the values in an array. (shape: (n,1)

.ravel will convert that array shape to (n, )


I also encountered this situation when I was trying to train a KNN classifier. but it seems that the warning was gone after I changed:
knn.fit(X_train,y_train)
to
knn.fit(X_train, np.ravel(y_train,order='C'))

Ahead of this line I used import numpy as np.


I had the same problem. The problem was that the labels were in a column format while it expected it in a row.use np.ravel()

knn.score(training_set, np.ravel(training_labels))

Hope this solves it.