简体   繁体   中英

given data samples, deducing approximate formula by programming?

Given a sample formula 'y=2x+1 ' we can get y when we know x with a defined ' function ', for example in python

def y(x):   return 2*x + 1

x :   1    2    3     4     5     ...
y :   3    5    7     9     11    ...

Can I do it reversed? Given data samples and get the approximate formula?

x :   1.001    2.12    3.1     4.001     5.021     ...
y :   3.002    5.23    7.2     9.002     11.32    . ..

Of course, it is easily to draw all points by coordinates and link those points to get approximate formula then guess the result when people to solve this, but how do it by programming?

Any keywords can search or libs can use?

Thank you.

=====

Many thanks for everyone. After searching a bit with information you shared, it is a BIGG...G area without a simple solution -_-!.

Technically, I realized what I need should be " polynomial fitting " given the difference of Polynomial regression/ Polynomial interpolation/ polynomial approximation ......

I will give the credit to Gerard, and again, thanks all for your help.

To make sure I understood your question, you want to retrieve the equation of the line that fits your data, right?

In case this is your question, you can use polyfit from the package numpy .

import numpy as np
x = [1.001, 2.12, 3.1, 4.001, 5.021]
y = [3.002, 5.23, 7.2, 9.002, 11.32]
np.polyfit(x, y, 1)

The returned values are:

array([ 2.05658156,  0.88110544])

This means that the equation of the line is:

y = 2.05658156x + 0.88110544

Here i would say the problem looks more like polynomial interpolation In python you have many libs dealing with that, for example high level scipy.interpolate or you can do it "by hand" like these examples

In any case, you will need some maths !

Probably you need NumPy and SciPy packages. For example, see Linear Regression docs for SciPy: http://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.stats.linregress.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM