How to implement KNN from scratch with Python

Поділитися
Вставка
  • Опубліковано 28 сер 2024
  • In the first lesson of the Machine Learning from Scratch course, we will learn how to implement the K-Nearest Neighbours algorithm. Being one of the simpler ML algorithms, it is a great way to kick off our deep dive into ML algorithms.
    You can find the code here: github.com/Ass...
    Previous lesson: • Machine Learning From ...
    Next lesson: • How to implement Linea...
    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...
    ▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
    Are KNN and K-means the same thing?
    No. KNN is a supervised learning algorithm whereas K-means is a clustering algorithm.
    #MachineLearning #DeepLearning

КОМЕНТАРІ • 65

  • @afizs
    @afizs Рік тому +31

    Short and simple. I like the way you explained the KNN in simple words.

  • @nizamuddinshaikh3185
    @nizamuddinshaikh3185 Рік тому +6

    Implementing KNN is so easy? That was my first thought after I saw this video. Really the way, it is explained and shown here is remarkable. It not only shows KKN but also how powerful is plain Python when used sensibly with library like Numpy. The entire idea is very useful for beginners like me. I am now AssemlyAI subscriber. I am going to not only see but follow along all videos of this playlist in order to get better understanding of Python, Numpy, Pandas and DataScience. Thank you AssemblyAI for sharing.

  • @l4dybu9
    @l4dybu9 Рік тому +9

    This is the best series to learn ML.
    🎓🔥🔥
    Imma recommend it to all my ml enthusiast friends ✌🏻

  • @semrana1986
    @semrana1986 Місяць тому

    wonderfully done with a lot of clarity

  • @johanriedel
    @johanriedel Рік тому +8

    The free course is appreciated, but I have trouble understanding some of the terms and the thoughts behind certain functions.

  • @mbmathematicsacademic7038
    @mbmathematicsacademic7038 3 місяці тому

    short and simple ,no complications

  • @philtrem
    @philtrem Рік тому +6

    Very easy to follow after I created my own implementation. Very similar to my own implementation, except I elected to use a priority queue to keep track of the k nearest, instead of sort (because having to keep track of indices was a pain, and it was getting late). Coded mine in C# without third party libraries. I like that numpy offers a argsort method here, comes in handy.

  • @bakhtiarrasheed9952
    @bakhtiarrasheed9952 Рік тому +1

    Great tutorial, I also added tie-breaking functionality in case tie occurs in most frequent label.

  • @emir5146
    @emir5146 6 місяців тому +2

    I don't understand that why we add terms that '[0][0]' to the list of most_commons? 8:04

    • @stanvanillo9831
      @stanvanillo9831 6 місяців тому +1

      The counter returns the sorted count of all possible outcomes, i.e. a list of tuples and each tuples has the label and the count, (label, count). You only want the most common one, i.e. the first element in the array and you only want the label, not the count, i.e. you want the first element of that tuple which is also accessed by using [0]. Therefore you need to apply [0][0].

  • @OmarAmil
    @OmarAmil Рік тому +2

    Short and simple, Thank you very much

  • @IgorKuts
    @IgorKuts Рік тому +1

    Nice and concise. Love it.

  • @emir5146
    @emir5146 6 місяців тому +1

    Thank you abla

  • @takasurazeem
    @takasurazeem 9 місяців тому +1

    How can I plot the graph again to see if it turned those blues into the green?

  • @luis96xd
    @luis96xd Рік тому +1

    Whoa, excellent video! It was well explained, thanks! 😁😁👍🤩

  • @Franshpsv
    @Franshpsv 8 місяців тому +1

    I like to follow this course from Lesson 1, what is the link that i need to start here?

  • @cherpysara
    @cherpysara 7 місяців тому +1

    I am getting error no module NAMED KNN .... pl help to resolve this problem.

  • @mrarsenal10vn
    @mrarsenal10vn Рік тому +1

    I love this tutorial so much

  • @uselessgarbagehandler
    @uselessgarbagehandler Рік тому +2

    cheers. still not entirely sure what the output of the Counter method is... i.e. that last array similar to this: [1, 2, 2, 0, 1, 2 ..., 1] etc.
    Could you perhaps explain?

    • @Akiiii009
      @Akiiii009 Рік тому +4

      These are the target labels
      example for reference :
      -- Iris Setosa : 0
      -- Iris Versicolour: 1
      -- Iris Virginica: 2

  • @sedwares
    @sedwares Рік тому +1

    Great video!

  • @imadsaddik
    @imadsaddik Рік тому +1

    Thank you for sharing

  • @urielcalderon1661
    @urielcalderon1661 Рік тому +2

    Thank you

  • @youssefelamrani7905
    @youssefelamrani7905 Рік тому +1

    good job, I like it, KNN doesn't well with images i believe right?

  • @andrea-mj9ce
    @andrea-mj9ce Рік тому +5

    What about the regression case?

    • @KarthickKenny.
      @KarthickKenny. 10 місяців тому

      There is no regression in knn it is a classification algorithm

    • @andrea-mj9ce
      @andrea-mj9ce 10 місяців тому

      @KarthickKenny. One can apply KNN when the response variable is continuous

    • @KarthickKenny.
      @KarthickKenny. 10 місяців тому

      @@andrea-mj9ce you have to apply regression algorithm in that case not knn

  • @esmailsayid2976
    @esmailsayid2976 10 місяців тому

    amazing job

  • @ge_song5
    @ge_song5 Місяць тому

    wow, she knows her stuff.

  • @youssefzaim7163
    @youssefzaim7163 10 місяців тому

    great excahnge ndiro niya

  • @tejasrathi635
    @tejasrathi635 6 місяців тому

    How did you visualised the data ?

  • @alexandergarzo9415
    @alexandergarzo9415 Рік тому +4

    Please explain in more detail every line code.

  • @1000marcelo1000
    @1000marcelo1000 Рік тому

    Amazing!

  • @atharsultan56
    @atharsultan56 7 місяців тому

    numpy error in vscode???

  • @derickkwame6148
    @derickkwame6148 Рік тому

    great simple tutorial but how do i plot a graph with the knn?

  • @SantiagoGomezVideos
    @SantiagoGomezVideos Рік тому

    Great video! Thnk you for making it.
    Got this error in Colab. ModuleNotFoundError: No module named 'KNN' when running from KNN import KNN

    • @AssemblyAI
      @AssemblyAI  Рік тому

      You're welcome Santiago! You should include the KNN python file we develop in the video in the file system of the collab notebook. That should get rid of the error! :)

  • @abheermehrotra329
    @abheermehrotra329 Рік тому

    how to setup my machine with all these libraries ???

  • @tebibusolomon1529
    @tebibusolomon1529 6 днів тому

    i love you

  • @orioncloud4697
    @orioncloud4697 Рік тому

    Thx, however, this euclidean distance function needs to be corrected.

    • @osviiii
      @osviiii Рік тому

      It's actually ok I'd say

    • @orioncloud4697
      @orioncloud4697 Рік тому

      @@osviiii yeap i checked that, i just confused a little

  • @systemforge
    @systemforge Рік тому

    nice

  • @sidekick3rida
    @sidekick3rida Рік тому +1

    How to implement knn from scratch… import numpy and sklearn ¯\_(ツ)_/¯

  • @lalalalaal7209
    @lalalalaal7209 Рік тому

    Are you Turkish

  • @firstlast-wz9jv
    @firstlast-wz9jv Рік тому +1

    thank you for the practice... but it's an exact copy from this one ua-cam.com/video/ngLyX54e1LU/v-deo.html created 4 years ago

    • @AssemblyAI
      @AssemblyAI  Рік тому +2

      Yes! Pat works with me too, we decided to do a new run of his videos :)

  • @KemalDirican-f2o
    @KemalDirican-f2o 20 годин тому

    Lewis Brenda Walker James Jackson Charles

  • @GeorgeZoto
    @GeorgeZoto Рік тому

    I like this approach, it is so helpful. Curious how it compares with sklearn's version of sklearn.neighbors.KNeighborsClassifier 😃

    • @michaelduffy5309
      @michaelduffy5309 Рік тому

      I went through all of these Assembly AI lessons, making each one work perfectly. Then I redid each one using Scikit Learn classes. In every case, I was able to drop in the sklearn equivalent and get the same or better results. A good entree into Scikit Learn.