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:


Download Materials

Relevant Projects

Loan Eligibility Prediction using Gradient Boosting Classifier
This data science in python project predicts if a loan should be given to an applicant or not. We predict if the customer is eligible for loan based on several factors like credit score and past history.

Predict Churn for a Telecom company using Logistic Regression
Machine Learning Project in R- Predict the customer churn of telecom sector and find out the key drivers that lead to churn. Learn how the logistic regression model using R can be used to identify the customer churn in telecom dataset.

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.

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.

Digit Recognition using CNN for MNIST Dataset in Python
In this deep learning project, you will build a convolutional neural network using MNIST dataset for handwritten digit recognition.

Build a Face Recognition System in Python using FaceNet
In this deep learning project, you will build your own face recognition system in Python using OpenCV and FaceNet by extracting features from an image of a person's face.

Resume parsing with Machine learning - NLP with Python OCR and Spacy
In this machine learning resume parser example we use the popular Spacy NLP python library for OCR and text classification.

Inventory Demand Forecasting using Machine Learning in R
In this machine learning project, you will develop a machine learning model to accurately forecast inventory demand based on historical sales data.

Classification - Zero to hero - Part 1
Classification is one of the basic things in ML and most of us jump to Neural networks or boosting to predict classes. But more often than not, to make the other person understand how the classification is happening, we need to use basic models like Logistic, decision trees etc. In this project we talk about you can apply various basic techniques, the maths and intuition behind them and how they paved way to bagging and boosting of the world

Identifying Product Bundles from Sales Data Using R Language
In this data science project in R, we are going to talk about subjective segmentation which is a clustering technique to find out product bundles in sales data.