Linear regression with least square methodBeijing 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 np2import matplotlib.pyplot as plt3
4# prepare data5data = 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 data15x = data[:, 0]16y = data[:, 1]17 18# draw scatter19plt.scatter(x, y)20plt.show()21
22# loss function23def compute_cost(w, b, points):24 total_cost = 025 M = len(points)26 27 # average of square loss28 for i in range(M):29 x = points[i, 0]30 y = points[i, 1]31 total_cost += (y - w * x - b) ** 232 33 return total_cost / M34 35# average function36def average(data):37 sum = 038 num = len(data)39 for i in range(num):40 sum += data[i]41 return sum / num42 43# fitting fucntion44def fit(points):45 M = len(points)46 x_bar = average(points[:, 0])47 48 sum_yx = 049 sum_x2 = 050 sum_delta = 051 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 ** 257 # 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 / M65 66 return w, b67 68# loss69w, 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# fit79plt.scatter(x, y)80# predict81pred_y = w * x + b82 83plt.plot(x, pred_y, c='r')84plt.show()The fitting results are as follows.
