K-Nearest Neighbors
Drag the target point to see how the KNN algorithm classifies it based on proximity.
Parameters
Visualization
Real-time Analysis
Result: Based on the 5 nearest neighbors, the target is classified as:
Drag the target point to see how the KNN algorithm classifies it based on proximity.
Result: Based on the 5 nearest neighbors, the target is classified as:
The K-Nearest Neighbors (KNN) algorithm is one of the simplest and most intuitive classification algorithms in machine learning. Its core idea is based on the saying, "You are known by the company you keep." To classify a new, unknown data point, KNN looks at the 'K' closest data points from the training set and makes a prediction based on a majority vote. This lab lets you explore this process interactively.
Imagine you have a new student (the target point) and you want to predict which class they belong to. KNN doesn't learn a "model" in the traditional sense. Instead, it follows a simple, lazy procedure at prediction time.
First, the algorithm calculates the distance (usually Euclidean distance) from the new target point to every single point in the training data. It then identifies the 'K' points that are closest to the target—these are its "nearest neighbors." In the visualization, a circle is drawn around the target to show the neighborhood, and lines connect to the K neighbors within it.
Next, the algorithm looks at the classes of these K neighbors and holds a vote. Each neighbor gets one vote for its class. The class with the most votes wins, and that becomes the prediction for the new target point. The "Neighbor Votes" panel on the right shows this process in real-time.
The most crucial parameter in KNN is 'K'. Let's see how it affects the outcome.
The K-Nearest Neighbors (KNN) algorithm is one of the simplest and most intuitive classification algorithms in machine learning. Its core idea is based on the saying, "You are known by the company you keep." To classify a new, unknown data point, KNN looks at the 'K' closest data points from the training set and makes a prediction based on a majority vote. This lab lets you explore this process interactively.