第十四节 模型的保存和加载

已经得到理想的模型以后,可以通过sklearn中的 from sklearn.externals import joblib 将模型保存下来,下次可以直接将想要预测的数据给入就好了

from sklearn.datasets import load_boston
from sklearn.linear_model import LinearRegression, SGDRegressor
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import mean_squared_error  # 回归方程性能评价均方误差API
from sklearn.externals import joblib  # 保存模型API

# 保存模型
def boston_linear():
    '''线性回归预测波士顿房价'''
    # 获取数据
    bl = load_boston()

    # 分割训练集和测试集
    x_train, x_test, y_train, y_test = train_test_split(bl.data, bl.target, test_size=0.25)

    # 进行标准化,特征值和目标值都需要进行标准化处理
    # 特征值
    std_x = StandardScaler()
    x_train = std_x.fit_transform(x_train)
    x_test = std_x.fit_transform(x_test)

    # 目标值,y_train.reshape(-1, 1)将一维数组转换成二维数组
    std_y = StandardScaler()
    y_train = std_y.fit_transform(y_train.reshape(-1, 1))
    y_test = std_y.fit_transform(y_test.reshape(-1, 1))

    # 预测
    # 最小二乘法求解结果
    lr = LinearRegression()
    lr.fit(x_train, y_train)

    # 保存模型
    joblib.dump(lr, "./test.pkl")

    print(lr.coef_)

    # 预测测试集的房子价格
    lr_y_predict = std_y.inverse_transform(lr.predict(x_test))
    print('最小二乘法测试集里面每个房子的价格', lr_y_predict)

    # 用均方误差对一个回归模型来进行评价,越小越好,mean_squared_error第一个参数是测试集的真实值,第二个参数数测试集的预测值
    print('最小二乘法的均方误差:', mean_squared_error(std_y.inverse_transform(y_test), lr_y_predict))

if __name__ == "__main__":
    boston_linear()
from sklearn.datasets import load_boston
from sklearn.linear_model import LinearRegression, SGDRegressor
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import mean_squared_error  # 回归方程性能评价均方误差API
from sklearn.externals import joblib  # 保存模型API

# 加载模型
def boston_linear():
    '''线性回归预测波士顿房价'''
    # 获取数据
    bl = load_boston()

    # 分割训练集和测试集
    x_train, x_test, y_train, y_test = train_test_split(bl.data, bl.target, test_size=0.25)

    # 进行标准化,特征值和目标值都需要进行标准化处理
    # 特征值
    std_x = StandardScaler()
    x_train = std_x.fit_transform(x_train)
    x_test = std_x.fit_transform(x_test)

    # 目标值,y_train.reshape(-1, 1)将一维数组转换成二维数组
    std_y = StandardScaler()
    y_train = std_y.fit_transform(y_train.reshape(-1, 1))
    y_test = std_y.fit_transform(y_test.reshape(-1, 1))

    lr = joblib.load("./test.pkl")
    y_predict = std_y.inverse_transform(lr.predict(x_test))
    print("保存的模型预测结果", y_predict)


if __name__ == "__main__":
    boston_linear()

 

posted @ 2020-03-26 23:07  kog_maw  阅读(148)  评论(0编辑  收藏  举报