Overfitting
Regularization Cost Function
Regularization
ideas Small values for parameters $\theta_0,\theta_1,\dots,\theta_n$
- “Simpler” hyphothesis
- Less prone to overfitting
cost function
Regularized Linear Regression
Repeat {
}
- 一步推导
Normal equation
- 正则化后的标准方程
- 可以解决由于$m \le n$导致的$X^TX$为奇异矩阵的问题。
Regularized Logistic Regression
Cost function
Gradient descent
Repeat {
}
Advanced optimization
matlab
Exercise
- 基于前面线性回归、逻辑回归的代码,加入了正则化的部分。如下的函数有些许改动:
linear_regression.py
def computeCost(self, X, y, theta, lamb_da):
m = len(y)
J = 0
J = (np.sum((X.dot(theta) - y) ** 2) + lamb_da * np.sum(theta**2)) / (2 * m)
return J
def gradientDescent(self, X, y):
m = len(y)
for i in range(self.num_iters):
regulation = self.theta * self.lamb_da
regulation[0] = 0
self.theta = self.theta - self.alpha / m * (X.T.dot(X.dot(self.theta) - y) + regulation)
J = self.computeCost(X, y, self.theta, self.lamb_da)
yield J
logistic_regression.py
def computeCost(self, X, y, theta, lamb_da):
m = len(y)
h = self.sigmoid(X.dot(theta))
J = - (y.T.dot(np.log(h)) + (1.0 - y).T.dot(np.log(1.0 - h))) / m + lamb_da / (2 * m) * np.sum(theta**2)
return np.sum(J)
def gradientDescent(self, X, y):
m = len(y)
for i in range(self.num_iters):
h = self.sigmoid(X.dot(self.theta))
regulation = self.lamb_da * self.theta
regulation[0] = 0
self.theta = self.theta - self.alpha / m * (X.T.dot(h - y) + regulation)
J = self.computeCost(X, y, self.theta, self.lamb_da)
yield J
Exercise
- 首先,在加入了正则化与归一化等处理细节后,将线性回归的多特征实验完成。结果如下:
- 重新更改了逻辑回归的部分细节。得到结果如下: