Error
Machine learning Error:AttributeError at /prediction/add/ 'function' object has no attribute 'predict' at
prediction=model.predict(pd.DataFrame(columns=['name','company','year','kms_driven','fuel_type'],data=np.array([car_model,company,year,driven,fuel_type]).reshape(1,5)))
my code is
with open('data/LinearRegressionModel.pkl', 'rb') as file:
model = pickle.load(file)
print("model kaa prediction")
print(type(model))
company=form.cleaned_data['make']
car_model=form.cleaned_data['model']
year=int(form.cleaned_data['year'])
fuel_type=form.cleaned_data['fuel']
driven=float(form.cleaned_data['mileage'])
# data = pd.DataFrame({'name': [car_model], 'company': [company], 'year': [year], 'kms_driven': [driven], 'fuel_type': [fuel_type]})
prediction=model.predict(pd.DataFrame(columns=['name','company','year','kms_driven','fuel_type'],data=np.array([car_model,company,year,driven,fuel_type]).reshape(1,5)))
Reason
To resolve this issue, you need to locate the correct pickle file that contains the serialized model object. Make sure that the file you are loading ('data/LinearRegressionModel.pkl') is the correct file and that it contains the model object.
Double-check the code that saves the model to ensure that you are correctly serializing the model object. When saving the model, you should use pickle.dump(model, file) to save the model object itself, not a function.
If you have multiple pickle files in your 'data' directory, make sure you are loading the correct file that contains the serialized model object.
If you are unable to find the correct pickle file or if the pickle file does not contain the expected model object, you might need to retrain and save the model again, ensuring that the correct model object is serialized and saved to the pickle file for later use.
By verifying the correct pickle file and ensuring that the model object is properly serialized and saved, you should be able to load the model object successfully and call the predict method without encountering the AttributeError.
pipe=make_pipeline(column_trans,lr)
pipe.fit(X_train,y_train)
y_pred=pipe.predict(X_test)
r2_score(y_test,y_pred)
print(type(pipe))
with open('data/LinearRegressionModel.pkl', 'wb') as file:
pickle.dump(pipeline, file)
Solution:
replace pickle.dump(pipeline, file) to pickle.dump(pipe, file)
pipe=make_pipeline(column_trans,lr)
pipe.fit(X_train,y_train)
y_pred=pipe.predict(X_test)
r2_score(y_test,y_pred)
print(type(pipe))
with open('data/LinearRegressionModel.pkl', 'wb') as file:
pickle.dump(pipe, file)
pred=pipe.predict(pd.DataFrame(columns=['name','company','year','kms_driven','fuel_type'],data=np.array(['Maruti Suzuki Swift','Maruti',2019,100,'Petrol']).reshape(1,5)))
print('pred data x target')
print(pred)
Top comments (0)