ENSPIRING.ai: What is the K-Nearest Neighbor (KNN) Algorithm?
The video provides an in-depth explanation of the K nearest neighbors (KNN) algorithm, renowned for its simplicity and application in classification and regression within machine learning. The KNN algorithm assumes similarity in data points, which can be classified based on their proximity. An example using a fruit dataset explains how the KNN algorithm classifies new data points through consideration of their nearest neighbors on a graph. It highlights the usage of distance metrics, such as euclidean distance, and the importance of selecting the appropriate 'k' value for optimal classification proficiency.
Key strengths and weaknesses of the KNN algorithm are highlighted, underscoring its ease of implementation and adaptability through its capacity to adjust to new data by storing all training data in memory. However, this same attribute contributes to its weakness in scaling, as larger datasets increase computational complexity, making it less efficient. Furthermore, KNN is susceptible to the curse of dimensionality when working with high-dimensional data inputs, challenging its ability to find meaningful neighbors during classification and potentially leading to overfitting or classification errors.
Main takeaways from the video:
Please remember to turn on the CC button to view the subtitles.
Key Vocabularies and Common Phrases:
1. classification [ˌklæsɪfɪˈkeɪʃən] - (noun) - The process of organizing data into categories or groups based on shared characteristics or features. - Synonyms: (categorization, grouping, sorting)
Whether you're just getting started on your journey to becoming a data scientist or you've been here for years, you'll probably recognize the KNN algorithm. It stands for K nearest neighbors, and it's one of the most popular and simplest classification and regression classifiers used in machine learning today.
2. regression [rɪˈɡrɛʃən] - (noun) - A statistical process for estimating the relationships among variables. - Synonyms: (analysis, computation, prediction)
It stands for K nearest neighbors, and it's one of the most popular and simplest classification and regression classifiers used in machine learning today.
3. hyperparameters [ˌhaɪpərˈpærəˌmɛtərz] - (noun) - Outside parameters set before a learning process that are used to train machine learning models. - Synonyms: (configurations, settings, parameters)
It also has only a few hyper parameters, which is a big advantage as well.
4. euclidean distance [juˈklɪdiən ˈdɪstəns] - (noun) - A method for calculating the straight-line distance between two points in space. - Synonyms: (straight-line distance, metric distance, geometric distance)
And this distance serves as our distance metric and can be calculated using various measures, such as euclidean distance or Manhattan distance.
5. voronoi diagrams [ˈvɔrəˌnɔɪ ˈdaɪəˌɡræmz] - (noun) - Geometric constructs that divide a space into regions based on distance to points in a specific subset of that space. - Synonyms: (geometric partitions, tessellations, space divisions)
The distance between the query point and the other data points needs to be calculated, forming decision boundaries and partitioning query points into different regions, which are commonly visualized using Veroni diagrams, which kind of look like a kaleidoscope.
6. dimensionality reduction [daɪˌmɛnʃəˈnælɪti rɪˈdʌkʃən] - (noun) - The process of reducing the number of random variables under consideration by obtaining a set of principal variables. - Synonyms: (data reduction, feature elimination, attribute scaling)
Feature selection and dimensionality reduction techniques can help minimize the curse of dimensionality, but if not done carefully, they can make KNn prone to another downside.
7. curse of dimensionality [kɜrs əv daɪˌmɛnʃəˈnælɪti] - (noun) - A phenomenon where the feature space becomes sparse as dimensions increase, making data analysis increasingly difficult. - Synonyms: (high dimensionality complexity, sparse data issue, dimensionality problem)
Now, KNN also tends to fall victim to something called the curse of dimensionality, which means it doesn't perform well with high dimensional data inputs.
8. overfitting [ˈoʊvərˌfɪtɪŋ] - (noun) - A modeling error that occurs when a machine learning model is too complex and captures noise in the training data, affecting its performance on new data. - Synonyms: (overtraining, model complication, excess fitting)
Lower values of k can overfit the data, whereas higher values of k tend to smooth out the prediction values, since it's averaging the values over a greater area or neighborhood.
9. query point [ˈkwɪri pɔɪnt] - (noun) - A specific data point for which the output needs to be predicted in the context of a learning algorithm. - Synonyms: (target point, data point, focus point)
The distance between the query point and the other data points needs to be calculated, forming decision boundaries...
10. imputation [ˌɪmpjʊˈteɪʃən] - (noun) - The process of replacing missing data with substituted values in data processing. - Synonyms: (substitution, replacement, data compensation)
That's pretty common use case for KNN, and that's because the KNN algorithm is helpful for data sets with missing values, since it can estimate for those values using a process known as missing data imputation.
What is the K-Nearest Neighbor (KNN) Algorithm?
Whether you're just getting started on your journey to becoming a data scientist or you've been here for years, you'll probably recognize the KNN algorithm. It stands for K nearest neighbors, and it's one of the most popular and simplest classification and regression classifiers used in machine learning today. As a classification algorithm, KNN operates on the assumption that similar data points are located near each other and can be grouped in the same category based on their proximity.
So let's consider an example. Imagine we have a dataset containing information about different types of fruit. So let's visualize our fruit data set here. Now, we have each fruit categorized by two things. We have it categorized by its sweetness. That's our x axis here. And then on the y axis, we are classifying it by its crunchiness. Now, we've already labeled some data points, so we've got a few apples here. Apples are very crunchy and somewhat sweet. And then we have a few oranges down here. Oranges are very sweet, not so crunchy.
Now, suppose you have a new fruit that you want to classify. Well, we measure its crunchiness and we measure its sweetness, and then we can plot it on the graph. Let's say it comes out maybe here. The k and n algorithm will then look at the k nearest points on the graph to this new fruit. And if most of these nearest points are classified as apples, the algorithm will classify the new fruit as an apple as well. How's that for an apples to apples comparison?
Now, before a classification can be made, the distance must be defined. And there are only two requirements for a KNN algorithm to achieve its goal, and the first one is what's called the distance metric. The distance between the query point and the other data points needs to be calculated, forming decision boundaries and partitioning query points into different regions, which are commonly visualized using Veroni diagrams, which kind of look like a kaleidoscope. And this distance serves as our distance metric and can be calculated using various measures, such as euclidean distance or Manhattan distance. So that's number one.
Number two is we now need to define the value of k, and the k value in the KNN algorithm defines how many neighbors will be checked to determine the classification of a specific query point. So, for example, if k equals one, the instance will be assigned to the same class as its single nearest neighbor. Choosing the right k value largely depends on the input data. Data with more outliers or noise will likely perform much better with higher values of k. Also, it's recommended to choose an odd number for k to minimize the chances of ties in classification.
Now, just like any machine learning algorithm, KNN has its strengths and it has its weaknesses. So let's take a look at some of those. And on the plus side, we have to say that KNN is quite easy to implement. Its simplicity and its accuracy make it one of the first classifiers that a new data scientist will learn. It also has only a few hyper parameters, which is a big advantage as well. KNN only requires a k value and a distance metric, which is a lot less than other machine learning algorithms.
Also, in the plus category, we can say that it's very adaptable as well meaning as new training samples are added, the algorithm adjusts to account for any new data, since all training data is stored into memory. That sounds good, but there's also a drawback here. And that is. But because of that, it doesn't scale very well. As a dataset grows, the algorithm becomes less efficient due to increased computational complexity comprising compromising the overall model performance. And this inability to scale. It comes from k and n in what's called a lazy algorithm, meaning it stores all training data and defers the computation to the time of classification. That results in higher memory usage and slower processing compared to other classifiers.
Now, KNN also tends to fall victim to something called the curse of dimensionality, which means it doesn't perform well with high dimensional data inputs. So in our sweetness to crunchiness example, this is a 2d space. Its relatively easy to find the nearest neighbors and classify new fruits accurately. However, if we keep adding more features like color and size and weight and so on, the data points become sparse in the high dimensional space, the distances between the points starts to become similar, making it difficult for k and n to find meaningful neighbors.
And it can also lead to something called the peaking phenomenon, where after reaching an optimal number of features, adding more features just increases noise and increases classification errors, especially when the sample size is small. Feature selection and dimensionality reduction techniques can help minimize the curse of dimensionality, but if not done carefully, they can make KNn prone to another downside. And that is the downside of over fitting. Lower values of k can overfit the data, whereas higher values of k tend to smooth out the prediction values, since it's averaging the values over a greater area or neighborhood.
So because of all this, the KNN algorithm is commonly used for simple recommendation systems. So for example, the algorithm can be applied in the areas of data preprocessing. That's pretty common use case for KNN, and that's because the KNN algorithm is helpful for data sets with missing values, since it can estimate for those values using a process known as missing data imputation. Now, another use case is in finance. In the k and n algorithm, it's often used in stock market forecasting, currency exchange rates, trading futures, and money laundering analysis. Money laundering analysis and also we have to consider the use case for healthcare. It's been used to make predictions on the risk of heart attacks and prostate cancer by calculating the most likely gene expressions.
So that's KNN, a simple but imperfect classification and regression classifier. In the right context, its straightforward approach is as delightful as biting into a perfectly classified Apple.
Machine Learning, Technology, Data Science, Knn Algorithm, Distance Metrics, Classification, Ibm Technology
Comments ()