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

Linear Regression in Python

This document provides an overview of machine learning algorithms and linear regression. It defines what algorithms and machine learning algorithms are used for, and lists some common types like linear regression, logistic regression, K-means clustering, and support vector machines. Linear regression is then explained in more detail, defining independent and dependent variables, and showing how to perform simple and multiple linear regression, including equations and examples in Python code.

Uploaded by

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

Linear Regression in Python

This document provides an overview of machine learning algorithms and linear regression. It defines what algorithms and machine learning algorithms are used for, and lists some common types like linear regression, logistic regression, K-means clustering, and support vector machines. Linear regression is then explained in more detail, defining independent and dependent variables, and showing how to perform simple and multiple linear regression, including equations and examples in Python code.

Uploaded by

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

Machine

Learning Lab
Linear Regression in Python
Algorithms
An algorithm is a procedure or set of steps or rules to accomplish a task.

Algorithms are one of the fundamental concepts in, or building blocks of,
computer science.

Some of the basic types of tasks that algorithms can solve are

● sorting,
● searching,
● graph-based computational problems
Machine Learning Algorithms
Machine learning algorithms are largely used to:

Predict,

Classify, or

Cluster

Machine learning algorithms are the basis of artificial intelligence (AI) such as
image recognition, speech recognition, recommendation systems,
ranking and personalization of content.
Machine Learning Algorithms
There are a number of Machine Learning Algorithms, Some of them are:

Linear Regression , Logistic Regression, Linear Discriminant Analysis (LDA)

K-Means , Naive Bayes , K-Nearest Neighbors

Support Vector Machines (SVM)

Bagging and Random Forest, Principal Component Analysis (PCA)


Linear Regression
Linear Regression is one of the most well known machine-learning algorithms.

It is one of the fundamental supervised machine-learning algorithms.

Linear Regression is a Statistical Model to predict the relationship between an


independent and dependent variable.

Linear regression might be simple linear or multivariate.

● Simple linear regression: the case of one explanatory variable


● Multiple linear regression: more than one explanatory variable
Linear Regression
Independent Variable

The value of an independent variable does not change based on the effects of the
other variables.

An independent variable is used to manipulate the dependent variable.

It is often denoted by ‘x’


Linear Regression
Dependent Variable

The value of this dependent variable changes when there is any change in the
values of the independent variables.

It is often denoted by ‘y’


Regression Equation
The simplest linear regression equation with one independent and one
dependent variable is :

y = m*x + c
Simple Linear Regression Equation
Let us have two sets of data with x as the independent variable and y as the
dependent variable.
Simple Linear Regression Equation
Then we calculate the mean or the average values of x and y.

The average of values of x and y is 3 and 4, mean or average point (3,4).


Simple Linear Regression Equation
Now let us discuss the regression line equation.
Simple Linear Regression Equation
Simple Linear Regression
Let us find predicted value of Y for corresponding value of X using the value of
m = 0.6 and c = 2.2
Simple Linear Regression
Let us find the sum of squared errors from Y_predict and Y_actual.

The sum of squared errors for this regression line is 2.4.


Multiple Linear Regression
Multiple Linear Regression Equation has multiple independent variables x1,
x2, …. xn, that influence the independent variable Y.
Linear Regression in Python
Let us see the multiple linear regression in python to do a profit estimation of
a company with certain independent variables.

The independent variables will be:

1. Administration
2. R & D Spend
3. Marketing Spend
4. State

The dependent variable will be: Profit


Linear Regression in Python
Let us see the linear regression in python for multiple linear regression.

1. First we will import important libraries we need:


Linear Regression in Python
2. Import the dataset and Extract independent and dependent variables
Linear Regression in Python
The data looks like the following in notepad
Linear Regression in Python
3. Visualize the dataset in a correlation matrix
Correlation Matrix
Linear Regression in Python
4. Encoding Categorical data
Linear Regression in Python
5. Avoid dummy variable trap

6. Split Train and Test data


Linear Regression in Python
7. Fit Multiple linear regression to training set
Linear Regression in Python
8. Predict the test set
Linear Regression in Python
9. Calculate the coefficient and intercept
Linear Regression in Python
10. Evaluate the Model

The R squared value of 0.93 proves that the model is a good model.

You might also like