Skip to content Skip to sidebar Skip to footer

Row-wise Prediction Over Pandas Dataframe By Passing Sklearn.predict To Df.apply

Assuming we have a Pandas dataframe and a scikit-learn model, trained (fit) using that dataframe. Is there a way to do row-wise prediction? The use case is to use the predict funct

Solution 1:

Slightly more verbose, you can turn each row into 2D array by adding new a new axis to the values. You will then have to access the prediction with 0 index:

df["predictions"] = df[["input1", "input2"]].apply(
    lambda s: model.predict(s.values[None])[0], axis=1
)

Post a Comment for "Row-wise Prediction Over Pandas Dataframe By Passing Sklearn.predict To Df.apply"