Beijing Institute of Technology | Ming-Jian Li
The following Python code is a companion code for the course on Artificial Intelligence and Simulation Science. It functions to fit a line using linear regression based on a given dataset and make predictions, with parameter optimization using the least square method.
x1import numpy as np
2import matplotlib.pyplot as plt
3
4# prepare data
5data = np.array([[3.2,31],[5.3,68],[6.1,62],[4.7,71],
6 [5.9,87],[5.5,78],[5.2,79],[3.9,59],
7 [4.8,75],[5.2,71],[4.5,55],[5.4,82],
8 [4.4,62],[5.8,75],[5.6,81],[4.8,60],
9 [4.4,82],[6.0,97],[4.5, 48],[3.8,56],
10 [6.6,83],[6.5,118],[4.7,57],[4.1,51],
11 [5.1,75],[5.9,74],[5.7,95],[6.3,95],
12 [4.6,79],[5.0,83]])
13
14# get data
15x = data[:, 0]
16y = data[:, 1]
17
18# draw scatter
19plt.scatter(x, y)
20plt.show()
21
22# loss function
23def compute_cost(w, b, points):
24 total_cost = 0
25 M = len(points)
26
27 # average of square loss
28 for i in range(M):
29 x = points[i, 0]
30 y = points[i, 1]
31 total_cost += (y - w * x - b) ** 2
32
33 return total_cost / M
34
35# average function
36def average(data):
37 sum = 0
38 num = len(data)
39 for i in range(num):
40 sum += data[i]
41 return sum / num
42
43# fitting fucntion
44def fit(points):
45 M = len(points)
46 x_bar = average(points[:, 0])
47
48 sum_yx = 0
49 sum_x2 = 0
50 sum_delta = 0
51
52 for i in range(M):
53 x = points[i, 0]
54 y = points[i, 1]
55 sum_yx += y * (x - x_bar)
56 sum_x2 += x ** 2
57 # get w
58 w = sum_yx / (sum_x2 - M * (x_bar ** 2))
59
60 for i in range(M):
61 x = points[i, 0]
62 y = points[i, 1]
63 sum_delta += (y - w * x)
64 b = sum_delta / M
65
66 return w, b
67
68# loss
69w, b = fit(data)
70
71print("w is: ", w)
72print("b is: ", b)
73
74cost = compute_cost(w, b, data)
75
76print("cost is: ", cost)
77
78# fit
79plt.scatter(x, y)
80# predict
81pred_y = w * x + b
82
83plt.plot(x, pred_y, c='r')
84plt.show()
The fitting results are as follows.