1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
| import numpy as np import matplotlib.pyplot as plt
data = np.loadtxt('data.csv', delimiter=',') X = data[:, :-1] y = data[:, -1]
plt.scatter(X, y, marker='.') plt.show()
data = np.hstack((np.ones((data.shape[0], 1)), data)) X_train = data[:, :-1] y_train = data[:, -1].reshape((-1, 1))
def hypothesis(X, theta): """ 进行预测 """ return np.dot(X, theta)
def gradient(X, y, theta): """ 计算梯度 """ h = hypothesis(X, theta) grad = np.dot(X.transpose(), (h - y)) return grad
def cost(X, y, theta): """ 计算损失函数值 """ h = hypothesis(X, theta) J = 1 / 2 * np.dot((h - y).transpose(), (h - y)) return J[0]
def gradient_descent(X, y, learning_rate=0.001, batch_size=25): """ 梯度下降算法 """ history_cost = [] theta = np.zeros((X.shape[1], 1)) n_points = X.shape[0]
for _ in range(batch_size): batch = np.random.choice(range(n_points), batch_size)
X_batch = X[batch, :] y_batch = y[batch]
theta = theta - learning_rate * gradient(X_batch, y_batch, theta) history_cost.append(cost(X_batch, y_batch, theta))
return theta, history_cost
theta, error_list = gradient_descent(X_train, y_train, batch_size=1000) print("Bias = ", theta[0]) print("Coefficients = ", theta[1:])
plt.plot(error_list) plt.xlabel("Number of iterations") plt.ylabel("Cost") plt.show()
y_pred = hypothesis(X_train, theta)
plt.scatter(X, y, marker='.') plt.plot(X_train[:, 1], y_pred, color='orange') plt.show()
|