Page 345 - AI Ver 3.0 Class 11
P. 345
Steps Involved in KNN
To better understand the working of KNN algorithm, apply the following steps when using it:
Step 1: Load both the training and test datasets.
Step 2: Select the number of nearest data points (K), which can be any integer.
Step 3: Choose a distance metric, such as Euclidean or Manhattan distance, to determine the closeness among data points.
Step 4: Arrange the dataset in ascending order based on the distance values and determine the K-Nearest Neighbors.
Step 5: Determine the total amount of data points in every category among these K neighbors.
Step 6: Assign the new data point to the category having the highest number across its immediate neighbors.
Step 7: The model is prepared and ready for usage.
Applications of KNN
Some applications of KNN are as follows:
• Image recognition: KNN may be used to categorise photographs depending on their attributes, such as pixel
values and colour, etc. KNN may compare the attributes of a picture with those of labelled images in the set used
for training and classify the majority of its K-Nearest Neighbors.
• Spam detection: KNN can identify spam emails through the comparison of new emails to a database containing
both spam and non-spam emails.
• Medical diagnosis: KNN can forecast diseases using patient information such as symptoms, medical history, and
medical test findings. Patient information can be expressed as feature vectors, which KNN can use to produce
forecasts by comparing them to those individuals with recognised diagnosis.
• Financial forecasting: KNN may be employed to anticipate stock prices and market movements using the past
information. KNN can discover trends in previous data and forecast future market behaviour.
• Anomaly detection: Anomaly detection uses KNN to detect data points which are substantially distinct from the
remaining portion of the data. It identifies points as anomalies when they are significantly different from their
K-Nearest Neighbors.
• Recommendation systems: KNN can enhance recommendation systems by identifying comparable people or things.
• Customer segmentation: KNN may divide customers into groups according to purchase habits, demographic
information, or other characteristics, allowing for more focused marketing campaigns.
Advantages of KNN
Some advantages of KNN are as follows:
• KNN modeling doesn't have a training period because the data itself serves as the model for future predictions.
This makes it very efficient for quickly using the available data.
• KNN is also very easy to implement. The main task is to calculate the distance between different points based on
their features. This can be done easily using distance formulas like Euclidean or Manhattan.
• Since there's no training period, new data can be added at any time without affecting the model.
Limitations of KNN
Some disadvantages of KNN are as follows:
• KNN is not good for large datasets because calculating distances for every data instance is very time-consuming.
Machine Learning Algorithms 343

