How to save trained model in Python?
MACHINE LEARNING RECIPES

How to save trained model in Python?

How to save trained model in Python?

This recipe helps you save trained model in Python

0
In [1]:
## How to save trained model in Python
## DataSet: skleran.datasets.load_breast_cancer()
def Snippet_185():
    print()
    print(format('How to save trained model in Python','*^82'))
    import warnings
    warnings.filterwarnings("ignore")
    # load libraries
    from sklearn import model_selection, datasets
    from sklearn.tree import DecisionTreeClassifier
    from sklearn.externals import joblib
    import pickle
    # load dataset
    dataset = datasets.load_breast_cancer()
    X = dataset.data; y = dataset.target
    X_train, X_test, y_train, y_test = model_selection.train_test_split(X, y, test_size=0.25)
    # Fit the model on 33%
    model = DecisionTreeClassifier()
    model.fit(X_train, y_train)

    # save the model to disk using Pickle
    filename = 'trained_model.pickle'
    pickle.dump(model, open(filename, 'wb'))
    # load the model from disk
    loaded_model = pickle.load(open(filename, 'rb'))
    result = loaded_model.score(X_test, y_test)
    print(); print(result)

    # save the model to disk using Joblib
    filename = 'trained_model.joblib'
    joblib.dump(model, filename)

    # load the model from disk
    loaded_model = joblib.load(filename)
    result = loaded_model.score(X_test, y_test)
    print(); print(result)
Snippet_185()
***********************How to save trained model in Python************************

0.916083916083916

0.916083916083916

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.

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.

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.

Data Science Project in Python on BigMart Sales Prediction
The goal of this data science project is to build a predictive model and find out the sales of each product at a given Big Mart store.

Human Activity Recognition Using Smartphones Data Set
In this deep learning project, you will build a classification system where to precisely identify human fitness activities.

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.

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.

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.

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.

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.