Simple and crude understanding and implementation of machine learning linear regression (XI): save and load API sklearn model, the model of linear regression saving load case

Linear Regression

learning target

  • Ownership in the implementation process of linear regression
  • Application LinearRegression or SGDRegressor achieve regression prediction
  • We know the assessment criteria and formulas regression algorithm
  • Overfitting know the causes and solutions underfitting
  • We know principle ridge regression and linear regression differences
  • Application Ridge achieve regression prediction
  • Application joblib achieve saving and loading models

Saving and loading of 2.11 model

Here Insert Picture Description

Saving and loading API 1 sklearn model

  • from sklearn.externals import joblib
    • Save: joblib.dump (estimator, 'test.pkl')
    • Load: estimator = joblib.load ( 'test.pkl')

2 linear regression model to save load case

def load_dump_demo():
    """
    线性回归:岭回归
    :return:
    """
    # 1.获取数据
    data = load_boston()

    # 2.数据集划分
    x_train, x_test, y_train, y_test = train_test_split(data.data, data.target, random_state=22)

    # 3.特征工程-标准化
    transfer = StandardScaler()
    x_train = transfer.fit_transform(x_train)
    x_test = transfer.fit_transform(x_test)

    # 4.机器学习-线性回归(岭回归)
    # # 4.1 模型训练
    # estimator = Ridge(alpha=1)
    # estimator.fit(x_train, y_train)
    #
    # # 4.2 模型保存
    # joblib.dump(estimator, "./data/test.pkl")

    # 4.3 模型加载
    estimator = joblib.load("./data/test.pkl")

    # 5.模型评估
    # 5.1 获取系数等值
    y_predict = estimator.predict(x_test)
    print("预测值为:\n", y_predict)
    print("模型中的系数为:\n", estimator.coef_)
    print("模型中的偏置为:\n", estimator.intercept_)

    # 5.2 评价
    # 均方误差
    error = mean_squared_error(y_test, y_predict)
    print("误差为:\n", error)
Published 596 original articles · won praise 790 · Views 100,000 +

Guess you like

Origin blog.csdn.net/qq_35456045/article/details/104643057