Nonlinear function modeling in Python

things to do

I will explain how to approximate and model observation values (set of x, y) with a specified function in Python.

Image diagram: I will do something like this image

Fitting in functions is the basis of modeling. If it's a linear approximation, I think you can usually use the linear regression package, Here, we will explain how to fit with any function you specify, including non-linear functions.

What to use

We use a module called "curve_fit" in Python's scipy package. More precisely, it is part of the scipy.optimize module.

First, load the package to be used this time.

import.py


##What to use for fitting
from scipy.optimize import curve_fit
import numpy as np

##What to use for illustration
import seaborn as sns
import matplotlib.pyplot as plt

numpy is used to represent exponential functions. If you are not familiar with the illustration method using seaborn, please click here.

Beautiful graph drawing with python http://qiita.com/hik0107/items/3dc541158fceb3156ee0

How to do-First try with a linear function

Let's actually see how to do function fitting First of all, I will try a linear approximation for arm leveling.

Create the observation data to be fitted as follows. Since it is linearly approximated, prepare a shape that is close to a straight line.

linear.py


list_linear_x = range(0,20,2)

array_error = np.random.normal(size=len(list_linear_x))

array_x = np.array(list_linear_x)
array_y = array_x + array_error ##Complete y=I am making bumpy data by adding an error term to the straight line of x

Let's take a look at the actually completed data.

linear.py


sns.pointplot(x=array_x, y=array_y, join=False)

image

Now let's fit this in the form of Y = ax + b. This is where curve_fit comes in.

fitting.py


##Define the function expression you want to fit as a function
def linear_fit(x, a, b):
    return a*x + b

param, cov = curve_fit(linear_fit, array_x, array_y)

Only this. The estimation results of parameters a and b are stored in list format in the first return value param. The contents of curve_fit are written as (function used for fitting, x for fitting, y for fitting). If you describe the 2nd and 3rd arguments in a list comprehension list, you can handle multiple variables.

Let's see the result of the fitting.

fitting.py


array_y_fit = param[0] * array_x + param[1]

sns.pointplot(x=array_x, y=array_y, join=False)
sns.pointplot(x=array_x, y=array_y_fit, markers="")

image

The fitting method is OLS (least squares error method).

Method-Try with a non-linear function

Next, let's try an approximation with a slightly more complicated nonlinear function. For example, consider a function such as f (x) = b * exp (x / (a + x)).

nonlinear.py


list_y = []

for num in array_x:
    list_y.append( param[1] * np.exp( num /(param[0] + num) ) + np.random.rand() )

array_y= np.array(list_y)
sns.pointplot(x=array_x, y=array_y, join=False)

image

I got the data like this. Somehow, a non-linear function that converges seems to fit better than a linear one.

Now let's fit this in the form f (x) = b * exp (x / (a + x)).

fitting.py


def nonlinear_fit(x,a,b):
    return  b * np.exp(x / (a+x)  )

param, cov = curve_fit(nonlinear_fit, array_x, array_y)

draw.py


list_y = []
for num in array_x:
    list_y.append( param[1] * np.exp( num /(param[0] + num) ))

sns.pointplot(x=array_x, y=array_y, join=False)
sns.pointplot(x=array_x, y=np.array(list_y), markers="")

image

It fits like that.

This article also

A rudimentary summary of data manipulation in Python Pandas http://qiita.com/hik0107/items/d991cc44c2d1778bb82e

Data analysis in Python Summary of sources to look at first for beginners http://qiita.com/hik0107/items/0bec82cc09d0e05d5357

If you are interested in data scientists, first look around here, a summary of literature and videos http://qiita.com/hik0107/items/ef5e044d2f47940ba712

Recommended Posts

Nonlinear function modeling in Python
Uplift modeling in Python
Create a function in Python
Use callback function in Python
ntile (decile) function in python
Draw implicit function in python
Immediate function in python (lie)
python function ①
[Python] function
python function ②
Implement R's power.prop.test function in python
Function argument type definition in python
Included notation in Python function arguments
Write AWS Lambda function in Python
Measure function execution time in Python
Function synthesis and application in Python
Quadtree in Python --2
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
python enumerate function
Meta-analysis in Python
Precautions when pickling a function in python
Unittest in python
Implemented in Python PRML Chapter 7 Nonlinear SVM
OR the List in Python (zip function)
Python> function> Closure
Discord in Python
[Python] Generator function
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Plink in Python
Constant in python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Python> function> Inner function
Csv in python
Disassemble in Python
Reflection in Python
Duality in function
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Python function decorator
Quad-tree in Python
Reflection in Python
Chemistry in Python
Hashable in python