Issue
I am fitting a sklearn model on pandas dataframe and then trying to predict it on each row. Because the fitting and prediction dimension is different, I am facing following error.
import pandas as pd
from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression
iris_dict = load_iris()
X = pd.DataFrame(iris_dict['data'])
y = pd.Series(iris_dict['target'])
clf = LogisticRegression()
clf.fit(X, y)
y_pred = clf.predict(X.loc[0,:])
Prediction on single row gives me an error
Expected 2D array, got 1D array instead:
How can I predict on each pandas row, one at a time. I have tried reshaping, it didn't work
Solution
Sklearn works with columns and primarily numpy. Your X.loc[0,:]
is a series meaning when it is converted to numpy it is a 1D array. I believe simply calling X.loc[0,:].to_numpy().reshape(1,-1)
would work
Answered By - Jeff
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.