实现机器学习的分步指南 VIII - 线性回归
易于实现的机器学习
引言
变量之间普遍存在关系。实际上,这种关系可以分为两类,即确定关系和不确定关系。确定关系可以用函数表示。确定关系也称为相关关系,可以用回归分析进行研究。
通常,线性回归模型为:
最优 可以通过最小化损失函数来确定:
回归模型
线性回归包括线性回归、局部加权线性回归、岭回归、Lasso 回归和逐步线性回归。
线性回归
线性回归的参数 可以通过梯度下降法或**正则表达式**计算。 由于梯度下降法已在逐步实现机器学习 IV - 逻辑回归中介绍,因此我们在本文中介绍使用正则表达式的解法。
首先,计算损失函数的导数:
然后,使导数等于 0,我们可以得到:
最后, 为:
其中 X 是训练数据,Y 是相应的标签。线性回归的代码如下所示:
def standardLinearRegression(self, x, y):
if self.norm_type == "Standardization":
x = preProcess.Standardization(x)
else:
x = preProcess.Normalization(x)
xTx = np.dot(x.T, x)
if np.linalg.det(xTx) == 0: # calculate the Determinant of xTx
print("Error: Singluar Matrix !")
return
w = np.dot(np.linalg.inv(xTx), np.dot(x.T, y))
return w
局部加权线性回归
由于线性回归使用最小均方误差 (MMSE) 的无偏估计,因此存在欠拟合。为了解决这个问题,我们为要预测的点周围的点分配权重。 然后,我们对其应用正态回归分析。 局部加权线性回归的损失函数为:
与线性回归一样,我们计算损失函数的导数并使其等于 0。最优 为:
局部加权线性回归中的权重类似于 SVM 中的核函数,由下式给出:
局部加权线性回归的代码如下所示:
def LWLinearRegression(self, x, y, sample):
if self.norm_type == "Standardization":
x = preProcess.Standardization(x)
else:
x = preProcess.Normalization(x)
sample_num = len(x)
weights = np.eye(sample_num)
for i in range(sample_num):
diff = sample - x[i, :]
weights[i, i] = np.exp(np.dot(diff, diff.T)/(-2 * self.k ** 2))
xTx = np.dot(x.T, np.dot(weights, x))
if np.linalg.det(xTx) == 0:
print("Error: Singluar Matrix !")
return
result = np.dot(np.linalg.inv(xTx), np.dot(x.T, np.dot(weights, y)))
return np.dot(sample.T, result)
岭回归
如果特征维度大于样本数,则输入矩阵不是满秩的,其逆矩阵不存在。 为了解决这个问题,岭回归添加 使矩阵非奇异。 实际上,这等于在岭回归的损失函数上添加 **L2 正则化**,即:
与线性回归一样,我们计算损失函数的导数并使其等于 0。最优 为:
岭回归的代码如下所示:
def ridgeRegression(self, x, y):
if self.norm_type == "Standardization":
x = preProcess.Standardization(x)
else:
x = preProcess.Normalization(x)
feature_dim = len(x[0])
xTx = np.dot(x.T, x)
matrix = xTx + np.exp(feature_dim)*self.lamda
if np.linalg.det(xTx) == 0:
print("Error: Singluar Matrix !")
return
w = np.dot(np.linalg.inv(matrix), np.dot(x.T, y))
return w
Lasso 回归
与岭回归一样,Lasso 回归在损失函数上添加 **L1 正则化**,即:
由于 L1 正则化包含绝对值表达式,因此损失函数在任何地方都不可导。 因此,我们应用**坐标下降法**(CD)。 CD 在每次迭代时沿一个方向获得最小值,即:
我们可以得到 CD 的闭合解,其由下式给出:
其中
Lasso 回归的代码如下所示:
def lassoRegression(self, x, y):
if self.norm_type == "Standardization":
x = preProcess.Standardization(x)
else:
x = preProcess.Normalization(x)
y = np.expand_dims(y, axis=1)
sample_num, feataure_dim = np.shape(x)
w = np.ones([feataure_dim, 1])
for i in range(self.iterations):
for j in range(feataure_dim):
h = np.dot(x[:, 0:j], w[0:j]) + np.dot(x[:, j+1:], w[j+1:])
w[j] = np.dot(x[:, j], (y - h))
if j == 0:
w[j] = 0
else:
w[j] = self.softThreshold(w[j])
return w
逐步线性回归
逐步线性回归与 Lasso 类似,它在每次迭代时应用贪婪算法来获得最小值,而不是 CD。 逐步线性回归在每次迭代时在权重上添加或减少一小部分。 逐步线性回归的代码如下所示:
def forwardstepRegression(self, x, y):
if self.norm_type == "Standardization":
x = preProcess.Standardization(x)
else:
x = preProcess.Normalization(x)
sample_num, feature_dim = np.shape(x)
w = np.zeros([self.iterations, feature_dim])
best_w = np.zeros([feature_dim, 1])
for i in range(self.iterations):
min_error = np.inf
for j in range(feature_dim):
for sign in [-1, 1]:
temp_w = best_w
temp_w[j] += sign * self.learning_rate
y_hat = np.dot(x, temp_w)
error = ((y - y_hat) ** 2).sum() # MSE
if error < min_error: # save the best parameters
min_error = error
best_w = temp_w
w = best_w
return w
结论与分析
有很多解决方案可以获得线性回归的最优参数。 在本文中,我们只介绍一些基本算法。 最后,让我们将我们的线性回归与 Sklearn 中的线性回归进行比较,检测性能显示如下:
Sklearn 线性回归性能
我们的线性回归性能
性能看起来相似。
本文相关的代码和数据集可以在 MachineLearning 中找到。
历史
- 2019年5月28日:初始版本