How to save trained model in Python?

This recipe helps you save trained model in Python

Recipe Objective

So after using the model, how to save our trained model. So in this recipe we will save our trained model and we will also load the saved model.

So this is the recipe on how we can save trained model in Python.

Step 1 - Import the library

from sklearn import model_selection, datasets from sklearn.tree import DecisionTreeClassifier from sklearn.externals import joblib import pickle

We have imported model_selection, datasets, joblib, DecisionTreeClassifier and pickel which will be needed for the dataset.

Step 2 - Setting up the Data

We have loaded inbuilt wine dataset and stored data in x and target in y. We have used test_train_split to split the dataset such that 30% of data is for testing the model. dataset = datasets.load_wine() X = dataset.data; y = dataset.target X_train, X_test, y_train, y_test = model_selection.train_test_split(X, y, test_size=0.3)

Master the Art of Classification in Machine Learning to Become a Pro

Step 3 - Training and Saving the model

We are using DecisionTreeClassifier as a model. We have trained the model by training data. We can save the model by using joblib.dump in which we have passed the parameter as model and the filename. model = DecisionTreeClassifier() model.fit(X_train, y_train) filename = "Completed_model.joblib" joblib.dump(model, filename)

Step 4 - Loading the saved model

So here we are loading the saved model by using joblib.load and after loading the model we have used score to get the score of the pretrained saved model. loaded_model = joblib.load(filename) result = loaded_model.score(X_test, y_test) print(result) So the output comes as:

0.9444444444444444

Download Materials

What Users are saying..

profile image

Anand Kumpatla

Sr Data Scientist @ Doubleslash Software Solutions Pvt Ltd
linkedin profile url

ProjectPro is a unique platform and helps many people in the industry to solve real-life problems with a step-by-step walkthrough of projects. A platform with some fantastic resources to gain... Read More

Relevant Projects

Deploy Transformer-BART Model on Paperspace Cloud
In this MLOps Project you will learn how to deploy a Tranaformer BART Model for Abstractive Text Summarization on Paperspace Private Cloud

Natural language processing Chatbot application using NLTK for text classification
In this NLP AI application, we build the core conversational engine for a chatbot. We use the popular NLTK text classification library to achieve this.

Census Income Data Set Project-Predict Adult Census Income
Use the Adult Income dataset to predict whether income exceeds 50K yr based oncensus data.

BigMart Sales Prediction ML Project in Python
The goal of the BigMart Sales Prediction ML project is to build and evaluate different predictive models and determine the sales of each product at a store.

Build a Logistic Regression Model in Python from Scratch
Regression project to implement logistic regression in python from scratch on streaming app data.

AWS MLOps Project to Deploy a Classification Model [Banking]
In this AWS MLOps project, you will learn how to deploy a classification model using Flask on AWS.

Deep Learning Project for Text Detection in Images using Python
CV2 Text Detection Code for Images using Python -Build a CRNN deep learning model to predict the single-line text in a given image.

Build Real Estate Price Prediction Model with NLP and FastAPI
In this Real Estate Price Prediction Project, you will learn to build a real estate price prediction machine learning model and deploy it on Heroku using FastAPI Framework.

Build CNN Image Classification Models for Real Time Prediction
Image Classification Project to build a CNN model in Python that can classify images into social security cards, driving licenses, and other key identity information.

NLP Project on LDA Topic Modelling Python using RACE Dataset
Use the RACE dataset to extract a dominant topic from each document and perform LDA topic modeling in python.