Using pretrained model with sample features

I am new to machine learning, and I have trained a linear regression model. I have loaded the pretrained model, but I don`t know how to put my features into the model and get the prediction.

I have tried this code and I expected 1 prediction with these 10 features, all this 10 features are what the model needs: x = [2.0 , 2.4, 1.5, 3.5, 3.5, 3.5, 3.5, 3.7, 3.7] linear.fit(x, y) predict_x = linear.predict(x) print(f'Predict = {predict_x}') But I got this error:

Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.

Upvotes: 0

Views: 52

Answers (1)

Harsh Mistry
Harsh Mistry

Reputation: 1

It depends on which library you have used to save the model. Like if you have used Joblib then it will be like this:

import joblib
#Saving a Pre-trained model
joblib.dump(model_name, 'file_name')

#Using an Pre-trained model
model = joblib.load('file_name')
model.predict([[2.0 , 2.4, 1.5, 3.5, 3.5, 3.5, 3.5, 3.7, 3.7]])

Here, For saving the model, the "model_name" will be replaced by the name of your model and "file_name" will be replaced by the name you want to save your model with. For Exporting and using a pre-trained model you only have to replace "file_name" with your saved model file name.

If you're using Pickle then it will be like this:

import pickle
with open("model_pickle", 'wb') as f:
    pickle.dump(model_name, f)

#To load a Pre-Trained model
with open("model_pickle", 'rb') as f:
    model = pickle.load(f)

model.predict([[2.0 , 2.4, 1.5, 3.5, 3.5, 3.5, 3.5, 3.7, 3.7]])

Here, You have to replace "model_name" with the name you have given to the model you have created.

Upvotes: 0

Related Questions