0% found this document useful (0 votes)
14 views

Aggialavura - Python Linear Regression Model

This document provides a cheat sheet for linear regression modeling in Python. It outlines the key steps, which include importing data and modeling libraries, preparing and visualizing the data, splitting the data into training and test sets, fitting a linear regression model to the training data, making predictions on the test data, and evaluating the model's performance using metrics like MAE, MSE, and RMSE. Code examples are provided for each step to demonstrate how to implement linear regression in Python.

Uploaded by

himtajay
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Aggialavura - Python Linear Regression Model

This document provides a cheat sheet for linear regression modeling in Python. It outlines the key steps, which include importing data and modeling libraries, preparing and visualizing the data, splitting the data into training and test sets, fitting a linear regression model to the training data, making predictions on the test data, and evaluating the model's performance using metrics like MAE, MSE, and RMSE. Code examples are provided for each step to demonstrate how to implement linear regression in Python.

Uploaded by

himtajay
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Python - Linear Regression Model Cheat Sheet

by DarioPittera (aggialavura) via cheatography.com/83764/cs/19917/

TO START TRAIN MODEL (cont)

# IMPORT DATA LIBRARIES lm.coef_ show coeffi​cients


import pandas as pd coeff_df = pd.DataFrame create coeff df
import numpy as np (lm.coef_,X.columns,columns=['Coeff'])*
# IMPORT VIS LIBRARIES
pd.Dat​aFrame: pd.Dat​aFr​ame​(da​ta=​None, index=​None, column​‐
import matplo​tli​b.p​yplot as plt
s=None, dtype=​None, copy=F​alse). data = values, index= name
import seaborn as sns index, columns= name column. This could be useful just to interpret
%matpl​otlib inline the coeffi​cient of the regres​sion.
# IMPORT MODELLING LIBRARIES
from sklearn.model_selection import MAKE PREDIC​TIONS
train_test_split
predic​tions = lm.pre​dic​t(X​_test) create predic​tions
from sklear​n.l​ine​ar_​model import Linear​Reg​ression
plt.sc​att​er(​y_t​est​,pr​edi​cti​ons)* plot predic​tions
from sklearn import metrics
sns.di​stp​lot​((y​_te​st-​pre​dic​tio​ns)​,bi​ns=50)* distplot of residuals

PRELIM​INARY OPERATIONS scatter: this graph show the difference between actual values and
the values predicted by the model we trained. It should resemble as
df = pd.rea​d_c​sv(​'da​ta.c​sv') read data
much as possible a diagonal line .
df.head() check head df
distplot: this graph shows the distri​butions of the residual errors, that
df.info() check info df is, the difference between the actual values minus the predicted
df.des​cribe() check stats df values; it should result in an as much as possible normal distri​bution.
df.columns check col names If not, maybe change model!

VISUALISE DATA EVALUATION METRICS

sns.pa​irp​lot(df) pairplot print(​'MAE:', metric​s.m​ean​_ab​sol​ute​_er​ror​(y_​test, predic​tions))

sns.di​stp​lot​(df​['Y']) distri​bution plot print(​'MSE:', metric​s.m​ean​_sq​uar​ed_​err​or(​y_test, predic​tions))

sns.he​atm​ap(​df.c​orr(), annot=​True) heatmap with values print('RMSE:', np.sqrt(metrics.mean_squared_error(y_test, predictions))

MAE is the easiest to unders​tand, because it's the average error.


TRAIN MODEL MSE is more popular than MAE, because MSE "​pun​ish​es" larger
CREATE X and y ------​---​------ errors, which tends to be useful in the real world.
RMSE is even more popular than MSE, because RMSE is interp​‐
X = df[['c​ol1​','​col​2',​etc.]] create df features
retable in the "​y" units.
y = df['col'] create df var to predict
 SPLIT DATASET ------​---​------
X_train, X_test, y_train, y_test = split df in train and test df
train_test_split(
X,
y,
test_size=0.3)
 FIT THE MODEL ------​---​------
lm = Linear​Reg​res​sion() instatiate model
lm.fit​(X_​train, y_train) train/fit the model
 SHOW RESULTS ------​---​------
lm.int​ercept_ show intercept

By DarioPittera (aggialavura) Not published yet. Sponsored by CrosswordCheats.com


Last updated 24th June, 2019. Learn to solve cryptic crosswords!
Page 1 of 1. http://crosswordcheats.com

cheatography.com/aggialavura/
www.dariopittera.com

You might also like