Linear Regression Normal Equation Python
Key Takeaways
This video demonstrates how to implement linear regression using the normal equation in Python, utilizing the NumPy library for efficient numerical computations.
Full Transcript
[Music] alright guys welcome back for another machine learning video so in this video I want to show you how to implement linear regression using the normal equation if you want to see the derivation of the normal equation I actually made a blog post and I'll link it in the description so in this video we'll just actually focus on the implementation of it so let's start with doing import numpy as NP then we're gonna let X be so just quickly show like go through the shapes of the data so let X be shape training examples come features then we're gonna let Y be shaped training examples comma 1 and so the output output W will be shaped so sort of the weights right will be features comma 1 and yeah so what we're gonna do is we're going to define linear regression regression normal equation and we're gonna send in x and y and let's just do a passings right now and then we're gonna have if name equals main and we're gonna run that right here but so first thing we want to do is we're gonna add a vector of of ones for the bias term so we're gonna do ones is NP dot ones of X shape of 0 right we're gonna add for all training examples we're gonna add we're gonna make a 0 vector I mean a 1 vector and then we're gonna add it so we're going to X is NP dot append 1 and then X and then we're gonna add it along axis 1 right along the features so just each training example will get an additional additional feature which will just be a 1 vector which is for the bias term and yes when we've done that are we going to do is kalca using the normal equation formula MP dot of MP dot Lin elk that you can use inverse here I'm gonna use the pseudo-inverse so P inverse of n P dot of X transpose X this is just exactly following the the normal equation and then MP dot X transpose Y and yeah then we're gonna return W and then we could do like X is let's actually do let's do NP dot array and then let's do just a simple data so we're gonna have this u a single array and it's do 0 1 2 for X values let's do Y values let's say n P dot array and say minus 1 0 1 right so the the intercept value of this if we made a straight line would be -1 and then the slope of this would be 1 since Forex each increase of X were increasing y1 then we can just do W W is linear regression actually let's print it this shape so we shape print why shape so we have X right here which is 1 comma 3 so this should be we should do transpose here since we need to have every training example we need to have it in sort of in this shape right here right so it'll be set in the beginning so we have three points so the training examples are three and we have a single feature which means that we should take this this should be transposed and this is well since we said actually this is correct so this is fine and then let's do linear regression normal equation X Y and let's print what we get so -1 1 which is sort of what we expect right if we if we have this we expect a intercept value of minus 1 and the slope is 1 so that's just a quick check that it works we could also do something more I guess general we can do like NPR and I'm friend say we have I don't know 5 5,000 points and say we have again in the XY plane then we have Y to be y1 to be let's say 5 times X and then plus some random noise let's say let's do 5,000 comma 1 and then its time is it by 0.1 or something so we have the slope and then we have some random noise and then we can just do w1 linear regression normal equation x1 y1 you know we can do yeah I guess print W 1 dot shape or something like that and alright so yeah this would not be it should be x1 yeah and then we get the shape of it so this is just this is just our values in the XY plane right so we should have the intercept value and then the slope so if we print w1 we should get something that is approximately 5 in the slope right yeah so we get this is the intercept value and this is the slope all right OOP that video was useful for you thank you so much for watching the video and I hope to see you in the next one [Music]
Original Description
In this video I show how to implement linear regression using the normal equation.
❤️ Support the channel ❤️
https://www.youtube.com/channel/UCkzW5JSFwvKRjXABI-UTAkQ/join
Paid Courses I recommend for learning (affiliate links, no extra cost for you):
⭐ Machine Learning Specialization https://bit.ly/3hjTBBt
⭐ Deep Learning Specialization https://bit.ly/3YcUkoI
📘 MLOps Specialization http://bit.ly/3wibaWy
📘 GAN Specialization https://bit.ly/3FmnZDl
📘 NLP Specialization http://bit.ly/3GXoQuP
✨ Free Resources that are great:
NLP: https://web.stanford.edu/class/cs224n/
CV: http://cs231n.stanford.edu/
Deployment: https://fullstackdeeplearning.com/
FastAI: https://www.fast.ai/
💻 My Deep Learning Setup and Recording Setup:
https://www.amazon.com/shop/aladdinpersson
GitHub Repository:
https://github.com/aladdinpersson/Machine-Learning-Collection
✅ One-Time Donations:
Paypal: https://bit.ly/3buoRYH
▶️ You Can Connect with me on:
Twitter - https://twitter.com/aladdinpersson
LinkedIn - https://www.linkedin.com/in/aladdin-persson-a95384153/
Github - https://github.com/aladdinpersson
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Aladdin Persson · Aladdin Persson · 47 of 60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
▶
48
49
50
51
52
53
54
55
56
57
58
59
60
computeCost.m Linear Regression Cost Function - Machine Learning
Aladdin Persson
gradientDescent.m Gradient Descent Implementation - Machine Learning
Aladdin Persson
Neural Network from scratch - Part 1 (Standard Notation)
Aladdin Persson
Neural Network from scratch - Part 2 (Forward Propagation)
Aladdin Persson
Neural Network from scratch - Part 3 (Backward Propagation)
Aladdin Persson
Neural Network from scratch - Part 4 (With Python)
Aladdin Persson
sigmoid.m - Programming Assignment 2 Machine Learning
Aladdin Persson
costFunction.m - Programming Assignment 2 Machine Learning
Aladdin Persson
predict.m - Programming Assignment 2 Machine Learning
Aladdin Persson
costFunctionReg.m - Programming Assignment 2 Machine Learning
Aladdin Persson
lrCostFunction.m - Programming Assignment 3 Machine Learning
Aladdin Persson
oneVsAll.m - Programming Assignment 3 Machine Learning
Aladdin Persson
predictOneVsAll.m - Programming Assignment 3 Machine Learning
Aladdin Persson
predict.m - Programming Assignment 3 Machine Learning
Aladdin Persson
Caesar Cipher Encryption and Decryption with example
Aladdin Persson
Cryptography: Caesar Cipher Python
Aladdin Persson
Vigenere Cipher Explained (with Example)
Aladdin Persson
Cryptography: Vigenere Cipher Python
Aladdin Persson
Hill Cipher Explained (with Example)
Aladdin Persson
Cryptography: Hill Cipher Python
Aladdin Persson
Interval Scheduling Greedy Algorithm: Python
Aladdin Persson
Weighted Interval Scheduling Algorithm Explained
Aladdin Persson
Weighted Interval Scheduling Python Code
Aladdin Persson
Sequence Alignment | Needleman Wunsch Algorithm
Aladdin Persson
Sequence Alignment | Needleman Wunsch in Python
Aladdin Persson
Codility BinaryGap Python
Aladdin Persson
Codility CyclicRotation Python
Aladdin Persson
Derivation Linear Regression with Gradient Descent
Aladdin Persson
Linear Regression Gradient Descent From Scratch in Python
Aladdin Persson
Pytorch Neural Network example
Aladdin Persson
Pytorch CNN example (Convolutional Neural Network)
Aladdin Persson
Pytorch LeNet implementation from scratch
Aladdin Persson
Pytorch VGG implementation from scratch
Aladdin Persson
Pytorch GoogLeNet / InceptionNet implementation from scratch
Aladdin Persson
How to save and load models in Pytorch
Aladdin Persson
How to build custom Datasets for Images in Pytorch
Aladdin Persson
Pytorch Transfer Learning and Fine Tuning Tutorial
Aladdin Persson
Pytorch Data Augmentation using Torchvision
Aladdin Persson
Pytorch Quick Tip: Weight Initialization
Aladdin Persson
Pytorch Quick Tip: Using a Learning Rate Scheduler
Aladdin Persson
Pytorch ResNet implementation from Scratch
Aladdin Persson
Pytorch TensorBoard Tutorial
Aladdin Persson
Pytorch DCGAN Tutorial (See description for updated video)
Aladdin Persson
Naive Bayes from Scratch - Machine Learning Python
Aladdin Persson
Spam Classifier using Naive Bayes in Python
Aladdin Persson
K-Nearest Neighbor from scratch - Machine Learning Python
Aladdin Persson
Linear Regression Normal Equation Python
Aladdin Persson
SVM from Scratch - Machine Learning Python (Support Vector Machine)
Aladdin Persson
Neural Network from Scratch - Machine Learning Python
Aladdin Persson
Pytorch RNN example (Recurrent Neural Network)
Aladdin Persson
Pytorch Bidirectional LSTM example
Aladdin Persson
Pytorch Text Generator with character level LSTM
Aladdin Persson
Logistic Regression from Scratch - Machine Learning Python
Aladdin Persson
K-Means Clustering from Scratch - Machine Learning Python
Aladdin Persson
Pytorch Torchtext Tutorial 1: Custom Datasets and loading JSON/CSV/TSV files
Aladdin Persson
Pytorch Torchtext Tutorial 2: Built in Datasets with Example
Aladdin Persson
Pytorch Torchtext Tutorial 3: From Textfiles to Dataset
Aladdin Persson
Paper Review: Sequence to Sequence Learning with Neural Networks
Aladdin Persson
Pytorch Seq2Seq Tutorial for Machine Translation
Aladdin Persson
Pytorch Seq2Seq with Attention for Machine Translation
Aladdin Persson
More on: ML Maths Basics
View skill →Related AI Lessons
⚡
⚡
⚡
⚡
How to Learn a Hard Technical Skill Without Burning Out
Dev.to · Anas Kalthoum | FreeBrain
After interviewing over 100 ML Candidates. Last Week Someone Walked In and Made Me Take Notes.
Medium · Machine Learning
How AI Learns with Less Labeled Data
Medium · Machine Learning
Mastering TypeScript — Understanding the TypeScript Compiler (tsc) from Scratch — Lesson 2
Medium · JavaScript
🎓
Tutor Explanation
DeepCamp AI