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
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

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.

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

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.

Sequence Classification with LSTM RNN in Python with Keras
In this project, we are going to work on Sequence to Sequence Prediction using IMDB Movie Review Dataset​ using Keras in Python.

PySpark Tutorial - Learn to use Apache Spark with Python
PySpark Project-Get a handle on using Python with Spark through this hands-on data processing spark python tutorial.

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.

Perform Time series modelling using Facebook Prophet
In this project, we are going to talk about Time Series Forecasting to predict the electricity requirement for a particular house using Prophet.

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.

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 Census Income using Deep Learning Models
In this project, we are going to work on Deep Learning using H2O to predict Census income.