0% found this document useful (0 votes)
13 views2 pages

Pipeline for Linear Regression Model

Uploaded by

ankit.malik
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views2 pages

Pipeline for Linear Regression Model

Uploaded by

ankit.malik
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

10/7/24, 6:18 PM about:blank

Data Analysis with Python


Cheat Sheet: Model Development
Process Description Code Example
1. 1
2. 2

Linear Regression Create a Linear Regression model object 1. from sklearn.linear_model import LinearRegression
2. lr = LinearRegression()

Copied!
1. 1
Train the Linear Regression model on decided 2. 2
data, separating Input and Output attributes. 3. 3
When there is single attribute in input, then it is 1. X = df[[‘attribute_1’, ‘attribute_2’, ...]]
Train Linear Regression model
simple linear regression. When there are 2. Y = df['target_attribute']
multiple attributes, it is multiple linear 3. [Link](X,Y)
regression.
Copied!
1. 1
Predict the output for a set of Input attribute 1. Y_hat = [Link](X)
Generate output predictions
values.
Copied!
1. 1
Identify the slope coefficient and intercept
2. 2
values of the linear regression model defined by
Identify the coefficient and 1. coeff = [Link]
intercept 2. intercept = lr.intercept_
Where m is the slope
coefficient and c is the intercept. Copied!
1. 1
2. 2
3. 3
This function will regress y on x (possibly as a
Residual Plot robust or polynomial regression) and then draw 1. import seaborn as sns
2. [Link](x=df[[‘attribute_1’]],
a scatterplot of the residuals. 3. y=df[[‘attribute_2’]])

Copied!
1. 1
2. 2
3. 3
This function can be used to plot the 1. import seaborn as sns
Distribution Plot
distribution of data w.r.t. a given attribute. 2. [Link](df['attribute_name'], hist=False)
3. # can include other parameters like color, label and so on.

Copied!
1. 1
2. 2
3. 3
4. 4
5. 5
6. 6
Available under the numpy package, for single 1. f = [Link](x, y, n)
Polynomial Regression
variable feature creation and model fitting. 2. #creates the polynomial features of order n
3. p = np.poly1d(f)
4. #p becomes the polynomial model used to generate the predicted output
5. Y_hat = p(x)
6. # Y_hat is the predicted output

Copied!
1. 1
2. 2
3. 3
Generate a new feature matrix consisting of all 4. 4
Multi-variate Polynomial polynomial combinations of the features with 1. from [Link] import PolynomialFeatures
Regression the degree less than or equal to the specified 2. Z = df[[‘attribute_1’,’attribute_2’,...]]
degree. 3. pr=PolynomialFeatures(degree=n)
4. Z_pr=pr.fit_transform(Z)

Copied!
Pipeline Data Pipelines simplify the steps of processing 1. 1
the data. We create the pipeline by creating a 2. 2
3. 3
list of tuples including the name of the model or 4. 4
estimator and its corresponding constructor. 5. 5
6. 6
7. 7
8. 8
9. 9

1. from [Link] import Pipeline


2. from [Link] import StandardScaler
3. Input=[('scale',StandardScaler()), ('polynomial',
4. PolynomialFeatures(include_bias=False)),
5. ('model',LinearRegression())]
6. pipe=Pipeline(Input)
7. Z = [Link](float)
8. [Link](Z,y)
9. ypipe=[Link](Z)

about:blank 1/2
10/7/24, 6:18 PM about:blank
Copied!
a.
1. 1
2. 2
3. 3
4. 4

1. X = df[[‘attribute_1’, ‘attribute_2’, ...]]


2. Y = df['target_attribute']
R^2, also known as the coefficient of 3. [Link](X,Y)
determination, is a measure to indicate how 4. R2_score = [Link](X,Y)
close the data is to the fitted regression line.
The value of the R-squared is the percentage of Copied!
variation of the response variable (y) that is
R^2 value
explained by a linear model.
a. For Linear Regression (single or multi b.
attribute) 1. 1
b. For Polynomial regression (single or multi 2. 2
attribute) 3. 3
4. 4

1. from [Link] import r2_score


2. f = [Link](x, y, n)
3. p = np.poly1d(f)
4. R2_score = r2_score(y, p(x))

Copied!
1. 1
2. 2
The Mean Squared Error measures the average
MSE value of the squares of errors, that is, the difference 1. from [Link] import mean_squared_error
2. mse = mean_squared_error(Y, Yhat)
between actual value and the estimated value.
Copied!

about:blank 2/2

You might also like