-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinear_Least_Square.py
More file actions
52 lines (38 loc) · 1.25 KB
/
Linear_Least_Square.py
File metadata and controls
52 lines (38 loc) · 1.25 KB
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
import numpy as np
from scipy.optimize import curve_fit
import matplotlib.pyplot as plt
def linear_ls(x_list, y_list, m):
sigma_x = sum(x_list)
sigma_y = sum(y_list)
sigma_x2 = sum(np.square(x_list))
xy = [x_list[i] * y_list[i] for i in range(len(x_list))]
sigma_xy = sum(xy)
A = np.array([[m, sigma_x], [sigma_x, sigma_x2]])
b = np.array([sigma_y, sigma_xy])
sol = list(np.linalg.solve(A, b))
return sol
# Define the linear model
def linear_model(x, a, b):
return a * x + b
m = int(input('How many points are there : '))
x_list = [0]*m
x_list = list(
map(float, input("Enter the x-coordinates of points : ").split(" ")))
y_list = [0]*m
y_list = list(
map(float, input('Enter the y-coordinates of points : ').split(" ")))
# Get the value of constants
output = linear_ls(x_list, y_list, m)
print(output)
x = np.array(x_list)
y = np.array(y_list)
# Fit the linear model
# The resulting coefficients of the linear model are stored in 'popt_linear',
# and the covariance matrix of the fit is stored in 'pcov_linear'.
popt_linear, pcov_linear = curve_fit(linear_model, x, y)
y_fit_linear = linear_model(x, *popt_linear)
# Plot the results
plt.scatter(x, y)
plt.plot(x, y_fit_linear, label='Linear')
plt.legend()
plt.show()