Manu Sharma
Manu Sharma

Reputation: 1739

How to predict a single row from dataframe, after fitting model on complete dataframe

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

Upvotes: 1

Views: 3640

Answers (2)

tdiceman
tdiceman

Reputation: 11

Try y_pred = clf.predict(X.loc[[0]]), that should work because double square brackets returns a dataframe instead of a series.

Upvotes: 1

Jeff
Jeff

Reputation: 634

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

Upvotes: 3

Related Questions