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
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt


data = np.loadtxt('ex1data1.txt', delimiter=',')
x = data[:, 0]
y = data[:, 1]
m = y.size
y = y.reshape(-1, 1)

plt.figure(0)
fg1 = plt.scatter(x, y, marker='x', c='r')
plt.xlabel('profit in $10,000s')
plt.ylabel('Population of City in 10,000s')


# 正规方程发
# x = np.column_stack((np.ones([m, 1]), x))
# theta = np.linalg.inv(x.T@x)@x.T@y
# print(theta)


# 代价函数
def computerCost(X, Y, Theta):
inner = X @ Theta.T - Y
J = np.sum(inner * inner) / (2 * Y.size)
return J


# 梯度下降法
x = np.column_stack((np.ones([m, 1]), x))
theta = np.zeros([1, 2])
alpha = 0.01
epoch = 1000


# 梯度下降函数
def gradientDescent(X, Y, Theta, Alpha=0.01, Epoch=1000):
Cost = np.zeros([Epoch, 1])
for i in range(Epoch):
inner = X @ Theta.T - Y
for j in range(Theta.size):
Xg = X[:, j].reshape(-1, 1)
inner = inner * Xg
Theta[0, j] = Theta[0, j] - (Alpha/m) * np.sum(inner)
Cost[i] = computerCost(X, Y, Theta)
return Cost, Theta


cost, theta = gradientDescent(x, y, theta, alpha, epoch)
plt.figure(0)
x2 = np.linspace(x[:, 1].min(), x[:, 1].max(), 100)
y2 = theta[0, 0] + theta[0, 1] * x2
fg2 = plt.plot(x2, y2)
plt.show()