The least squares method is a method of finding a relational expression by minimizing the sum of the squares of the errors in the processing of measured values with errors.
The program created this time is a process that lists the data x`` y
prepared in advance, finds the matrix S`` T
from the data, and derives the coefficient ʻa. Also, the degree of the polynomial you want to find is set as
m. There may already be a library that can easily find the least squares method, but this time we will not use such a library, only the array manipulation library
numpy`, and change the amount of data. We have created a flexible process.
LeastSquares.py
# -*- coding: utf-8 -*-
#!/usr/bin/env python3
import numpy as np
X_data = [10, 20, 50, 70, 100]
Y_data = [9.3, 9.8, 10.9, 11.9, 13.1]
m = 1
def ST_list(power):
S, T = 0, 0
for i, j in zip(X_data,Y_data):
S += i**power
T += i**power * j
return S, T
def A_matrix(n_size, len):
A = []
for k in range(n_size):
A.append(S_list[k+len-m*2-1:k+len-m*2-1+n_size])
return A
S_list = []
T_list = []
for n in range(len(X_data)):
S , T = ST_list(n)
S_list.insert(0, S)
T_list.insert(0, T)
A = np.array(A_matrix(m+1, len(S_list)))
b = np.array(T_list[-1*m-1::]).reshape(m+1, 1)
Ainv = np.linalg.inv(A)
x = np.dot(Ainv, b)
print("S ="); print(A); print()
print("T ="); print(b); print()
print("a ="); print(x)
m = 1
S =
[[17900 250]
[ 250 5]]
T =
[[2977.]
[ 55.]]
a =
[[0.04203704]
[8.89814815]]
m = 2
S =
[[130430000 1477000 17900]
[ 1477000 17900 250]
[ 17900 250 5]]
T =
[[2.2141e+05]
[2.9770e+03]
[5.5000e+01]]
a =
[[1.22729504e-05]
[4.07142857e-02]
[8.92034855e+00]]
In this data, the coefficients ʻafor the first-order polynomial and the second-order polynomial were obtained. If you increase the amount of data, you can also perform cubic and quaternary orders, but since they are affected by floating point numbers like quadratic polynomials, you need to use
round or
format` to make the values easier to see. That's right.
That's all for this article. Sorry for the hard-to-see program. I feel like there is a better way, especially when it comes to array manipulation. If you have any advice, please leave it in the comments section. Thank you for staying with us until the end.
Recommended Posts