[Python][Scikit-learn][ML study notes 03] Local storage and curing of models

>Extended data records

Scikit-Learn文档:Model persistence

pickle — Python object serialization

Persistence


>Joblib

The Joblib used here is used for python object curing:

# save the object - joblib
from sklearn.externals import joblib
# compressed storage
joblib.dump(my_object, 'my_objectpikcel.pkl', compress=True)
# load model
reload_obj = joblib.load('my_object.pkl')


Example: Local solidification of a prediction model on the iris dataset (logistic regression)

Sample code (Jupyter file) download: https://download.csdn.net/download/shenpibaipao/10388901

Function function:

# cartography
import matplotlib.pyplot as plt
def paint(predicted, target):
    fig, ax = plt.subplots() # import matplotlib.pyplot as plt  
    ax.plot(range(len(predicted)), predicted, 'gx', label='Predicted Class')  
    ax.plot(range(len(target)), target, 'r--', label='True Class')  
    plt.show()# http://blog.csdn.net/shenpibaipao
# Calculate the correct rate
def acc(predicted, target):
    sums = 0  
    for i in range(len(predicted)):  
        if predicted[i] == target[i]:  
            sums += 1  
    print 'ACC: ', sums * 100.0 / len(predicted), "%"
# Calculate the correct rate & map
def acc_and_paint(predicted, target):
    acc(predicted, target)
    paint(predicted, target)
Train and predict models:
from sklearn import datasets
from sklearn import linear_model
from sklearn import model_selection
# import dataset
iris = datasets.load_iris() # from sklearn import datasets
# split the dataset
train_X, test_X, train_y, test_y = \
   model_selection.train_test_split(iris.data,
                         iris.target,
                         test_size=0.2,
                         random_state=45)
# import model
lg = linear_model.LogisticRegression(multi_class='ovr') # Multi-classification strategy using one-vs-rest
lg.fit(train_X, train_y)
predicted = lg.predict(test_X)
# Determine the classification error rate and draw a map
acc_and_paint(predicted, test_y)
Save the model and load it for use:
# save the model-joblib
from sklearn.externals import joblib
# compressed storage
joblib.dump(lg, 'joblib_my_lg_pikcel.pkl', compress=True)
# load model
reload_lg=joblib.load('joblib_my_lg_pikcel.pkl')
# Use the loaded model to make predictions
predicted = reload_lg.predict(test_X)   
# Determine the classification error rate and draw a map
acc_and_paint(predicted, test_y)
The result is as follows:




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325644682&siteId=291194637