Master Linear Regression Step by Step | Regression Analysis Tutorial in Machine Learning | Day 5

AnuTech-CH · Beginner ·📐 ML Fundamentals ·11mo ago

About this lesson

This video is all about linear regression, which is a fundamental part in supervised machine learning. This is a beginner-friendly video to understand what is linear regression, how it works and its mathematical example. This video is part of our Machine Learning series where we break down every topic in a clear and simple way. TimeStamps: 00:00:00- Introduction 00:00:18- Linear Regression? 00:00:47- Example 00:01:57- Linear Regression Applications 00:02:43- Mathematical Expression 00:06:22- Python example 00:16:13- Advantages 00:16:22- Disadvantages 00:16:37- Types of Linear Regression 00:17:16 - Conclusion 👍 Like, Share, and Subscribe for more videos on: Python | SQL | Artificial Intelligence | Generative AI | Machine Learning 🔔 Hit the bell icon to stay updated with our upcoming videos! 🔴 Subscribe to our channel to get video updates. Hit the subscribe button above: https://goo.gl/6ohpTV Do not miss: Machine Learning- https://youtube.com/playlist?list=PLQtyrrKdUiv3x1vNumlRJlxI1EyqEXbRA&si=_zEDO3fPNxIcU11B Python Tutorials - https://youtube.com/playlist?list=PLQtyrrKdUiv2p1IEmuXRZu4F2P87mt4as&si=rmuRuSDzf6YpsTKf Generative AI (GenAI) - https://youtube.com/playlist?list=PLQtyrrKdUiv2Xd4Dp_N4gJAy_hP8Mpy4N&si=GYNXt6e_2ckwWXSq SQL - https://youtube.com/playlist?list=PLQtyrrKdUiv2p1IEmuXRZu4F2P87mt4as&si=OiDkstzQRAuX5WDo #linearregression#supervisedlearning #machinelearning

Full Transcript

Hi everyone, welcome to machine learning series. Today we are going to learn about linear regression. So without a further ado, let's start our video. So what is linear regression? Linear regression is a type of supervised machine learning algorithm where the model learns from the label data and identify patterns from those data to make predictions. And there is a linear relationship between input and output. Which means if we give some input data then it will try to predict an output number such as in predicting the price of house. And it's like drawing a straight line through our data and finding the best possible relationship between the inputs and the outputs. Let's see an example. Suppose we are trying to predict a student's score based on number of hours they have studied. It is obvious that if a student study for more hours then they will get better marks. Let me explain this. For example, I have some data like this. Here we have two variables. One is independent variable X which is study hours and another is dependent variable Y that is score. So let's assume if the student study for 1 hour they will score 54 and if they study for 2 hours they will score 60 and if they study for 3 hour they will score 66 and 4 hour they will get 71 and if they study for 5 hour they will get 79. So this is our data set and if we plot this in graph we will see like this. So see here we get a straight line and the score changes at a constant rate as the input changes. That is if a students study for more hours then they will get more score. Hence this is what simple linear regression looks like. Linear regression applications. It is often used in predicting house prices. For example, linear regression helps us to create a model to predict the price of the house based on its size. Bigger the house, higher the prices. It is used in education to predict student performances. In finance, to predict stock prices. In marketing, for example, a company wants to know how much to spend on ads to increase sales. In that case, linear regression helps them to forecast sales for given ad spins. It can be used in agriculture. For example, farmers can use data such as temperature and rainfall to predict crop LS and so on. Therefore, these are some of the applications of linear regression as we are dealing with numeric and continuous values which is exactly what linear regression is designed for. Mathematical expression. Mathematical expression or the best fit line for linear regression with one independent variable is given by y = to mx + b where x is the input that is independent variable. y is the predicted value that is dependent variable. m is the slope of the line that is how steep or how much y changes for a unit change in x and b is the intercept that is when line cuts y-axis or the value of the y when x equals to zero. The goal of linear regression is to find the best values for M and B that fit our data. Let's see the real example and calculate slope M and intercept B. So I am going to take the earlier data set. We have study hours as X a dependent variable and the another is scores that is Y which is independent variable. Now let's calculate the mean of X as shown in the table. We add all the values of X and divide by five. Then we'll get the mean of x that is 1 + 2 + 3 + 4 + 5 and then divide by 5 and the result we get is 3. So this is our mean and we represent it as a x dash. Similarly we calculate mean of y and as shown in the table we get the value 66. Next step is to calculate the deviation of x that is x dash which is our mean value minus x. Suppose 3 - 1 = 2, 3 - 2 = 1 and so on. Then we calculate the square of deviation of x that is square of 2 is 4, 1 is 1, 0 is 0, - 1 is 1, - 2 is 4. And next step is to calculate deviation of y that is yd - y. Then we calculate x dash - x into y - y. And as a result we get like 2 into 12 that is 24 1 into 6 that is 6 and so on. And at last we calculate the sum of product of deviations that is 24 + 6 + 0 + 5 + 26 that is equal to 61. So these are our calculations. The formula to calculate slope is m = to sum of product of deviations divided by sum of square of deviations of x. that is 61 / 10 equals to 6.1. So this means if I change the value of x by 1 unit then the value of y changes to 6.1. And the formula to calculate intercept b is b = to mean of y minus slope into mean of x that is 66 - 6.1 into 3 that is equal to 66 - 18.3 which is 47.7. Hence this is the value of B. The intercept B is the value of Y when X equals to zero. That means in this example 47.7 could be the base score when a student studies for 0 hours. Now we want to know how much a student scores if he or she studies for 6 hours. We have our equation y = mx + b. And here we have calculated all the values needed. So y = to mx + b that is 6.1 into 6 + 47.7 = to 84.3. Therefore if the student study for 6 hours he or she will score 84.3 marks. So this is the best case scenario. Let's see the example of linear regression in Python code. So first let's import our libraries. This is step one. and then import numpy as np. Numpy is used to create data arrays and it is for numerical operations. Now let's import another import matt plot live dot pi plot s plt. So matt plot live.pip plot will help to visualize the data and the fitted line. Next we will skarn linear model and then import linear regression. So we import linear regression which is the regression model from scikit learn. Next we are step two like this and then then step two is about creating data set data set right and then we'll create the data set of X and Y where X is the study hours. So has study hours and then variable x equals to and p array and let's give our data that is 1 2 3 4 5 and then close the parenthesis. this and dot sorry dot reshape and -1 comma 1. So here x is the hours studied which is our input and this is independent variable. So this will recept the data into the column vector. Another we have is scores. So scores and then y equals to np dot arrays and then we have the max is 54 60 66 71 79 and So y is a scores obtained which is the output and this is a dependent variable. So this is our data set. Now let's create and train the linear regression model that is step three. Create and train the model. So model equals to linear regression and then this. So we have created a linear regression model. Now we will train the model. model dot fit x comma y. So this fit trains the model using the data. Next step is to make prediction. Let's say we want to predict the score for a students who have studied for 6 hours. Therefore, step four is like make a has sorry make a prediction. So score predicted equals to model dot predict and then we want to predict for 6 hours. So predict six will provide the predicted score for a student who have studied for 6 hours. Now let's print the result. print and then score predicted for 6 hours of study and then score underscore predicted and then I zero. Okay, now let's run this code. I got the error. Ah, skarn dot linear. [Music] Oh, too many errors. Track NP is 1 2 3 4 save. Sorry. Okay. Score predicted for 6 hours of study is 84.3. Now let's visualize our result. Therefore, the step five would be uh let me write here. Okay. hash step five equals to um visualization and then I'll plot plot scatter x comma y comma and let's give color like blue and then a label equal equals to um actual data and then close this. Another is plot dot plot x comma model dot predict uh x and then color equals to red and comma label equals to regression line. Okay. So here the scatter plots the actual data points and in blue color whereas the plot draws the regression line predicted by the model in red color. And let's give X and Y like X label uh then study hours and then log at Y label equals to scores and then plot Our title study hours versus scores and then plot dot lesson and then plot dot show. So labels and the titles are added for clarity and so we'll display the plot. So let's run this. See we get our result. So this blue dots are the actual data and the red line represent the regression line and we have predicted the uh scores for 6 hour is like 84.3. Advantages very easy to implement. Results are easy to interpret. Great for small data sets. Computationally fast. Disadvantages of linear regression. They are sensitive to outliers. They assumes linearity and it doesn't perform well on complex or nonlinear data and it can only predicts numbers. Types of linear regression. Simple linear regression. If there is only one independent variable then it is known as simple linear regression. Multiple linear regression. When there are more than one variable then it is known as multiple linear regression. So that's it for linear regression. We have learned what is linear regression, how it works, math behind it and how to implement in Python and its advantages and disadvantages. So if you found this video helpful do not forget to like, share and subscribe my channel. Share it with your friends who are studying machine learning. See you in the next video. Stay tuned.

Original Description

This video is all about linear regression, which is a fundamental part in supervised machine learning. This is a beginner-friendly video to understand what is linear regression, how it works and its mathematical example. This video is part of our Machine Learning series where we break down every topic in a clear and simple way. TimeStamps: 00:00:00- Introduction 00:00:18- Linear Regression? 00:00:47- Example 00:01:57- Linear Regression Applications 00:02:43- Mathematical Expression 00:06:22- Python example 00:16:13- Advantages 00:16:22- Disadvantages 00:16:37- Types of Linear Regression 00:17:16 - Conclusion 👍 Like, Share, and Subscribe for more videos on: Python | SQL | Artificial Intelligence | Generative AI | Machine Learning 🔔 Hit the bell icon to stay updated with our upcoming videos! 🔴 Subscribe to our channel to get video updates. Hit the subscribe button above: https://goo.gl/6ohpTV Do not miss: Machine Learning- https://youtube.com/playlist?list=PLQtyrrKdUiv3x1vNumlRJlxI1EyqEXbRA&si=_zEDO3fPNxIcU11B Python Tutorials - https://youtube.com/playlist?list=PLQtyrrKdUiv2p1IEmuXRZu4F2P87mt4as&si=rmuRuSDzf6YpsTKf Generative AI (GenAI) - https://youtube.com/playlist?list=PLQtyrrKdUiv2Xd4Dp_N4gJAy_hP8Mpy4N&si=GYNXt6e_2ckwWXSq SQL - https://youtube.com/playlist?list=PLQtyrrKdUiv2p1IEmuXRZu4F2P87mt4as&si=OiDkstzQRAuX5WDo #linearregression#supervisedlearning #machinelearning
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Related Reads

Up next
Difference between MCP & API | MCP vs API Explained | Why AI Needs MCP | Tamil | Karthik's Show
Karthik's Show
Watch →