机器学习实战篇:线性回归及多项式回归实现波士顿房价预测并评估模型

1、简介

本文使用传统机器学习算法线性回归及多项式回归实现波士顿房价数据集预测并评估两种模型

2、使用方法

线性回归、多项式回归、均方误差评估、决定系数评估

3、代码实现

import matplotlib.pyplot as plt
import pandas as pd
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
from sklearn.model_selection import train_test_split

# 数据读取
df = pd.read_csv('../dataset/boston.csv', sep=',')
df.columns = ['CRIM', 'ZN', 'INDUS', 'CHAS',
              'NOX', 'RM', 'AGE', 'DIS', 'RAD',
              'TAX', 'PTRATIO', 'LSTAT', 'MEDV']
# 所有属性拟合线性模型
X = df.iloc[:, :-1].values
y = df[['MEDV']].values
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.3, random_state=0)
# 线性模型训练
slr = LinearRegression()
slr.fit(X_train, y_train)
y_train_pred = slr.predict(X_train)
y_test_pred = slr.predict(X_test)

# 多项式回归模型构造及训练
spr = LinearRegression()
quadratic = PolynomialFeatures()
X_train_quad = quadratic.fit_transform(X_train)
spr.fit(X_train_quad, y_train)
y_train_pred_quad = spr.predict(X_train_quad)
y_test_pred_quad = spr.predict(quadratic.fit_transform(X_test))


# 残差评估方法
plt.scatter(y_train_pred, y_train_pred-y_train,
            c='blue', marker='o', label='Training data')
plt.scatter(y_test_pred, y_test_pred-y_test,
            c='lightgreen', marker='s', label='Test data')
plt.xlabel('Predicted values')
plt.ylabel('Residuals')
plt.legend(loc='upper left')
plt.hlines(y=0, xmin=-10, xmax=50, lw=2, colors='red')
plt.xlim([-10, 50])
plt.savefig('../result/residuals_metric.png')
plt.show()

# 均方误差评价指标
from sklearn.metrics import mean_squared_error
print('MSE train: %.3f, test: %.3f' % (
    mean_squared_error(y_train, y_train_pred),
    mean_squared_error(y_test, y_test_pred))
)
print('PolynomialFeatures MSE train: %.3f, test: %.3f' % (
    mean_squared_error(y_train, y_train_pred_quad),
    mean_squared_error(y_test, y_test_pred_quad))
)

# 决定系数评价指标
from sklearn.metrics import r2_score
print('R^2 train: %.3f, test: %.3f' %
      (r2_score(y_train, y_train_pred),
      r2_score(y_test, y_test_pred)))
print('PolynomialFeatures R^2 train: %.3f, test: %.3f' %
      (r2_score(y_train, y_train_pred_quad),
      r2_score(y_test, y_test_pred_quad)))


4、实验结果
MSE train: 20.217, test: 28.147
PolynomialFeatures MSE train: 5.948, test: 16.181
R^2 train: 0.761, test: 0.662
PolynomialFeatures R^2 train: 0.930, test: 0.806

 

参考: Python机器学习

原创整理,转载请注明出处!!!

你可能感兴趣的:(机器学习实战系列)