版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Maverick_7/article/details/82355245
from sklearn.linear_model import LogisticRegression
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
# Load and split data
data = load_iris()
Xtrain, Xtest, Ytrain, Ytest = train_test_split(data.data, data.target, test_size=0.3, random_state=4)
# Create a model
model = LogisticRegression(C=0.1,
max_iter=20,
fit_intercept=True,
n_jobs=3,
solver='liblinear')
model.fit(Xtrain, Ytrain)
"""
our resulting model:
LogisticRegression(C=0.1, class_weight=None, dual=False, fit_intercept=True,
intercept_scaling=1, max_iter=20, multi_class='ovr', n_jobs=3,
penalty='l2', random_state=None, solver='liblinear', tol=0.0001,
verbose=0, warm_start=False)
"""
Pickle Module (also: cPickle)
导入pickle模块保存模型到本地:
import pickle
#
# Create your model here (same as above)
#
# Save to file in the current working directory
pkl_filename = "pickle_model.pkl"
with open(pkl_filename, 'wb') as file:
pickle.dump(model, file)
# Load from file
with open(pkl_filename, 'rb') as file:
pickle_model = pickle.load(file)
# Calculate the accuracy score and predict target values
score = pickle_model.score(Xtest, Ytest)
print("Test score: {0:.2f} %".format(100 * score))
Ypredict = pickle_model.predict(Xtest)
也可以将一些过程中的参数通过tuple的形式保存下来:
tuple_objects = (model, Xtrain, Ytrain, score)
# Save tuple
pickle.dump(tuple_objects, open("tuple_model.pkl", 'wb'))
# Restore tuple
pickled_model, pickled_Xtrain, pickled_Ytrain, pickled_score = pickle.load(open("tuple_model.pkl", 'rb'))
Joblib Module
from sklearn.externals import joblib
# Save to file in the current working directory
joblib_file = "joblib_model.pkl"
joblib.dump(model, joblib_file)
# Load from file
joblib_model = joblib.load(joblib_file)
# Calculate the accuracy and predictions
score = joblib_model.score(Xtest, Ytest)
print("Test score: {0:.2f} %".format(100 * score))
Ypredict = pickle_model.predict(Xtest)
也可以自己DIY存储模块,Manual Save and Restore to JSON
import json
import numpy as np
class MyLogReg(LogisticRegression):
# Override the class constructor
def __init__(self, C=1.0, solver='liblinear', max_iter=100, X_train=None, Y_train=None):
LogisticRegression.__init__(self, C=C, solver=solver, max_iter=max_iter)
self.X_train = X_train
self.Y_train = Y_train
# A method for saving object data to JSON file
def save_json(self, filepath):
dict_ = {}
dict_['C'] = self.C
dict_['max_iter'] = self.max_iter
dict_['solver'] = self.solver
dict_['X_train'] = self.X_train.tolist() if self.X_train is not None else 'None'
dict_['Y_train'] = self.Y_train.tolist() if self.Y_train is not None else 'None'
# Creat json and save to file
json_txt = json.dumps(dict_, indent=4)
with open(filepath, 'w') as file:
file.write(json_txt)
# A method for loading data from JSON file
def load_json(self, filepath):
with open(filepath, 'r') as file:
dict_ = json.load(file)
self.C = dict_['C']
self.max_iter = dict_['max_iter']
self.solver = dict_['solver']
self.X_train = np.asarray(dict_['X_train']) if dict_['X_train'] != 'None' else None
self.Y_train = np.asarray(dict_['Y_train']) if dict_['Y_train'] != 'None' else None
应用模块:
filepath = "mylogreg.json"
# Create a model and train it
mylogreg = MyLogReg(X_train=Xtrain, Y_train=Ytrain)
mylogreg.save_json(filepath)
# Create a new object and load its data from JSON file
json_mylogreg = MyLogReg()
json_mylogreg.load_json(filepath)
json_mylogreg
"""
MyLogReg(C=1.0,
X_train=array([[ 4.3, 3. , 1.1, 0.1],
[ 5.7, 4.4, 1.5, 0.4],
...,
[ 7.2, 3. , 5.8, 1.6],
[ 7.7, 2.8, 6.7, 2. ]]),
Y_train=array([0, 0, ..., 2, 2]), class_weight=None, dual=False,
fit_intercept=True, intercept_scaling=1, max_iter=100,
multi_class='ovr', n_jobs=1, penalty='l2', random_state=None,
solver='liblinear', tol=0.0001, verbose=0, warm_start=False)
"""