10 Minutes Regression Using Python
By: Abideen Opeyemi Bello Bideen
Tags: Python Programming, Data Science, Linear Regression, Deep Learning, Machine Learning

Simple and Multilinear Regression

Photo by data-driven investor

1. Simple linear regression(SLR)

Simple linear regression(SLR) refers to one independent variable to make a prediction. Simple linear regression(SLR) helps us understand the relationship between the independent variable(X) and the dependent variable(y).

X — Independent variable(one or more features)

Y —dependent variable(one feature)

Let's import the data and assign our X and Y variable:

# Import libraries

import pandas as pd

import numpy as np

import matplotlib.pyplot as plt

path = '

df = pd.read_csv(path) # path of datapath = ' https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/DA0101EN/automobileEDA.csv' df = pd.read_csv(path)

The result of Linear Regression is a linear function that predicts the response (dependent) variable as a function of the predictor (independent variable).

Linear function: 𝑌ℎ𝑎𝑡=𝑎+𝑏𝑋

a— Intercept(is the value of Y when X is 0)

b —Slope(is the value with which Y changes when X increases by 1 unit)

# Lets load the modules for linear regression lm = LinearRegression()

lm #Output :

LinearRegression(copy_X=True, fit_intercept=True, n_jobs=None, normalize=False)

How could Highway-mpg help us predict car price? Let’s look at how highway-mpg can help us predict the car price. Using simple linear regression, we will create a linear function with “highway-mpg” as the predictor variable and the “price” as the response variable.

X = df[['highway-mpg']] # Independent varaible

Y = df['price'] # dependent varaible

Then we Fit the linear model using highway-mpg and predict an output.

# fit model

lm.fit(X,Y) # Prediction

Yhat = lm.predict(X)

What is the value of the intercept(a) and slope(b)

# the intercept

a = lm.intercept_

# Output : 38423.305858157415

# the slope

b = lm.coef_

#Output: -821.73337832

So the final estimate for the linear model using the linear function above:

𝑌ℎ𝑎𝑡=𝑎+𝑏𝑋

is

price = 38423.31–821.73 x highway-mpg

2. Multilinear regression(MLR)

Multilinear regression(MLR) is used to explain the relationship between one or more continuous target variable and two or more predictors. Multiple Linear Regression(MLR) can use more variables in our model to predict car price.

it is very similar to Simple Linear Regression, but this method is used to explain the relationship between one continuous response (dependent) variable and two or more predictor (independent) variables. Most of the real-world regression models involve multiple predictors.

I will illustrate the structure by using four predictor variables, but these results can generalize to any integer:

Y: Response Variable, a: intercept

X1: Predictor Variable, b1: coefficient of Variable 1

X2: Predictor Variable, b2: coefficient of Variable 2

X3: Predictor Variable, b3: coefficient of Variable 3

X4: Predictor Variable, b4: coefficient of Variable 4

The MLR equation is given by:

𝑌ℎ𝑎𝑡=𝑎+𝑏1𝑋1+𝑏2𝑋2+𝑏3𝑋3+𝑏4𝑋4

From the dataset, we would see that other good predictors of price could be:

Horsepower Curb-Weight Engine-size Highway-mpg

Let’s Develop a model using these variables as the predictor variables.

Z = df[['horsepower', 'curb-weight', 'engine-size', 'highway-mpg']] # Fit the linear model

lm.fit(Z, df['price']) # Output : LinearRegression(copy_X=True, fit_intercept=True, n_jobs=None, normalize=False) # Intercept

lm.intercept_ # Output: -15806.624626329234 # Slope

lm.coef_ # Output: [53.49574423, 4.70770099, 81.53026382, 36.05748882]

So, we have an estimated linear model using the linear function

𝑌ℎ𝑎𝑡=𝑎+𝑏1𝑋1+𝑏2𝑋2+𝑏3𝑋3+𝑏4𝑋4

which is equivalent to the price: