How to save trained model in Python?
MACHINE LEARNING RECIPES DATA CLEANING PYTHON DATA MUNGING PANDAS CHEATSHEET     ALL TAGS

How to save trained model in Python?

How to save trained model in Python?

This recipe helps you save trained model in Python

0

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)

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

Relevant Projects

Music Recommendation System Project using Python and R
Machine Learning Project - Work with KKBOX's Music Recommendation System dataset to build the best music recommendation engine.

Ensemble Machine Learning Project - All State Insurance Claims Severity Prediction
In this ensemble machine learning project, we will predict what kind of claims an insurance company will get. This is implemented in python using ensemble machine learning algorithms.

Walmart Sales Forecasting Data Science Project
Data Science Project in R-Predict the sales for each department using historical markdown data from the Walmart dataset containing data of 45 Walmart stores.

German Credit Dataset Analysis to Classify Loan Applications
In this data science project, you will work with German credit dataset using classification techniques like Decision Tree, Neural Networks etc to classify loan applications using R.

Choosing the right Time Series Forecasting Methods
There are different time series forecasting methods to forecast stock price, demand etc. In this machine learning project, you will learn to determine which forecasting method to be used when and how to apply with time series forecasting example.

Predict Employee Computer Access Needs in Python
Data Science Project in Python- Given his or her job role, predict employee access needs using amazon employee database.

Data Science Project-TalkingData AdTracking Fraud Detection
Machine Learning Project in R-Detect fraudulent click traffic for mobile app ads using R data science programming language.

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.

Credit Card Fraud Detection as a Classification Problem
In this data science project, we will predict the credit card fraud in the transactional dataset using some of the predictive models.

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