'How to find y=0 using regression in sklearn?

I'm tasked with determining the distance of a throw. I have height and distance values in a csv, which I have imported into a dataframe and separated the x and y, but this csv doesn't cover the entire throw, so I need to predict the length using regression. However, I do not know how to predict y=0, because regression.intercept_ returns the y value for x=0, which I am not interested in. Here's my code, which I have built based on the examples provided by the course lecturer:

import sys
import time
import pandas
import numpy as np
from sklearn import linear_model
from sklearn.preprocessing import PolynomialFeatures
import matplotlib.pyplot as plt
#We must use the given libraries as is

inputfile='mittaus.csv'


data=pandas.read_csv(inputfile)

degree = 2

#Shaping data
X = np.array(data['x'])
y = np.array(data['y'])
X = X.reshape(len(data), 1)
y = y.reshape(len(data), 1)


#Regression
poly_reg = PolynomialFeatures(degree=degree)
X_poly = poly_reg.fit_transform(X) #I don't fully understand what we are doing here, and why are we using linear regression model with a polynomial function
lin_reg = linear_model.LinearRegression()
lin_reg.fit(X_poly, y)
y_pred = lin_reg.predict(X_poly)

#Plotting for diagnostics
plt.plot(X, y_pred)
plt.show

#Printing the answer
print('Estimated landing spot =',lin_reg.intercept_) # Gives the wrong answer, obviously. In this context this answer would be the height of the thrower.


Solution 1:[1]

I don't know what the context of the throw is, but it is common physics exercise to prove by Newton's second law that the object you throw follows a quadratic trajectory. Thus, you can't immediately perform linear regression, and that's why you use PolynomialFeatures(degree = 2): you want to approximate y as a linear function of [1, x, x²]: y = a x² + bx + c for example. Then, you just have to solve second degree equation a x² + b x + c = 0, where [a, b] = lin_reg.coef_ and c = lin_reg.intercept_ (- b +/- sqrt(b² - 4ac))/(2a)).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1