Menu

Saving and Loading Machine Learning Models

Saving and Loading Machine Learning Models

Before deploying a model, you need to save it to disk so you don't have to retrain it every time you use it.

Step 1: Train the Model


from sklearn.linear_model import LogisticRegression

model = LogisticRegression()
model.fit(X_train, y_train)

Step 2: Save the Trained Model

The joblib library is commonly used to save Scikit-learn models efficiently, especially when the model contains large numerical data.

import joblib

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

Step 3: Load the Saved Model

Once saved, you can load the model back at any time, without retraining, using the same library.

loaded_model = joblib.load("model.joblib")

Step 4: Use the Loaded Model for Predictions

predictions = loaded_model.predict(X_test)
print(predictions)

You can now move this saved file to a server, an application, or any environment where the model needs to run, making it the foundation for deployment.


Machine Learning Tutorial

AP

Abhishek Pati



Learn via Video Course

Artificial Intelligence and Machine Learning Course Logo

Artificial Intelligence and Machine Learning Course

55 hrs



Get in Touch For Details