How to find the coefficient of the trendline that passes through the vertices in Python

Overview

$ M $ order curve $ y = a_mx ^ m + a_ {m-1} x ^ {passing $ n $ points $ (x_1, y_1), \ cdots, (x_n, y_n) $ on the 2D plane I want to find the coefficient $ a_0, \ dots, a_m $ of m-1} + \ cdots + a_1x + a_0 $. If $ n \ le m + 1 $ and the values of $ x_1, \ cdots, x_n $ are all different, there is a curve that passes through all the points, but if not, the coefficient that minimizes the error between the points and the curve. Need to take. At that time, it seems that scipy's fmin method can be used. However, note that the fmin function may not be the optimal solution because it finds the local optimal solution of the objective function.

Program example of cubic curve

import numpy as np
from scipy.optimize import fmin

def f(a, x):
    #Cubic curve
    return a[0] + a[1]*x + a[2]*x**2 + a[3]*x**3

def obj_func(a,x,y):
    #The objective function is the sum of the squared errors of the vertices and the curve.
    return sum((f(a,x) - y)**2)

#Give 5 points
x = np.array([ -8.,  -3.,  2.,   6.,   9.])
y = np.array([ 30., -22., 15., -17., -25.])

#Initial value of coefficient
a= np.array([ 0., 0., 0., 0.])

#Find the optimum coefficient
opt = fmin(obj_func, a, args=(x,y))

print(opt)
# [-8.20775435  3.2028276   0.2150416  -0.09395258]

figout.png

Recommended Posts

How to find the coefficient of the trendline that passes through the vertices in Python
How to get the number of digits in Python
How to find the optimal number of clusters in k-means
How to find the first element that matches your criteria in a Python list
How to determine the existence of a selenium element in Python
How to know the internal structure of an object in Python
How to check the memory size of a variable in Python
How to judge that the cross key is input in Python3
[Python] Programming to find the number of a in a character string that repeats a specified number of times.
How to check the memory size of a dictionary in Python
How to use the C library in Python
How to find the area of the Voronoi diagram
Summary of how to import files in Python 3
Summary of how to use MNIST in Python
Find the divisor of the value entered in python
How to get the files in the [Python] folder
Find the solution of the nth-order equation in python
[Circuit x Python] How to find the transfer function of a circuit using Lcapy
How to find out what kind of files are stored in S3 in Python
How to get a list of files in the same directory with python
How to retrieve the nth largest value in Python
How to get the variable name itself in python
How to know the current directory in Python in Blender
Find the part that is 575 from Wikipedia in Python
[Python] Summary of how to specify the color of the figure
How to use the model learned in Lobe in Python
Modules that may go through the shell in Python
[Python] How to output the list values in order
To do the equivalent of Ruby's ObjectSpace._id2ref in Python
Find the difference in Python
How to identify the element with the smallest number of characters in a Python list?
[Django] Let's try to clarify the part of Django that was somehow through in the test
How to check in Python if one of the elements of a list is in another list
I used Python to find out about the role choices of the 51 "Yachts" in the world.
How to develop in Python
How to count the number of occurrences of each element in the list in Python with weight
I tried to find the entropy of the image with python
[python] How to check if the Key exists in the dictionary
Find out the apparent width of a string in python
How to debug the Python standard library in Visual Studio
Processing of python3 that seems to be usable in paiza
Python --Find out number of groups in the regex expression
Consideration for Python decorators of the type that passes variables
How to develop in a virtual environment of Python [Memo]
Comparison of how to use higher-order functions in Python 2 and 3
How to get the last (last) value in a list in Python
How to get a list of built-in exceptions in python
How to find the scaling factor of a biorthogonal wavelet
Find the eigenvalues of a real symmetric matrix in Python
[Super easy! ] How to display the contents of dictionaries and lists including Japanese in Python
How to quickly count the frequency of appearance of characters from a character string in Python?
How to check if the contents of the dictionary are the same in Python by hash value
How to find the cumulative sum / sum for each group using DataFrame in Spark [Python version]
How to pass the execution result of a shell command in a list in Python (non-blocking version)
[Python] A program to find the number of apples and oranges that can be harvested
How to calculate the autocorrelation coefficient
[Python] How to do PCA in Python
How to collect images in Python
How to use SQLite in Python
In the python command python points to python3.8
[Python] How to import the library