Simple and crude understanding and implementation of machine learning linear regression (VII): Boston house prices forecast

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

2.7 Case: Boston house prices forecast

Here Insert Picture Description

  • Data presentation

[Image dump the chain fails, the source station may have security chain mechanism, it is recommended to save the picture down uploaded directly (img-x0ToRgCJ-1583245183925) (../ images /% E6% 88% BF% E4% BB% B7% E6 % 95% B0% E6% 8D% AE% E9% 9B% 86% E4% BB% 8B% E7% BB% 8D.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Ew3VVu5s-1583245183926)(../images/%E5%B1%9E%E6%80%A7.png)]

Given these characteristics, the experts have come to affect the price of the property results. We do not need to at this stage whether it is useful to explore the features, you only need to use these features. Back to quantify many of the features we need to find their own

1 analysis

Data inconsistencies among the size of the return, the result will lead to a greater impact. It needs to be done normalized.

  • Dividing the normalized data
  • Regression prediction
  • Linear regression algorithm to evaluate the effect of

2 Return Performance Evaluation

Mean square error (Mean Squared Error) MSE) Evaluation Mechanism:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-vofC3ZUK-1583245183926)(../images/%E7%BA%BF%E6%80%A7%E5%9B%9E%E5%BD%92%E8%AF%84%E4%BC%B0.png)]

Note: yi as the predicted value, ¯y is the true value

  • sklearn.metrics.mean_squared_error(y_true, y_pred)
    • Return loss mean square error
    • y_true: true value
    • y_pred: predictive value
    • return: floating-point results

Code 3

def linear_model1():
    """
    线性回归:正规方程
    :return:None
    """
    # 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.机器学习-线性回归(正规方程)
    estimator = LinearRegression()
    estimator.fit(x_train, y_train)

    # 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)


def linear_model2():
    """
    线性回归:梯度下降法
    :return:None
    """
    # 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.机器学习-线性回归(特征方程)
    estimator = SGDRegressor(max_iter=1000)
    estimator.fit(x_train, y_train)

    # 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)

We can also try to modify the learning rate

estimator = SGDRegressor(max_iter=1000,learning_rate="constant",eta0=0.1)

At this point we can adjust the parameters to find the learning rate better value.

Published 596 original articles · won praise 790 · Views 100,000 +

Guess you like

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