从头开始使用梯度下降优化在Python中实现多元线性回归(后续)
使用3-D散点图对特征和目标变量的实际和预测进行并排可视化:
=>实际目标变量可视化:
from matplotlib import pyplot
from mpl_toolkits.mplot3d import Axes3Dsequence_containing_x_vals = list(X_train.transpose()[0])
sequence_containing_y_vals = list(X_train.transpose()[1])
sequence_containing_z_vals = list(y_train)fig = pyplot.figure()
ax = Axes3D(fig)ax.scatter(sequence_containing_x_vals, sequence_containing_y_vals,
sequence_containing_z_vals)
ax.set_xlabel('Living Room Area', fontsize=10)
ax.set_ylabel('Number of Bed Rooms', fontsize=10)
ax.set_zlabel('Actual Housing Price', fontsize=10)
=>预测目标变量的可视化:
# Getting the predictions...
X_train = np.concatenate((np.ones((X_train.shape[0],1)), X_train)
,axis = 1)
predictions = hypothesis(theta, X_train, X_train.shape[1] - 1)from matplotlib import pyplot
from mpl_toolkits.mplot3d import Axes3Dsequence_containing_x_vals = list(X_train.transpose()[1])
sequence_containing_y_vals = list(X_train.transpose()[2])
sequence_containing_z_vals = list(predictions)fig = pyplot.figure()
ax = Axes3D(fig)ax.scatter(sequence_containing_x_vals, sequence_containing_y_vals,
sequence_containing_z_vals)
ax.set_xlabel('Living Room Area', fontsize=10)
ax.set_ylabel('Number of Bed Rooms', fontsize=10)
ax.set_zlabel('Housing Price Predictions', fontsize=10)
实际房价与预计房价
性能分析:
- 平均绝对错误:51502.7803(美元)
- 均方误差:4086560101.2158(以美元为单位)
- 均方根误差:63926.2082(以美元为单位)
- R均分:0.7329
要注意的一件事是,平均绝对误差,均方误差和均方根误差不是单位自由的。为了使它们无单元,在训练模型之前,可以按照相同的方式对目标标签进行缩放,对要素进行缩放。除此之外,还获得了0.7329的下降R-Square-Score。
这就是从头开始使用梯度下降在Python中实现多元线性回归的全部内容。