How to implement Logistic Regression from scratch with Python

แชร์
ฝัง
  • เผยแพร่เมื่อ 12 ก.ย. 2024
  • In the third lesson of the Machine Learning from Scratch course, we will learn how to implement the Logistic Regression algorithm. It is quite similar to the Linear Regression implementation, just with an extra twist at the end.
    You can find the code here: github.com/Ass...
    Previous lesson: • How to implement Linea...
    Next lesson: • How to implement Decis...
    Welcome to the Machine Learning from Scratch course by AssemblyAI.
    Thanks to libraries like Scikit-learn we can use most ML algorithms with a couple of lines of code. But knowing how these algorithms work inside is very important. Implementing them hands-on is a great way to achieve this.
    And mostly, they are easier than you’d think to implement.
    In this course, we will learn how to implement these 10 algorithms.
    We will quickly go through how the algorithms work and then implement them in Python using the help of NumPy.
    ▬▬▬▬▬▬▬▬▬▬▬▬ CONNECT ▬▬▬▬▬▬▬▬▬▬▬▬
    🖥️ Website: www.assemblyai...
    🐦 Twitter: / assemblyai
    🦾 Discord: / discord
    ▶️ Subscribe: www.youtube.co...
    🔥 We're hiring! Check our open roles: www.assemblyai...
    ▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
    #MachineLearning #DeepLearning

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

  • @josiahtettey6315
    @josiahtettey6315 ปีที่แล้ว +10

    Best concise video on logistic regression I have seen so far

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

      That's great to hear, thanks Josiah!

  • @sreehari.s6515
    @sreehari.s6515 2 ปีที่แล้ว +3

    went through when I first started video editing, now it's taking a whole new switch and learning soft will only boost my courage for the

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

    Great video, but my only dubt comes when J'() is calculated as the derivate of MSE and not as the derivate of the Cross Entropy, which is the loss function that we are using

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

      Found the answer yet? Please let us know if you do!

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

      That's what I noticed too.

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

      Derivation of Cross Entropy looks like derivation of MSE but y^ is calculated differently.

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

      log loss

    • @HamidNourashraf
      @HamidNourashraf 10 หลายเดือนก่อน

      We should start with Maximum Likelihood. Take the log. Then take derivative with respect to B (or W). Not sure how she took derivative. I guess she used MSE for classification problem instead of using Binary Cross Entropy. 🤔. Log L(B) = Sigma_{i=1}{N}((y_i*B*X_i - log(1 + exp(B*X_)).

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

    def sigmoid(x):
    x = np.clip(x, -500, 500)
    return (1/(1+np.exp(-x)))
    To avoid overflow runtime error as the return statement can reach large values

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

    This was a great video, will there be one in the future that covers how to do this for multiple classes?

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

    how is the derivative of loss function w.r.t weights same for cross entropy loss and MSE loss ?

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

    need more algorithms , you are the best

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

    Great work from @AssemblyAI 👍✨thank you from India.

  • @akhan344
    @akhan344 11 หลายเดือนก่อน +1

    superb video! I am saying that because coding from scratch is important for me.

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

    AWESOME EXPLANATION THANKS A LOT !!

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

      You're very welcome!

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

    Wow , what a great video, very helpful

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

      Glad it was helpful!

  • @prodipsarker7884
    @prodipsarker7884 10 หลายเดือนก่อน

    Studying CSE in GUB from Bangladesh , Love the way you teach the explanation & everything ; )

  • @DanielRamBeats
    @DanielRamBeats 10 หลายเดือนก่อน

    Thanks for sharing this, I am doing something similar in JavaScript. The part about calculating the gradients for backpropagation is very helpful!

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

    We should maximize likelihood or minimize minus likelihood, I think the cost function is missing a minus , Am i right ?

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

    7:51 Why you didn't multiplied by 2 the derivatives?

    • @stanvanillo9831
      @stanvanillo9831 7 หลายเดือนก่อน +1

      Technically you would have to but in the end it does not make a difference, you only effectively half your learning rate if you don't multiply by two.

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

      Same question arises for me too why it wasnt multiplied by 2 and for bias it took sum instead of mean ?

  • @lebesgue-integral
    @lebesgue-integral 6 หลายเดือนก่อน

    Very good! Thanks for your videos!

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

    I have seen other videos where people use a ReLU function instead of sigmoid. Would this logistic regression algorithm be an appropriate place to use ReLU instead of Sigmoid? If not, why not?

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

      Logistic regression typically uses the sigmoid activation function because it provides a probability interpretation and is mathematically suited for binary classification. ReLU is more commonly used in deep neural networks with hidden layers, not in logistic regression.

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

    Great work! Thank you :)

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

    love your code 👍

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

    Amazing video, I'm liking so much this free course, I'm learning a lot, thanks! 😁💯😊🤗

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

      Happy to hear that!

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

    Love it. Keep up the good work

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

    Excellent video. Thanks.

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

    The same 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
    It does not affect too much, but we follow the slide to not be confusing

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

      I noticed this as well. But adding in those *2 reduced the accuracy of my predictor from 92.98% to 88.59%

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

      But in bias why we are not using mean coz the formula says summation then it has 1/N too ?

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

    with the partial derivatives, where did the multiple 2 go?

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

    wow you are an amazing teacher thanks alot god l love youtube !!!!!

  • @OmarKhaled-dw7oi
    @OmarKhaled-dw7oi ปีที่แล้ว

    Awesome work, also, great English !

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

    When you write the dw and db, shouldn't be (2 / n_samples)? There is the 2 in the derivative that you can take outside of the summation

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

      That's what I am thinking from previous video

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

    Hello @AssemblyAI, where can I find the slides?

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

    why you have not used summation for dw for calculating error

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

    how can find presentation file.

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

    I have a question, why can't you include accuracy function in class module?

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

    Why doesn't this algorithm work on the Diabetes dataset? I'm getting an accuracy of 0

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

    Please I want to build a personality assessment through cv analysis using this model, could you please help me?

  • @noname-anonymous-v7c
    @noname-anonymous-v7c 7 หลายเดือนก่อน

    7:09 The gradient should not have the coefficient 2 . 7:43 linear_pred there should be a minus sign before np.dot.

    • @noname-anonymous-v7c
      @noname-anonymous-v7c 7 หลายเดือนก่อน

      The above mentioned issue does not have much effect on the predicting result though.

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

    Does this use regularization?

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

    Thanks 🙏 ❤

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

    What if you have categorical data? Like if different scale

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

    Is there a way to visualize this?

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

    Amazing video!
    Can you add the plot of it?

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

    Super 👏👏

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

    Hi, does an increase in sample size increase the prediction accuracy?

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

      Look for statquest's Logistic regression playlist.

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

    Amazing

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

    Relevant feature selection not shown

  • @mohammadnaweedmohammadi5936
    @mohammadnaweedmohammadi5936 10 หลายเดือนก่อน

    I didn't know exactly why you imported Matplot Library

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

    Can we make this work for multiclass classification?

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

      yes you can use logistic regression for problems like digit recognition as well

  • @md.alamintalukder3261
    @md.alamintalukder3261 ปีที่แล้ว

    You are superb

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

    My accuracy came to be around 40, even after tweaking with n_iters, lr couple of times, is that okay ?

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

      same

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

      I think we made the same stupid mistake where we did not loop through for n_iters. for _ in range(self.n_iters):. silly me

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

    But why are we using numpy here it's supposed to be from scratch

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

    Can it be used to predict house prices?

  • @xhtml-xe7zg
    @xhtml-xe7zg 11 หลายเดือนก่อน

    wish one day i will be in this level of coding

  • @Semih-nd3sq
    @Semih-nd3sq 3 หลายเดือนก่อน

    Where is the backward propagation? In conclusion logistic regression is also a neural network

  • @krrsh
    @krrsh 8 หลายเดือนก่อน

    In predict(), is it X.T or just X for the linear_pred dot product?

  • @WilliamDye-willdye
    @WilliamDye-willdye 2 ปีที่แล้ว +2

    I haven't used enough Python yet to accept the soul-crushing inevitability that there's going to be "self." everywhere. I guess you could call it "self." hatred. Maybe ligatures could come to the rescue, replacing every instance with a small symbol. While we're at it, put in ligatures for double underscores and "numpy." (or in the case of this video, "np.").
    Yes, it's an aesthetic rant that is ultimately not a big deal, but gradient descent is a beautifully simple concept. The presenter does a great job of matching that simplicity with clean, easy to follow code. Maybe it's not such a bad thing to be irritated at the parts of her code which are inelegant only because the language doesn't give her better options.

  • @gajendrasinghdhaked
    @gajendrasinghdhaked 10 หลายเดือนก่อน

    make for multiclass also

  • @md.alamintalukder3261
    @md.alamintalukder3261 ปีที่แล้ว

    Like it most

  • @moonlight-td8ed
    @moonlight-td8ed 11 หลายเดือนก่อน

    A M A Z I N G

  • @user-qo1xg5oq7e
    @user-qo1xg5oq7e 10 หลายเดือนก่อน

    تو عالی هستی❤

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

    What is this witchcraft? I thought ML was supposed to be too hard to know wth is going on!

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

    NICEEE ♥💙💙💚💚

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

    -1/N

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

    ♥♥♥♥♥

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

    Amazing video but you're going over too fast

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

      Thanks for the feedback Lucian!

  • @hoseinhabibi2385
    @hoseinhabibi2385 2 หลายเดือนก่อน

    i got 0.94 accuracy with below code:
    import numpy as np
    from sklearn.model_selection import train_test_split
    from sklearn import datasets
    import matplotlib.pyplot as plt
    from sklearn.linear_model import LogisticRegression
    bc = datasets.load_breast_cancer()
    X, y = bc.data, bc.target
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1234)
    clf = LogisticRegression(penalty='l2',max_iter=10000)
    clf.fit(X_train,y_train)
    y_pred = clf.predict(X_test)
    def accuracy(y_pred, y_test):
    return np.sum(y_pred==y_test)/len(y_test)
    acc = accuracy(y_pred, y_test)
    print(acc)