How to save trained model in Python?

How to save trained model in Python?

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 =; y = X_train, X_test, y_train, y_test = model_selection.train_test_split(X, y, test_size=0.3)

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(), 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:


Relevant Projects

Build an Image Classifier for Plant Species Identification
In this machine learning project, we will use binary leaf images and extracted features, including shape, margin, and texture to accurately identify plant species using different benchmark classification techniques.

Customer Market Basket Analysis using Apriori and Fpgrowth algorithms
In this data science project, you will learn how to perform market basket analysis with the application of Apriori and FP growth algorithms based on the concept of association rule learning.

Predict Credit Default | Give Me Some Credit Kaggle
In this data science project, you will predict borrowers chance of defaulting on credit loans by building a credit score prediction model.

Human Activity Recognition Using Multiclass Classification in Python
In this human activity recognition project, we use multiclass classification machine learning techniques to analyse fitness dataset from a smartphone tracker.

Customer Churn Prediction Analysis using Ensemble Techniques
In this machine learning churn project, we implement a churn prediction model in python using ensemble techniques.

Predict Census Income using Deep Learning Models
In this project, we are going to work on Deep Learning using H2O to predict Census income.

Data Science Project - Instacart Market Basket Analysis
Data Science Project - Build a recommendation engine which will predict the products to be purchased by an Instacart consumer again.

Predict Macro Economic Trends using Kaggle Financial Dataset
In this machine learning project, you will uncover the predictive value in an uncertain world by using various artificial intelligence, machine learning, advanced regression and feature transformation techniques.

Topic modelling using Kmeans clustering to group customer reviews
In this Kmeans clustering machine learning project, you will perform topic modelling in order to group customer reviews based on recurring patterns.

Demand prediction of driver availability using multistep time series analysis
In this supervised learning machine learning project, you will predict the availability of a driver in a specific area by using multi step time series analysis.