Debapratim Chakraborty
Debapratim Chakraborty

Reputation: 417

How can I predict X value from a given Y value from a polynomial fitted Linear Regression model?

After having fit a simple Linear Regression model, I used this formula : "y=mx+c" to find the 'x' value for a given 'y' value. Clearly, having fit the model, I had gotten the values of 'm' and 'c'.

model = LinearRegression(fit_intercept=True)
model.fit(X,Y)
print('intercept:', model.intercept_)
print('slope:', model.coef_)
intercept: 1.133562363647583
slope: [-0.00075101]
#finding 'x' val for y=1 : x=(y-c)/m
x=(1-model.intercept_)/model.coef_[0] 

Now, I decided that Linear wasn't doing a good job, and saw that 3rd degree polynomial fit gave best results (not including the graphs here). Now how do I predict my 'x' value for a given 'y' value from this code snippet below :

from sklearn.preprocessing import PolynomialFeatures
poly = PolynomialFeatures(degree = 3) 
X_poly = poly.fit_transform(X) 
  
poly.fit(X_poly, Y) 
model2 = LinearRegression(fit_intercept=True) 
model2.fit(X_poly, Y)

print('intercept:', model2.intercept_)
print('slope:', model2.coef_)
intercept: 1.461870542630406
slope: [ 0.00000000e+00 -1.12408245e-02  9.69531205e-05 -2.72315461e-07]

Upvotes: 0

Views: 3767

Answers (1)

Girish Hegde
Girish Hegde

Reputation: 1515

Try

p = [slope, intercept - y]

x = numpy.roots(p)

Upvotes: 1

Related Questions