Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there a Python function for finding a function for the fit curve of a set of points?

Say I have a set of points. I know about a lot of options for fitting a curve for those points, such as numpy.polyfit() and scipy.fit_curve.

How can I get a function for the fit curve? In other words, how can I get the y value for an x value based on those set of points?

Sorry for awkward wording, I don’t know how to phrase this.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

In numpy, you can pass numpy.polyfit to numpy.poly1d which then returns a function that you can pass x-values to.

Example:

>>> import numpy as np
>>> points = np.array([(1, 1), (2, 4), (3, 1), (9, 3)])
>>> x = points[:,0]
>>> y = points[:,1]
>>> a = np.polyfit(x, y, 3)
>>> f = np.poly1d(a)
>>> f(1)
1.0000000000000675
>>> f(3)
1.0000000000000284
>>> f(6)
-17.928571428571125
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading