1 Upvote

Linear Regression with Sklearn

Python
Supervised Learning

Here we initialise and train a Linear Regression model before using the model to make predictions. Finally we find the intercept and cofficients of the model along with analysing the models performance on test data using mean squared error and mean absolute error metrics.

#Import Library
from sklearn.linear_model import LinearRegression

#Initialise & Fit Model
model = LinearRegression()
model.fit(X_train, y_train)

#Use Model to Make Predictions
y_pred = model.predict(X_test)

#Get Intercept & Coefficients
print(model.intercept_)
coef = pd.DataFrame(model.coef_, X_train.columns, columns=['Coef'])

#Get MSE & MAE
from sklearn.metrics import mean_squared_error, mean_absolute_error
print('MSE:',mean_squared_error(y_test,y_pred))
print('MAE:',mean_absolute_error(y_test,y_pred))

By analyseup - Last Updated Jan. 10, 2022, 11:27 p.m.

Did you find this snippet useful?

Sign up to bookmark this in your snippet library

COMMENTS
RELATED SNIPPETS
Top Contributors
75