Menu

Saving the Trained Model

Saving the Trained Model

Once the model has been trained, it can be saved and reused without retraining. This is especially useful when deploying the model in a web application or API.

We will use the joblib library to save both the trained Logistic Regression model and the TF-IDF vectorizer.

Import Joblib

import joblib

Save the Model

joblib.dump(model, "fake_review_detector.pkl")

Save the TF-IDF Vectorizer

joblib.dump(tfidf, "tfidf_vectorizer.pkl")

Load the Saved Model

loaded_model = joblib.load("fake_review_detector.pkl")

loaded_vectorizer = joblib.load("tfidf_vectorizer.pkl")

Predict Using the Loaded Model

sample = "Excellent product with fast delivery."

sample = clean_text(sample)

sample = remove_stopwords(sample)

sample = lemmatize_text(sample)

sample_vector = loaded_vectorizer.transform([sample])

prediction = loaded_model.predict(sample_vector)

print(prediction)