# 机器学习造轮子之线性回归

• 构造数据
• 构造loss function(coss function)
• 分别对W和b计算梯度（也是对cost function分别对W和b求导）
• 计算Y_hat
• 多次迭代计算梯度，直接收敛或者迭代结束

``````import matplotlib.pyplot as plt
import numpy as np

def f(X):
w = np.array([1, 3, 2])
b = 10
return np.dot(X, w.T) + b

def cost(X, Y, w, b):
m = X.shape[0]
Z = np.dot(X, w) + b
Y_hat = Z.reshape(m, 1)
cost = np.sum(np.square(Y_hat - Y)) / (2 * m)
return cost

def gradient_descent(X, Y, W, b, learning_rate):
m = X.shape[0]
W = W - learning_rate * (1 / m) * X.T.dot((np.dot(X, W) + b - Y))
b = b - learning_rate * (1 / m) * np.sum(np.dot(X, W) + b - Y)
return W, b

def main():
# sample number
m = 5
# feature number
n = 3
total = m * n
# construct data
X = np.random.rand(total).reshape(m, n)
Y = f(X).reshape(m, 1)

#     X, Y = iris.data, iris.target.reshape(150, 1)
#     X = X[Y[:, 0] < 2]
#     Y = Y[Y[:, 0] < 2]
#     m = X.shape[0]
#     n = X.shape[1]

# define parameter
W = np.ones((n, 1), dtype=float).reshape(n, 1)
b = 0.0
# def forward pass++
learning_rate = 0.1
iter_num = 10000

i = 0
J = []
while i < iter_num:
i = i + 1
W, b = gradient_descent(X, Y, W, b, learning_rate)
j = cost(X, Y, W, b)
J.append(j)
print(W, b)
print(j)

plt.plot(J)
plt.show()

if __name__ == '__main__':
main()

``````

``````step: 4998 loss: 3.46349593719e-07
[[ 1.00286704]
[ 3.00463459]
[ 2.00173473]] 9.99528287088
step: 4999 loss: 3.45443124835e-07
[[ 1.00286329]
[ 3.00462853]
[ 2.00173246]] 9.99528904819
step: 5000 loss: 3.44539028368e-07
``````