How to simplify restricted polynomial fit in python

Easy polynomial fit

If you want to fit data with a polynomial, but you want certain data points to pass through, here's an easy way to do it.

Code overview

When using numpy's polyfit, you can specify the weight with the option w, so increase the weight of the point you want to fix. Here is an example where the data does not pass through the origin, but you want to force it through the origin when fitting. Data is added to the original data x, y with te, numpy.append, the weight of that point is set to 1e3, and the others are set to 1.

Sample code

qiita_polyfit_limited.py



#!/usr/bin/env python

import numpy as np
import matplotlib.pyplot as plt

x = np.array([-10, -8,-6,-4,-2,0,2,4,6,8,10])
y = np.array([-15,-10,-3,-2,-1,3,1,3,4,8,12])
weight = np.ones(len(x))
npoly=4

x_add = np.append(x,0)
y_add = np.append(y,0)
weight_add = np.append(weight,1e3)

coef=np.polyfit(x, y, npoly, w=weight)
yfit = np.poly1d(coef)(x)         

coef_add=np.polyfit(x_add, y_add, npoly, w=weight_add)
yfit_add = np.poly1d(coef_add)(x)         

F = plt.figure(figsize=(7,7))
ax = plt.subplot(1,1,1)

plt.errorbar(x,y,fmt="o",label="data",alpha=0.8,ms=3)
plt.errorbar(x,yfit,fmt="--",label="fit (no limit), order = "  +str(npoly),alpha=0.8,ms=3)
plt.errorbar(x,yfit_add,fmt="--",label="fit (fixed at 0,0), order = "  +str(npoly),alpha=0.8,ms=3)

plt.grid(linestyle='dotted',alpha=0.5)
plt.legend(numpoints=1, frameon=False, loc="best") 

outfile = "qiita_polyfitlimited.png "
plt.savefig(outfile)

Fit result

If you do not limit anything, it will not pass through the origin, but if it is restricted, you can see that it has passed through the origin.

qiita_polyfitlimited.png

Since it is not a strict conditional fit, a small value remains, not zero, at the origin. If you want to be exactly 0 at the origin, you have to write the restricted fit function yourself. In that case, How to do fitting with parameter restrictions in python Please refer to.

Recommended Posts

How to simplify restricted polynomial fit in python
How to develop in Python
[Python] How to do PCA in Python
How to collect images in Python
How to use SQLite in Python
How to use Mysql in python
How to wrap C in Python
How to use ChemSpider in Python
How to use PubChem in Python
How to handle Japanese in Python
[Introduction to Python] How to use class in Python?
How to access environment variables in Python
How to dynamically define variables in Python
How to do R chartr () in Python
[Itertools.permutations] How to put permutations in Python
How to work with BigQuery in Python
How to get a stacktrace in python
How to display multiplication table in python
How to extract polygon area in Python
How to check opencv version in python
How to switch python versions in cloud9
How to adjust image contrast in Python
How to use __slots__ in Python class
How to dynamically zero pad in Python
How to use regular expressions in Python
How to display Hello world in python
How to use is and == in Python
How to write Ruby to_s in Python
How to use the C library in Python
How to receive command line arguments in Python
[REAPER] How to play with Reascript in Python
How to clear tuples in a list (Python)
How to install Python
How to generate permutations in Python and C ++
How to embed a variable in a python string
How to implement Discord Slash Command in Python
How to use Python Image Library in python3 series
How to implement shared memory in Python (mmap.mmap)
How to create a JSON file in Python
How to install python
Summary of how to use MNIST in Python
How to specify TLS version in python requests
How to notify a Discord channel in Python
How to get the files in the [Python] folder
How to use tkinter with python in pyenv
How to run Leap Motion in non-Apple Python
[Python] How to draw a histogram in Matplotlib
How to output "Ketsumaimo" as standard output in Python
How to handle datetime type in python sqlite3
How to make Python Interpreter changes in Pycharm
How to plot autocorrelation and partial autocorrelation in python
How to remove duplicate elements in Python3 list
How to retrieve the nth largest value in Python
[For beginners] How to use say command in python!
How to convert / restore a string with [] in python
How to get the variable name itself in python
How to get the number of digits in Python
How to write string concatenation in multiple lines in Python
How to do hash calculation with salt in Python
How to measure processing time in Python or Java
How to know the current directory in Python in Blender