本篇文章详细介绍了如何手动编写和理解线性回归模型中的梯度下降算法,并提供了具体的Python代码示例。该教程不依赖于Scikit-Learn库,适合初学者深入学习机器学习基础理论与实践操作。
以下是BGD(批量梯度下降)、SGD(随机梯度下降)以及MBGD(小批量梯度下降)的手写代码实现示例:
```python
import numpy as np
class LinearRegression:
def __init__(self, learning_rate=0.01, n_iterations=1000):
self.learning_rate = learning_rate
self.n_iterations = n_iterations
# 批量梯度下降BGD实现
def fit_BGD(self, X_train, y_train):
m_samples, n_features = X_train.shape
self.weights = np.zeros((n_features, 1))
self.bias = 0
for _ in range(self.n_iterations):
y_pred = np.dot(X_train, self.weights) + self.bias
dw = (1 / m_samples) * np.dot(X_train.T, (y_pred - y_train))
db = (1 / m_samples) * np.sum(y_pred - y_train)
self.weights -= self.learning_rate * dw
self.bias -= self.learning_rate * db
# 随机梯度下降SGD实现
def fit_SGD(self, X_train, y_train):
m_samples = X_train.shape[0]
for i in range(m_samples):
rand_ind = np.random.randint(0, m_samples)
x_i = X_train[rand_ind].reshape(-1, 1)
y_i = y_train[rand_ind].reshape(-1, 1)
y_pred = self.predict(x_i)
dw = (x_i.T @ ((y_pred - y_i)))
db = np.sum((y_pred - y_i))
self.weights -= self.learning_rate * dw
self.bias -= self.learning_rate * db
# 小批量梯度下降MBGD实现
def fit_MBGD(self, X_train, y_train, batch_size=32):
m_samples = X_train.shape[0]
for _ in range(m_samples // batch_size):
rand_ind = np.random.randint(0, m_samples - batch_size)
x_batch = X_train[rand_ind:rand_ind+batch_size].reshape(-1, 1) if len(X_train.shape) == 1 else X_train[rand_ind:rand_ind + batch_size]
y_batch = y_train[rand_ind:rand_ind+batch_size].reshape(-1, 1)
y_pred = self.predict(x_batch)
dw = (x_batch.T @ ((y_pred - y_batch)))
db = np.sum((y_pred - y_batch))
self.weights -= self.learning_rate * dw
self.bias -= self.learning_rate * db
def predict(self, X):
return np.dot(X, self.weights) + self.bias
# 示例使用代码:
X_train = np.random.rand(100, 2)
y_train = np.random.rand(100)
model_BGD = LinearRegression()
model_SGD = LinearRegression()
model_MBGD = LinearRegression()
model_BGD.fit_BGD(X_train.T, y_train.reshape(-1, 1))
model_SGD.fit_SGD(X_train.T, y_train.reshape(-1, 1))
model_MBGD.fit_MBGD(X_train.T, y_train.reshape(-1, 1), batch_size=32)
```
以上代码提供了三种梯度下降算法的实现,可以方便地进行模型训练。