KNN Algorithm In Machine Learning | KNN Algorithm Using Python | K Nearest Neighbor | Simplilearn
VloĆŸit
- Äas pĆidĂĄn 7. 07. 2024
- đ„AI Engineer Masters Program (Discount Code - YTBE15): www.simplilearn.com/masters-i...
đ„AI & Machine Learning Bootcamp(US Only): www.simplilearn.com/ai-machin...
đ„Professional Certificate Course In AI And Machine Learning by IIT Kanpur (India Only): www.simplilearn.com/iitk-prof...
đ„ Purdue Post Graduate Program In AI And Machine Learning: www.simplilearn.com/pgp-ai-ma...
đ„AI Engineer Masters Program (Discount Code - YTBE15): www.simplilearn.com/masters-i...
This KNN Algorithm in Machine Learningtutorial will help you understand what is KNN, why do we need KNN, and how KNN algorithm works using Python. You will learn how do we choose the factor 'K', when do we use KNN, with proper hands on demonstration to predict whether a person will have diabetes or not, using the KNN algorithm.
Below topics are explained in this K-Nearest Neighbor Algorithm (KNN Algorithm) tutorial:
00:00 Introduction to KNN(K Nearest Neighbor)
00:57 Why do we need KNN?
02:33 What is KNN?
03:51 How do we choose the factor 'K'?
05:46 When do we use KNN?
06:42 How does the KNN algorithm work?
09:19 Use case - Predict whether a person will have diabetes or not?
Dataset Link - drive.google.com/drive/folder...
â Subscribe to our Channel to learn more about the top Technologies: bit.ly/2VT4WtH
â© Check out the Machine Learning tutorial videos: bit.ly/3fFR4f4
You can also go through the slides here: goo.gl/XP6xcp
#KNNAlgorithmInMachineLearning #KNNAlgorithm #KNN #KNearestNeighbor #KNNMachineLearning #KNNAlgorithmPython #KNearestNegighborMachineLearning #MachineLearningAlgorithm #MachineLearning #Simplilearn
When Do We Use the KNN Algorithm?
The KNN algorithm is used in the following scenarios:
â Data is labeled
â Data is noise-free
â Dataset is small, as KNN is a lazy learner
Pros and Cons of Using KNN
â Pros: Since the KNN algorithm requires no training before making predictions, new data can be added seamlessly, which will not impact the accuracy of the algorithm.
KNN is very easy to implement. There are only two parameters required to implement KNN-the value of K and the distance function (e.g. Euclidean, Manhattan, etc.)
â Cons: The KNN algorithm does not work well with large datasets. The cost of calculating the distance between the new point and each existing point is huge, which degrades performance.
Feature scaling (standardization and normalization) is required before applying the KNN algorithm to any dataset. Otherwise, KNN may generate wrong predictions.
âĄïž About Post Graduate Program In AI And Machine Learning
This AI ML course is designed to enhance your career in AI and ML by demystifying concepts like machine learning, deep learning, NLP, computer vision, reinforcement learning, and more. You'll also have access to 4 live sessions, led by industry experts, covering the latest advancements in AI such as generative modeling, ChatGPT, OpenAI, and chatbots.
â Key Features
- Post Graduate Program certificate and Alumni Association membership
- Exclusive hackathons and Ask me Anything sessions by IBM
- 3 Capstones and 25+ Projects with industry data sets from Twitter, Uber, Mercedes Benz, and many more
- Master Classes delivered by Purdue faculty and IBM experts
- Simplilearn's JobAssist helps you get noticed by top hiring companies
- Gain access to 4 live online sessions on latest AI trends such as ChatGPT, generative AI, explainable AI, and more
- Learn about the applications of ChatGPT, OpenAI, Dall-E, Midjourney & other prominent tools
â Skills Covered
- ChatGPT
- Generative AI
- Explainable AI
- Generative Modeling
- Statistics
- Python
- Supervised Learning
- Unsupervised Learning
- NLP
- Neural Networks
- Computer Vision
- And Many MoreâŠ
đ Learn More At: www.simplilearn.com/artificia...
đ„đ„ Interested in Attending Live Classes? Call Us: IN - 18002127688 / US - +18445327688
đ„AI & Machine Learning Bootcamp(US Only): www.simplilearn.com/ai-machine-learning-bootcamp?KNNInMLMachineLearning&Comments&
đ„Professional Certificate Course In AI And Machine Learning by IIT Kanpur (India Only): www.simplilearn.com/iitk-professional-certificate-course-ai-machine-learning?AugustTubebuddyExpPCPAIandML&Comments&
đ„ Purdue Post Graduate Program In AI And Machine Learning: www.simplilearn.com/pgp-ai-machine-learning-certification-training-course?KNNInMLMachineLearning&Comments&
đ„AI Engineer Masters Program (Discount Code - YTBE15): www.simplilearn.com/masters-in-artificial-intelligence?SCE-AIMasters&Comments&
We hope you enjoyed watching our video. The link for the dataset used in the video is provided in the description. Thanks!
I guess im randomly asking but does any of you know of a trick to log back into an instagram account..?
I somehow forgot the account password. I appreciate any help you can offer me
I've been trying to understand this for weeks and you've summed it up in the first 2 minutes. Light bulb moment!
Glad you found it useful! Thank you for watching!
Incredible video. I used it when I first heard of KNN to better understand it, and just used it to create my first model. You all are the best!
Thank you for the appreciation. You can check our videos related to various technologies and subscribe to our channel to stay updated with all the trending technologies.
Thank you for sharing this video it was very clear in detailed manner with example.
But I have a doubt in whether I to should take squareroot of n (n = Sum of Output of Distance Function) for calculating K.
5:11 (Choosing K)
8:55 (Choosing K=3)
My professor took 1 hour to clear the basic concepts of KNN but I was unable to understand. Thanks for clearing my concepts in just under 15 minutes. Thanks a lot. Really appreciated. I am going to subscribe your channel. Thanks once again.
Thank you for the appreciation. You can check our videos related to various technologies and subscribe to our channel to stay updated with all the trending technologies.
These tutorials are easy to understand and informative compared to other videosđThanks:)
You're welcome đ
This video really helped my overall understanding of KNN and refreshed my understanding of the euclidean distance formula. Subscribed!
Hey Aaron, thank you for appreciating our work. We are glad to have helped. We are happy to receive a new subscriber and we hope you are loving our community. We put up new videos on all important topics, so stay tuned. Cheers!
This is an amazing video! I am trying to help my niece and have never read anything about KNN in my life but the way this video explains it is simply awesome! So thankful to you for creating this video as it would help thousands of students and those family members that want to help them learn properly. Do not understand why some professors can't seem to explain it so simply as you have! God bless you man!
WooHoo! We are so happy you love our videos. Please do keep checking back in. We put up new videos every week on all your favorite topics. Whenever you have the time, you must also check out our blog page @simplilearn.com and tell us what you think. Have a good day!
you put it better than I did. I have been struggling to understand what KNN means. Had over 6 lecturers mentioning it in the class but it still sounded vague. But he just made my day with this video.
Great video, up until the point where you skipped the part where you show how to train the algorithm. One could argue thats the most important part...
Keep learning with us .Stay connected with our channel and team :) . Do subscribe the channel for more updates : )
Thank you so much for such wonderful tutorial ! Can't express how grateful I am for this. Everything is very nicely explained.
Hey, thank you for appreciating our work. We are glad to have helped. Do check out our other tutorial videos and subscribe to us to stay connected. Cheers :)
I really really love how you broke down the topic and able to pass across all valuable information in a short while. Thank you
Thank you for choosing us as your learning partner. We are thrilled to hear that you enjoyed your experience with us! If you are looking to expand your knowledge further, we invite you to explore our other courses in the description box.
Thanks alot that's very helpful, but when trying to use StandardScaler an error occurs
"ValueError: could not convert string to float"
i can't solve it , ahat shall i do ?
thanks in advance.
i know im late but anyways it occurs because string data cant be standaradized i.e. put into StandardScaler. comvert it to float value
try doing this:
df[column_name] = df[column_name].astype('float')
(i didnt try it myself but it should hopefull solve the issue)
Thanks for a *fantastic* video!!! may I ask - when determining K, why do you do the sqrt of y_test, rather than y_train (or x_train, which is the same length). In the video, it looks like you intended to do that, then for some reason - changed it...
Many thanks team, this leaa than 30mins clip saved me a couple of days to learn similar thing from some books and articles. Fantastic job :)
Glad it was helpful!
best tutorial of KNN i've ever watched. It helps me a lot
Glad you enjoyed our video! We have a ton more videos like this on our channel. We hope you will join our community!
20:32 Correction - standard scaler does not restrict the range of data between -1 to +1 . It converts the data to a mean of 0 and standard deviation of 1 . So if u take the mean of a standardised column ul find it equals 0. It basically skews the data to a smaller range and makes it comparable with other variables with different magnitudes which otherwise would not have been comparable. Min-max scaler (normalisation) restricts the data between 0 to +1.
true, the formula is normalisedX= (X-mean)/deviation
whats a confusion matrix?
In the field of machine learning and specifically the problem of statistical classification, a confusion matrix, also known as an error matrix, is a specific table layout that allows visualization of the performance of an algorithm, typically a supervised learning one. Hope that helps!
If you're confused about confusion matrix, you should check out zero r and one r classifier.
I found this fascinating. Wish machine learning/data analysis was around when I was doing my CS masters degree. Lots of spare time to learn in retirement. Great video, and I loved the Python even more. Iâve always found the correct working code to be the true documentation.
Glad it was helpful!
Wonderful explaination of the code which many of the other channels lack in their videos. THank you so much for explaining each step...
Hey Abhishek, thank you for appreciating our work. We are glad to have helped. Do check out our other tutorial videos and subscribe to us to stay connected. Cheers :)
Best data science video ever. so detailed and explanatory. very good for beginners. Please keep making detailed videos like this
We are glad you found our video helpful. Like and share our video with your peers and also do not forget to subscribe to our channel for not missing video updates. We will be coming up with more such videos. Cheers!
I'm totally new to any machine learning or programming stuff, and tbh I was super scared about learning -'K-NN algorithm' because the word 'algorithm' already sounded scary enough. However, your analogy of "Cats or dogs" and "claws and ears" REALLY MADE A LOT OF SENSE! If I have to lecture my own class about machine learning someday for other beginners, I think your "cat and dog" analogy of explanation cannot be more simpler. Thanks for the great video!
Glad you enjoyed our video! We have a ton more videos like this on our channel. We hope you will join our community!
This is the best explanation I have seen so far. Thank you.
You're very welcome!
This is such a great tutorial! Thank you so much!
We are glad you found our video helpful, Scott. Like and share our video with your peers and also do not forget to subscribe to our channel for not missing video updates. We will be coming up with more such videos. Cheers!
One of the best explanations I have come across, thank you so much!
Glad it was helpful!
Hey this is really an amazing video! I was looking for this type of video and to my luck i found this!!
Hey Vandan, thank you for appreciating our work. We are glad to have helped. Do check out our other tutorial videos and subscribe to us to stay connected. Cheers :)
Thank you for this illustrative and informative video. Excellent job explaining KNN and a practical real life Python application example.
WooHoo! We are so happy you love our videos. Please do keep checking back in. We put up new videos every week on all your favorite topics. Whenever you have the time, you must also check out our blog page @simplilearn.com and tell us what you think. Have a good day!
This was really good compared to others, thanks i'll keep looking for more. Cheers!
Awesome, thank you!
Thank you very much for this tutorial, it has been very clear, it has helped me a lot for my first Machine Learning model. Awesome!!
Glad it helped!
Wow, your way of explaining and your didactic examples and materials are simply awesome!!. You got a new subscriber. Thank you, and please keep posting your amazing work.
We appreciate your kind comment. Welcome aboard!
Thanks for the video! simple yet detail explanation!
Hey, thank you for appreciating our work. We are glad to have helped. Do check out our other tutorial videos and subscribe to us to stay connected. Cheers :)
Such great tutorial, keep making such great video's. Thank you !!!
Hey Maltesh, thank you for appreciating our work. We are glad to have helped. Do check out our other tutorial videos and subscribe to us to stay connected. Cheers :)
Amazing video, everything I expected was there, thank you.
Glad you enjoyed it! Thank you for watching!
This is really simple as the name of the channel suggests, thanks
Glad you think so! Thank you for watching!
Brief and very clear! Thanks!
Hey Denis, thank you for appreciating our work. We are glad to have helped. Do check out our other tutorial videos and subscribe to us to stay connected. Cheers :)
Good video. Clearly explained all the concept and Diabetes example is neat! Thanks!
Hey Aditya, thank you for appreciating our work. We are glad to have helped. Do check out our other tutorial videos and subscribe to us to stay connected. Cheers :)
Amazing video. I'm learning data science and this helps a lot!
Glad it was helpful!
Thank you for sharing this in very A nice and clear explanation for the KNN Algorithm with use case.
Appreciated !!
Hey Tejas, thank you for appreciating our work. We are glad to have helped. Do check out our other tutorial videos and subscribe to us to stay connected. Cheers :)
My uni teacher made knn sucha scary thing to me... thanks God i found this vid...i m in love with knn now
Happy to help
Very nice explanation .....awesome visualization.
Hello, thank you for watching our video. We are glad that you liked our video. Do subscribe and stay connected with us. Cheers :)
Thank you so much for this video, your way of explanation is really great, i really admire it. Thank you very much again
You are welcome!
Great tutorial. Thank you!
Super useful and informative, thanks!
Hey, thank you for watching our video. We appreciate the kind comment. Do subscribe and stay connected with us. Cheers :)
Thank you for this great course. Very well explained. I subscribe :)
Awesome, thank you for subscribing to our channel.
Thank you for such great explanation !!!
You are welcome!
That was awesome:) liked and subbed Thanks a lot :)
Hey, thank you for appreciating our work. We are glad to have helped. Do check out our other tutorial videos and subscribe to us to stay connected. Cheers :)
For someone that is just hoping into Data science, this video is absolutely magnificent and loved how you go step by step on data preparation and KNN algorithm. Is there any way that I might get the dataset? Congrats for the video.
Hello Luis, thanks for viewing our tutorial. It would be helpful if you will provide your email ID to us so that we could send the requested dataset promptly. On the off chance that you need your email ID to be kept hidden from others, we can do that also. Hope that helps.
what an amazing video , thank you!
You're so welcome!
Very nice explanation!!
Glad it helped!
Before replacing the zero's by the mean in the columns you can first test is there is NanN in the data, if there is not you dont need the first line in the for loop I guess,
data[column]=data[column].replace(0,NanN).
A simple command is: dataset.isnull().sum()
Kudos mate Wonderful Video!
Glad you enjoyed it
Very good video easy way to understand.
Glad it was helpful!
Finally ! easiest video i foundđ .Thanks simplilearn!!!
Hello, thank you for watching our video. We are glad that you liked our video. Do subscribe and stay connected with us. Cheers :)
cool tutorial. keep it up!
Thanks, you too!
Awesome explanation...thank you so much for the video
Hey Kumari, thank you for appreciating our work. We are glad to have helped. Do check out our other tutorial videos and subscribe to us to stay connected. Cheers :)
Thank you alot for your work
Our pleasure!
Really helpful video, simple examples and explanation
Great that you liked the tutorial, Nealabh! If you agree that Machine Learning is a good career move, please refer to this, www.simplilearn.com/big-data-and-analytics/machine-learning-certification-training-course. Also, Subscribe to our channel by clicking on the bell icon for never missing another update. Cheers !
Thanks a lot for explaining very clearly.
Glad it was helpful!
awesome Tutorial...Thank you soo much
Hi Munaza, we are glad you found our video helpful and awesome. Like and share our video with your peers and also do not forget to subscribe to our channel for not missing video updates. We will be coming up with more such videos. Cheers!
Simple and efficient, thumbs up
Glad you like it!
What a gem!!! Very clear and logic. I just have a question of k (k=11). Why using the sqrt(len(X_test))-1, not the sqrt(len(X_train))-1. Thanks!!!!
Keep learning with us .Stay connected with our channel and team :) . Do subscribe the channel for more updates : )
If a give an input list for the KNN algorithm to predict the classes of each element, How can I print out the list of inputs only belonging to a particular class?
Thank You ! Best explanation with a simplistic example. Can I ask how could we improve the efficiency of the model ??
Hi Redamichael,
There are multiple methods to improve the accuracy of any model. Some of them are feature engineering, feature selection, algorithm tuning, and ensemble methods.
Thanks! A simple but very good turorial!
Hey Madhvapathi, thank you for appreciating our work. We are glad that you liked our video. Do subscribe and stay connected with us. Cheers :)
pls provide the data set
cant we replace 0 directly with mean?
diff b.w fit, transform ,fit_transform when to use which?
why k should be even?
This is so amazing, thank you đ
You're so welcome!
Hi there, thanks for your video. it helps me alot in studing ML. I have one doubt, Why do you choose only glucose, BloodPressure, skinthickness, insulin and BMI?
Hi Joshi, thanks for checking out our tutorial and tossing out your queries. Of course, there are a lot of factors affecting the actual result but we have chosen the most important and obvious factors for a clear understanding of the concepts used and to not confuse the audience with medical jargons. Thank you.
amazing video. I learned a lot and it was very clear and informative
Glad it was helpful!
If I preprocess my data using a standard scaler, how will I format my data when I'm doing real time predictions?
Hi, Thank you for sharing the tutorial. After reading and watching instructions from different resources, I have a question regarding on making predictions. That is,
How do I make the prediction when I have a new dataset with no info. in the outcome column?
For example, from a logistic regression analysis, I know there is a coefficient value for each valid predictor. I can use those coefficients to predict new cases. But I don't understand how to implement KNN except I know that (from your video example), after feeding training data, I have 82% of accuracy on predicting test data.
Thank you in advance.
Hi William, thanks for checking out our tutorial. KNN works on the simple principle based on minimum distance from the query object to the training samples to determine the nearest neighbours (K), after which we predict the query object based on the majority of closest neighbours. Hope that helps.
Replacing zeros using pandas
zero_not_accepted = ['Glucose','BloodPressure','SkinThickness','BMI','Insulin']
print(diabetes_data[zero_not_accepted].mean())#to know values of mean
diabetes_data[zero_not_accepted] = diabetes_data[zero_not_accepted].replace(0, diabetes_data[zero_not_accepted].mean())
superb explanation
Thank you so much đ
Great video!!! just one question: i didnt understand what part of the video was the 'k' you chose?
Great video.
Thanks for the visit!
omg thank you for explaining the code
You are welcome! Do subscribe to our channel and stay tuned.
Thank you !
You're welcome!
very brief explanation thank you so much for that.
I have a doubt when you divide the dataset into train and test data sets with the class labels.
we apply K-NN function on train data set with the class labels I mean scaling then after we use test data set to a prediction right but here we don't take test dataset class labels.
is it right if not just let me know?
Thanks a lot for the video, its very helpful. I have a question on how KNN is effected by imbalanced data. It would be great if you could if you answer me.
Thanks in advance.
Examples here a great... I appreciate you
Hey Erin, we're glad you enjoyed this video! If you want to learn more, you can check out this playlist: czcams.com/video/ukzFI9rgwfU/video.html.
And don't forget to like, share and subscribe to our channel! :)"
UBER helpful - well set up and well explained - THANK U
Glad it was helpful!
Can i use k-nn if i have like 48 features? Or was it too much? If so, what type of ML method would you recommend for it?
You'd rather use random forest for this.
Great video. Thanks.
Thanks, Chakradhar :) Like and share our video with your peers and also do not forget to subscribe to our channel for not missing video updates. We will be coming up with more such videos. Cheers!
Thank you for the nice and crystal clear explanation. I have one doubt over here in choosing the K value for given dataset. how it come to 11 ?
As you taught 'K' Value should be sqrt of data points given which are 768.
you cannot train against your entire dataset. You need to reserve a subset for testing/verification.
Thank you for the explanation it was a great help... I have a little question though.. I have a dataset that contains some missings value and I want to replace those values usin the knn... And I was wondering if I should just separate them from the dataset and train the knn usin the rest of the dataset, and then predict their valuesor is there any other way (without separating the dataset)
Thanks a lot that's video is very useful my concept
Hey Abubakar, thank you for appreciating our work. We are glad to have helped. Do check out our other tutorial videos and subscribe to us to stay connected. Cheers :)
loved it
great tutor
Hi, we appreciate the kind comment! enjoy!
SIr, Extremely useful
Glad to hear that! Thank you for watching!
Is there any way we can see what impact independent variables have on dependent variables like in linear regression? For example, if we were looking at the price of a car with price being dependent and mpg being independent, we can find that for every unit increase in mpg, a car's price goes up by $100.
Is there a similar way to find the coefficients from our KNN model like we can with models like linear regression?
Very helpful :)
Glad you think so! Thank you for watching!
For choosing k, we are taking sqrt(n). what if the data points are 1000 the sqrt(1000) approx 31. Allocating k=31 is too much for the resources. Any other suggestions on this?
hello, that's a great tutorial, thanks for the effort. I don't know if I missed it but I can't seem to find where you trained the model, or that step is not really important?!
It is our pleasure! Check out :www.simplilearn.com/tutorials/machine-learning-tutorial/knn-in-python for more in-depth tutorials! Happy learning!
what to do if we have six datasets and how to merge them? and between the datasets, there are some similarities between them and not some new features were introduced too. can you give me any suggestions regarding this matter?
hey there is an error when i try to run this command
Y_pred=knn.predict(X_test)
Y_pred
this is the error, Notfittederror
Nice tutorial. How you have selected the list item for zero_not_accepted? Why the "Age" column is not taken for the list item?
Thank you Simplilearn team for the clear explanation. Can you please provide the dataset and the python notebook used in the video?
Hello Aisha, thanks for viewing our tutorial and we hope it is helpful. It would be helpful if you will provide your email ID to us so that we could send the requested dataset promptly.
Nice explanation and neat code. Awseome video
Greetings! Thank you for your kind words. Spread the word by liking, sharing and subscribing to our channel! Cheers :).
nice video but could you guys show us how we can insert the new data into the model and the model itself will classify it.
Hey ,we'll go into these implementations in detail in another video. Stay tuned!
Thanks so much , :) :)
You're welcome!
Two Questions:
1. How do you embed a KNN model into a real application?
2. If you train a model on scaled data, how do you implement the resulting model to any platform for real use?
Thanks,
"Hey, we hope you enjoyed this video! If you want to learn more, you can check out this linl: www.simplilearn.com/tutorials/machine-learning-tutorial/knn-in-python
And don't forget to like, share and subscribe to our channel! :)"
facing problem while doing replacing zeroes to mean...
error is 'int' object is not subscriptable