Reputation: 282
I'm facing a problem with different linear models from scikit-learn. There is my code
from sklearn.linear_model import LinearRegression
reg = LinearRegression().fit(X_train, y_train)
y_pred = reg.predict(X_train).reshape(-1)
print(f"R2 on train set:{reg.score(X_train, y_train)}")
print(f"R2 on test set:{reg.score(X_test, y_test)}")
print(f"MSE on train set:{mean_squared_error(y_train, y_pred)}")
print(f"MSE on test set:{mean_squared_error(y_test, reg.predict(X_test))}")
output:
>R2 on train set:0.5810258473777401
>R2 on test set:0.5908396388537969
>MSE on train set:0.023576848498732563
>MSE on test set:0.02378699441936436
Model is fitted, now I want to get the slope coefficient and the intercept from my model:
A, B = reg.coef_[0], reg.intercept_[0]
A, B
output:
>(array([ 0.14373081, -1.8211677 , 1.81493948, 1.39041689, -0.14027746]),
> 0.060286931992710735)
Since I used 5 features to fit the model I also have 5 slope coefficients, ok.
But when I try to visualize y_true, y_pred and the regression (ax +b) it's looks wrong for the regression of the second feature (total rooms). Since it has -1.81 as coef slope it's look logic but if the predictions of the model look fine, how it's possible to have this regression looks that bad, it make no sense right ?
I think that the return of reg.coef_
is not in the same order as the features the model is fitted with. But as far as I have see, it should be the same order, so idk.
There is also this part of code, that plot the regression just in case
sns.lineplot(x=X[:, i], y=(a[i]*X[:, i])+b, label="regression", color=c3, alpha=1, ci=None, ax=axes[i])
Any idea ? I keep in mind that there may be no problem at all but visually it hurts a bit
Upvotes: 0
Views: 379
Reputation: 19
y_pred is a quantifier for listreg. We introduce N as an other variable which cannot be quantified or consecutive. N=ax/k-b of a scatter plot. This helps to find the total shape or size of the bedroom; b, b=l. 5 is right. 5 is independent of the regression. I mean of an independent variable.
Upvotes: 2