Get the inverse function of a polyfit in numpy

2024/10/8 8:27:07

I have fit a second order polynomial to a number of x/y points in the following way:

poly  = np.polyfit(x, y, 2)

How can I invert this function in python, to get the two x-values corresponding to a specific y-value?

Answer

Here's an example that shows how you can combine your poly with my answer at Inverse function of numpy.polyval().

First some data:

In [44]: x
Out[44]: array([0, 1, 2, 3, 4, 5, 6, 7])In [45]: y
Out[45]: array([ 9,  4,  0, -1, -1,  4,  8, 16])

Fit a polynomial to the data:

In [46]: poly = np.polyfit(x, y, 2)

Find where the polynomial has the value y0

In [47]: y0 = 4

To do that, create a poly1d object:

In [48]: p = np.poly1d(poly)

And find the roots of p - y0:

In [49]: (p - y0).roots
Out[49]: array([ 5.21787721,  0.90644711])

Check:

In [54]: x0 = (p - y0).rootsIn [55]: p(x0)
Out[55]: array([ 4.,  4.])
https://en.xdnf.cn/q/70145.html

Related Q&A

Installing an old version of scikit-learn

Problem StatmentIm trying to run some old python code that requires scikit-learn 18.0 but the current version I have installed is 0.22 and so Im getting a warning/invalid data when I run the code.What …

remove characters from pandas column

Im trying to simply remove the ( and ) from the beginning and end of the pandas column series. This is my best guess so far but it just returns empty strings with () intact. postings[location].replace(…

numerically stable inverse of a 2x2 matrix

In a numerical solver I am working on in C, I need to invert a 2x2 matrix and it then gets multiplied on the right side by another matrix:C = B . inv(A)I have been using the following definition of an …

Type annotating class variable: in init or body?

Lets consider the two following syntax variations:class Foo:x: intdef __init__(self, an_int: int):self.x = an_intAndclass Foo:def __init__(self, an_int: int):self.x = an_intApparently the following cod…

decoding shift-jis: illegal multibyte sequence

Im trying to decode a shift-jis encoded string, like this:string.decode(shift-jis).encode(utf-8)to be able to view it in my program.When I come across 2 shift-jis characters, in hex "0x87 0x54&quo…

Add columns in pandas dataframe dynamically

I have following code to load dataframe import pandas as pdufo = pd.read_csv(csv_path) print(ufo.loc[[0,1,2] , :])which gives following output, see the structure of the csvCity Colors Reported Shape Re…

How do you add input from user into list in Python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 9 years ago.Improve…

How to suppress matplotlib inline for a single cell in Jupyter Notebooks/Lab?

I was looking at matplotlib python inline on/off and this kind of solves the problem but when I do plt.ion() all of the Figures pop up (100s of figures). I want to keep them suppressed in a single cel…

Using django-filer, can I chose the folder that images go into, from Unsorted Uploads

Im using django-filer for the first time, and it looks great, and work pretty well.But all my images are being uploaded to the Unsorted Uploads folder, and I cant figure out a way to put them in a spec…

how to use distutils to create executable .zip file?

Python 2.6 and beyond has the ability to directly execute a .zip file if the zip file contains a __main__.py file at the top of the zip archive. Im wanting to leverage this feature to provide preview r…