机器学习:sklearn训练结果的保存和加载

版权声明:本文为博主原创文章,欢迎转载,请注明出处 https://blog.csdn.net/mouday/article/details/86652953

API

sklearn.externals.joblib

文件格式:pkl

代码示例

from sklearn.datasets import load_boston
from sklearn.externals import joblib
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler

# 加载数据
boston = load_boston()

# 训练集,测试集拆分
X_train, X_test, y_train, y_test = train_test_split(
    boston.data, boston.target, test_size=0.25)

# 数据标准化处理
# 特征值 标准化
std_x = StandardScaler()
X_train = std_x.fit_transform(X_train)
X_test = std_x.transform(X_test)

# 目标值 标准化
std_y = StandardScaler()
y_train = std_y.fit_transform(y_train.reshape(-1, 1))
y_test = std_y.transform(y_test.reshape(-1, 1))

# 训练数据并序列化训练结果
# lr = LinearRegression()
# lr.fit(X_train, y_train)
# joblib.dump(lr, "boston.pkl")

# 反序列化保存的训练结果
lr = joblib.load("boston.pkl")

y_lr_predict = std_y.inverse_transform(lr.predict(X_test))
print(y_lr_predict)

没有更多推荐了,返回首页