Find the coefficients of the least squares polynomial

Overview

Least squares

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.

About the program

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.

program

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)

Execution result

Find the coefficient a of the first-order polynomial
m = 1
S =
[[17900   250]
 [  250     5]]

T =
[[2977.]
 [  55.]]

a =
[[0.04203704]
 [8.89814815]]

Find the coefficient a of the quadratic polynomial
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]]

Summary

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 useround or format` to make the values easier to see. That's right.

At the end

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

Find the coefficients of the least squares polynomial
Find the definition of the value of errno
Migemo version of the: find command,: mfind
How to find the area of the Voronoi diagram
I tried the least squares method in Python
Find the number of days in a month
Find the divisor of the value entered in python
Approximation by the least squares method of a circle with two fixed points
Find the solution of the nth-order equation in python
Find out the day of the week with datetime
Find the geometric mean of n! Using Python
Projecet Euler 12 Find the number of divisors without division.
Find the sum of unique values with pandas crosstab
Find out the location of Python class definition files.
Summarized the types of sum of squares for analysis of variance
Find out the location of packages installed with pip
Calculation of homography matrix by least squares method (DLT method)
The beginning of cif2cell
Find the maximum Python
The meaning of self
the zen of Python
The story of sys.path.append ()
Revenge of the Types: Revenge of types
I tried to find the entropy of the image with python
How to find the optimal number of clusters in k-means
Maya | Find out the number of polygons in the selected object
Find out the apparent width of a string in python
I tried to find the average of the sequence with TensorFlow
Inherit the standard library to find the average value of Queue
Python --Find out number of groups in the regex expression
Find the index of the maximum value (minimum value) of a multidimensional array
How to find the scaling factor of a biorthogonal wavelet
Find the diameter of the graph by breadth-first search (Python memory)
Find the average / standard deviation of the brightness values in the image
Find the eigenvalues of a real symmetric matrix in Python