I'm trying to get the equation of a linear regression model created with sklearn. However I get strange results when I try to calculate the prediction with the coefficients from the model by hand. I guess I made a mistake somewhere, but I couldn't figure it out by myself...
Here's my code:
# Many data points in Pandas DataFrame "filtered_data"
predictors = ["Druckwinkel korrigiert [°]", "Druckwinkel sq.", "Drehzahl [1/min]"]
regressant = "Kraft [N]"
x = filtered_data[predictors].to_numpy()
y = filtered_data[regressant].to_numpy()
model = LinearRegression()
model.fit(x, y)
print("Intercept:", model.intercept_)
print("Coefficients:", model.coef_)
print("R²:", model.score(x, y))
This prints:
Intercept: 150070.5970260448
Coefficients: [-1.28305930e+04 2.73978667e+02 1.48116871e-01]
R²: 0.9578737003844259
If I do
model.predict(np.array([28, 28**2, 2768]).reshape(1, -1))
I get
array([6023.2553988])
which seems reasonable. But if I use the coefficients and intercept to calculate Y like this:
def load(contact_angle, shaft_speed):
return 150070.59702 - 12830.59299 * (contact_angle ** 2) + 273.97866 * contact_angle + 0.14811 * shaft_speed
load(28, 2768)
I get
-9901032.920822442
which is not at all what I expected...
Can anyone help?
I think you are predicting on [28, 28**2, 2768]
and your manual calculation is passing [28**2, 28, 2768]
.
To fix this:
def load(contact_angle, shaft_speed):
return 150070.59702 - 12830.59299 * contact_angle + 273.97866 * (contact_angle ** 2) + 0.14811 * shaft_speed
load(28, 2768)
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.