How to make prediction with single sample in sklearn model.predict?

Viewed 8346

I trained a logistic regression model with some data. I applied standard scalar to train and test data, trained model. But if I want to make prediction with the model with the data outside the train and test data, I have to apply standard scalar to new data but what if I have single data than i cannot apply standard scalar to that new single sample that i want to give as input. What should be the procedure to predict results with new data especially single sample at a time?

1 Answers

The predict() method always expects a 2D array of shape [n_samples, n_features]. This means that if you want to predict even for a single data point, you will have to convert it into a 2D array.

Converting data into a 2D array using reshape

# Sample data
print(arr)
[1, 2, 3, 4]

# Reshaping into 2D
arr.reshape(1, -1)

# Result
array([[1, 2, 3, 4]])

This array can now be transformed using standard scalar using transform() method before being used to generate a prediction from the model.

Related