How to implement Linear Regression from scratch with Python

แชร์
ฝัง
  • เผยแพร่เมื่อ 11 ม.ค. 2025

ความคิดเห็น • 86

  • @Artificial_Intelligence_AI
    @Artificial_Intelligence_AI 2 ปีที่แล้ว +11

    This channel is amazing ❤. This is the type of content a lot of instructors forget to teach you when you’re learning ML but this girl explains everything very well from scratch. Congratulations for your content and I hope to watch more of your videos, you deserve more views for this incredible job.

  • @yozil11
    @yozil11 2 วันที่ผ่านมา +1

    # Gradients
    d_w = (2 / n_samples) * np.dot(X.T, (y_pred - y))
    d_b = (2 / n_samples) * np.sum(y_pred - y)

  • @tienshinhan8189
    @tienshinhan8189 ปีที่แล้ว +2

    I rarely bother commenting because I'm rarely impressed. This video was amazing. I love that you're showing theory and OOP. Usually I see basic definitions and code all in one script.

  • @afizs
    @afizs 2 ปีที่แล้ว +7

    I have used Linear Regression many times, but never implemented from scratch. Thanks for an awesome video. Waiting for the next one.

    • @AssemblyAI
      @AssemblyAI  2 ปีที่แล้ว +1

      That's great to hear Afiz!

  • @bendirval3612
    @bendirval3612 2 ปีที่แล้ว +7

    Wow, that really was from scratch. And the hardest way possible. But it's perfect for teaching python. Thanks!

    • @AssemblyAI
      @AssemblyAI  2 ปีที่แล้ว +2

      You're very welcome :)

  • @MU_2000
    @MU_2000 7 หลายเดือนก่อน +2

    Thank you for this video. Before i wached it i spend a couple of day to understand how to make custome code without frameworks ))

  • @markkirby2543
    @markkirby2543 ปีที่แล้ว +2

    This is amazing. Thank you so much for all your clear explanations. You really know you stuff, and you make learning this complex material fun and exciting.

  • @ayo4590
    @ayo4590 ปีที่แล้ว +2

    Thank you so much, this video really helped me get started with understanding machine learning algorithms. I would love if you could do a video on how you would modify the algorithm for multivariate linear regression.

  • @haseebmuhammad5145
    @haseebmuhammad5145 5 หลายเดือนก่อน

    I was seeking such kind of lectures. Lectures are awesome

  • @ryanliu93
    @ryanliu93 ปีที่แล้ว +14

    I think there is a problem of missing the 2 multiplication in calculating dw, db in the .fit() method:
    dw = (1/n_samples) * np.dot(X.T, (y_pred-y)) * 2
    db = (1/n_samples) * np.sum(y_pred-y) * 2
    If we follow the slides, it's not absolutely wrong but it can affect the learning rate

    • @priyanshumsharma4276
      @priyanshumsharma4276 ปีที่แล้ว

      No, after derivation, there is no square.

    • @zh7866
      @zh7866 ปีที่แล้ว +1

      The comment suggests multiplication with 2 based on derivation rule NOT square

    • @jhicks2306
      @jhicks2306 11 หลายเดือนก่อน

      Agree with this! Also, thanks, Misra, for the awesome video series.

    • @surajsamal4161
      @surajsamal4161 9 หลายเดือนก่อน +3

      @@priyanshumsharma4276 he's not talking about the square he's talking about the square which came down cause of derivative

  • @l4dybu9
    @l4dybu9 2 ปีที่แล้ว +6

    Thank u so much for this video. 💖💖
    It's makes us feel more confident when we know how to do it drom scratch than using libraries ✨

    • @AssemblyAI
      @AssemblyAI  2 ปีที่แล้ว +1

      That's great to hear!

  • @1000marcelo1000
    @1000marcelo1000 2 ปีที่แล้ว +1

    Amazing video! I learned so much from it! Congrats!!!
    Could explain more detailed all of this and show next steps like "where" and "how" this can be implemented further in some scenarios?

  • @kamranzamanni6826
    @kamranzamanni6826 3 หลายเดือนก่อน

    Thanks for making it easy to follow along!

  • @luis96xd
    @luis96xd 2 ปีที่แล้ว +3

    Amazing video, I liked the code and the explanations, it was easy to read and understand, thanks! 😁👍👏💯

  • @KelvinMafurendi
    @KelvinMafurendi 5 หลายเดือนก่อน +1

    Great explanations indeed. The transition from theory to implementation in Python was awesome!
    Say, is this a good starting point for a beginner in Data Science or I should stick to the out-of-the-box sklearn methods for now?

  • @GeorgeZoto
    @GeorgeZoto 2 ปีที่แล้ว +6

    Another amazing video! Slight typo in the definition of matrix multiplication and =dw part as well as an omission on the constant 2 (which does not effect calculations much) in the code when you define the gradients but other than that this is beautiful 😃

    • @AssemblyAI
      @AssemblyAI  2 ปีที่แล้ว

      Thanks for that!

  • @compilation_exe3821
    @compilation_exe3821 2 ปีที่แล้ว +3

    YOU guys are AWWWWESOME

  • @kreativeworld2073
    @kreativeworld2073 2 ปีที่แล้ว

    Thank you so much.
    It really helped me understand the entire concept:)

  • @sajinsanthosh9536
    @sajinsanthosh9536 2 ปีที่แล้ว +2

    followed the tutorial exactly right, but still different. Using trial version. Thank you*

    • @AssemblyAI
      @AssemblyAI  2 ปีที่แล้ว

      You're welcome :)

  • @1Rashiid
    @1Rashiid 3 หลายเดือนก่อน

    perfect explanation! Thank you.

  • @harry-ce7ub
    @harry-ce7ub 10 หลายเดือนก่อน +2

    Do you not need to np.sum() the result of np.dot(x, (y_pred-y)) in dw as well as multiply by 2?

  • @parthaguha4705
    @parthaguha4705 ปีที่แล้ว +1

    Awesome explanation😇

    • @AssemblyAI
      @AssemblyAI  ปีที่แล้ว

      Glad you liked it!

  • @AndroidoAnd
    @AndroidoAnd 10 หลายเดือนก่อน +1

    I am not sure if you copied this code from Patrick Loeber. He has a youtube video with the same code posted years ago. If you did, please give credit to Patrick.
    This is the name of his video: Linear Regression in Python - Machine Learning From Scratch 02 - Python Tutorial

  • @AbuAl7sn1
    @AbuAl7sn1 6 หลายเดือนก่อน

    thanks lady .. that was easy

  • @MahmouudTolba
    @MahmouudTolba 2 ปีที่แล้ว +1

    your code is clean

  • @mohamedsadeksnoussi8479
    @mohamedsadeksnoussi8479 2 ปีที่แล้ว +4

    The content is really insightful, but for dw and db, should it be (2/n_samples) instead of (1/n_samples) ?

    • @adarshtiwari7395
      @adarshtiwari7395 2 ปีที่แล้ว +2

      did you figure out why she used the equations without 2 in the video?

    • @mohamedsadeksnoussi8479
      @mohamedsadeksnoussi8479 2 ปีที่แล้ว +4

      Hi@@adarshtiwari7395, unfortunately no. But (2/n_samples) appears to be correct.
      I checked out other resources and all of them used (2/n_samples).
      You can even try it by yourselves, (1/n_samples) doesn't affect the model behavior (performance), but from my point of view, it's incorrect.

    • @prashantkumar7390
      @prashantkumar7390 ปีที่แล้ว +3

      @@adarshtiwari7395 It doesn't change our actual optimisation problem, I mean when the mean_square_loss is minimised, any scalar*mean_square_loss will be minimised. Hence using 2 or not doesn't make a difference at all.

    • @mohammedabdulhafeezkhan4633
      @mohammedabdulhafeezkhan4633 ปีที่แล้ว +2

      @@prashantkumar7390 But it is better to use 2 in the equation, even though it's a constant and doesn't affect the outcome in a major way.

    • @prashantkumar7390
      @prashantkumar7390 ปีที่แล้ว +1

      @@mohammedabdulhafeezkhan4633 it doesn't affect the outcome AT ALL.

  • @LouisDuran
    @LouisDuran 9 หลายเดือนก่อน

    WHat check could be added in the case that the model is not converging to the best fit within the n_iters value?

  • @JweJwe-youtube
    @JweJwe-youtube หลายเดือนก่อน

    Thanks for this perfect video!
    When I tried this code, the learning rate (0.001) was too small to fit the ground truth 'weight'( it was about 75.00). I saw the visualization plot and could manually calculate approximate weight.( thought the weight would be between 50 to 100). Finally, the learning rate 1 was way better than 0.001.
    The range of y and x are about -200 to 200 and -3 to 3.
    I thought that
    1. normalizing X and y in order to change each range into -1 to 1 that makes easy to find weight and bias without fixing learning rate( e.g. 0.001 to 1 that I did)
    2. Fit the model
    3. Using the way we normalize at 1. I thought I can Re-normalize the model (2.)
    And my real question is which is common, finding appropriate initial learning rate and bias value OR renormalize model after normalize the ranges of X and y??
    Sorry for my English writing skills :)

    • @JweJwe-youtube
      @JweJwe-youtube หลายเดือนก่อน

      If I enlarge and reduce the coordinate plane, predicting procedure would be complicated. The x_test should be normalize in order to use the normalized model. And only then the re-normalization is meaningful. And also, It may be a problem in terms of post-processing the y_pred because of re-normalization process.

  • @srinivasanshanmugam9527
    @srinivasanshanmugam9527 ปีที่แล้ว

    Thanks for this video, May I know , why we need to run in a for loop for 1000 iterations?

  • @m.bouanane4455
    @m.bouanane4455 ปีที่แล้ว +2

    The gradient calculation lacks the multiplication by coefficient 2, I guess.

  • @tiagosilva856
    @tiagosilva856 2 ปีที่แล้ว +1

    Where did you put the "2" of the mathematical formula in dw and db on phyton?

    • @chidubem31
      @chidubem31 2 ปีที่แล้ว +1

      the 2 is a scaling factor that can be omitted.

    • @tiagosilva856
      @tiagosilva856 2 ปีที่แล้ว +1

      @@chidubem31 ty. I understand. But omitting that don't change the performance? M.S.E still be the same if we don't ommite the "2"?

    • @chidubem31
      @chidubem31 2 ปีที่แล้ว +1

      @@tiagosilva856 MSE will still be the same. intuitively if you multiply the 2 in the formula it scales the x for all values of x, therefore removing it will affect the whole dataset in the same way as if nothing happened

  • @سعیداحمدی-ل1ث
    @سعیداحمدی-ل1ث ปีที่แล้ว

    ادامه بده دختر کارت عالیه ممنون ازت

  • @traagshorts4617
    @traagshorts4617 2 ปีที่แล้ว

    How did you fit the dimensions for the test and weights in the predict function

  • @rajesh_ramesh
    @rajesh_ramesh 6 หลายเดือนก่อน

    the gradient part actually misses the coefficient 2 in both differentiations (dw, db) and y - y^

  • @KumR
    @KumR 11 หลายเดือนก่อน

    Wow... Awesome.. Do you have something like this for Deep learning algo ?

  • @rbodavan8771
    @rbodavan8771 2 ปีที่แล้ว +2

    how you designed your video templets is awesome.
    but it would be good for learner if you also post it on kaggle notebook and link it each other. sometimes it's happened to me like best to read then watch. let me know your thought

    • @AssemblyAI
      @AssemblyAI  2 ปีที่แล้ว +1

      You can find the code in our github repo. The link is in the description :)

  • @gechimmchanel2917
    @gechimmchanel2917 ปีที่แล้ว +1

    Has anyone encountered the same situation as me when given a larger dataset with about 4 weights and 200 rows, the result predicts -inf or NaN , anyone have a way to fix this??

    • @gechimmchanel2917
      @gechimmchanel2917 ปีที่แล้ว

      [-inf -inf -inf] -inf
      [-inf -inf -inf] -inf
      [-inf -inf -inf] -inf
      result của weight and bias :))

  • @2122345able
    @2122345able 11 หลายเดือนก่อน

    where is the part where gradient decent is being coded , how the code will know when to stop ?

  • @surendrakakani7014
    @surendrakakani7014 ปีที่แล้ว

    I have taken input(200 ,1) while executing above program i am getting y_pred as (200,200) can and geting dw shape as (1,200) but dw should be (1,1) right any body explaing is that correct

  • @omaramil5103
    @omaramil5103 ปีที่แล้ว

    Thank you ❤

    • @AssemblyAI
      @AssemblyAI  ปีที่แล้ว

      You're welcome 😊

  • @okefejoseph6825
    @okefejoseph6825 7 หลายเดือนก่อน

    Is there a way I can get the slide?

  • @alexisadr
    @alexisadr ปีที่แล้ว

    Thank you!

  • @MartinHolguin-d3k
    @MartinHolguin-d3k ปีที่แล้ว

    your explanation is amazing and I see how linear regression works however this only works with 1 feature
    and if you want to implement it with more than one it will fail.

  • @akifarslan6022
    @akifarslan6022 ปีที่แล้ว +2

    Güzel video

  • @shivamdubey4783
    @shivamdubey4783 2 ปีที่แล้ว

    can you plzz explain why we took x.T transpose

    • @0xmatriksh
      @0xmatriksh 2 ปีที่แล้ว +2

      It is because we are doing dot product of two matrices here. So we need two matrices to be in the form of mxn and nxp, that means the number of columns in first matrix should be same as the number of rows in second matrix.
      And here,
      Suppose number of rows of X is n(which is same as y)
      But the n is number of rows for both of them so we transpose X to make n in column to match the n of mxn and nxp(like explained above) to successfully dot product them

  • @Salma30b
    @Salma30b 3 หลายเดือนก่อน

    Why don't our teachers teach us like this?? Like 15 minutes of this video is all it took to understand hours of lecturing

  • @prigithjoseph7018
    @prigithjoseph7018 2 ปีที่แล้ว

    Hi can you please upload, presentation file also

  • @سعیداحمدی-ل1ث
    @سعیداحمدی-ل1ث ปีที่แล้ว

    لطفا ویدیو های بیشتری بسازید

  • @jinxiong2973
    @jinxiong2973 2 ปีที่แล้ว

    how to improve my attention span , I have good ideas and soft that I tNice tutorialnk up , the problem is putting it down in fruit loops and knowing

  • @ge_song5
    @ge_song5 6 หลายเดือนก่อน +2

    what's her name?

    • @LegendJonny-bq6td
      @LegendJonny-bq6td 21 วันที่ผ่านมา +1

      It's written in the title "with Python"😅

  • @timuk2008
    @timuk2008 5 หลายเดือนก่อน

    J'(m,b) = [df / dm df/db] ===> m is the w

  • @hiteshmistry413
    @hiteshmistry413 ปีที่แล้ว

    Thank you very much, You made Linear Regression very easy for me. Here is how the linear regression training looks like in action "th-cam.com/video/2QqLl_wpfSo/w-d-xo.html"

  • @hannukoistinen5329
    @hannukoistinen5329 ปีที่แล้ว

    Forget Python!!! It's just fashionable right now. R is much, much better!!!

  • @saideeppandey3788
    @saideeppandey3788 4 หลายเดือนก่อน

    Pls note x.wtranspose because w is (1,n)