K-nearest neighbors algorithm
K-nearest neighbors algorithm (K-NN) is a type of instance-based learning, or lazy learning, where the function is only approximated locally and all computation is deferred until function evaluation. It is a non-parametric method used for classification and regression. In both cases, the input consists of the k closest training examples in the feature space. The output depends on whether K-NN is used for classification or regression:
- In K-NN classification, the output is a class membership. An object is classified by a plurality vote of its neighbors, with the object being assigned to the class most common among its k nearest neighbors (k is a positive integer, typically small). If k = 1, then the object is simply assigned to the class of that single nearest neighbor.
- In K-NN regression, the output is the property value for the object. This value is the average of the values of its k nearest neighbors.
K-NN is a type of lazy learning, where the function is only approximated locally and all computation is deferred until classification. The K-NN algorithm is among the simplest of all machine learning algorithms, but its simplicity can lead to high accuracy in many real-world scenarios.
Algorithm[edit]
The K-NN working algorithm is as follows: 1. Load the data 2. Initialize K to your chosen number of neighbors 3. For each example in the data
1. Calculate the distance between the query example and the current example from the data. 2. Add the distance and the index of the example to an ordered collection
4. Sort the ordered collection of distances and indices from smallest to largest (in ascending order) by the distances 5. Pick the first K entries from the sorted collection 6. Get the labels of the selected K entries 7. If regression, return the mean of the K labels 8. If classification, return the mode of the K labels
Choosing the right value of K[edit]
Choosing the right value of K is a critical step in the K-NN algorithm. A smaller value of K means that noise will have a higher influence on the result, and a large value makes it computationally expensive. Data scientists usually choose a K that is not too small and not too large. This can be done through various methods such as cross-validation. A good K can be selected by the square root of n, where n is the total number of data points.
Distance Measures[edit]
The distance measure between two instances can be crucial in the performance of K-NN. Common measures include the Euclidean distance, Manhattan distance, and Minkowski distance. The choice of distance measure depends on the type of data.
Advantages and Disadvantages[edit]
Advantages[edit]
- Simple to understand and implement
- No need for model training or parameter tuning
- Naturally handles multi-class cases
- Can be effective if the training dataset is large
Disadvantages[edit]
- The algorithm gets significantly slower as the number of examples and/or predictors/independent variables increase
- Sensitive to the scale of the data and irrelevant features
- Requires high memory for storing the entire training dataset for prediction
Applications[edit]
K-NN can be used in a variety of applications such as:
See also[edit]

This article is a algorithms or data structures-related stub. You can help WikiMD by expanding it!
-
K-nearest neighbors classification example
-
K-nearest neighbors decision surface animation
-
Border ratio in K-nearest neighbors
-
Types of points in K-nearest neighbors
-
Data with 3 classes for K-nearest neighbors
-
1-nearest neighbor map
-
5-nearest neighbors map
-
Reduced data set for K-nearest neighbors
-
1-nearest neighbor map on reduced data set
Ad. Transform your life with W8MD's Budget GLP-1 injections from $75


W8MD offers a medical weight loss program to lose weight in Philadelphia. Our physician-supervised medical weight loss provides:
- Weight loss injections in NYC (generic and brand names):
- Zepbound / Mounjaro, Wegovy / Ozempic, Saxenda
- Most insurances accepted or discounted self-pay rates. We will obtain insurance prior authorizations if needed.
- Generic GLP1 weight loss injections from $75 for the starting dose.
- Also offer prescription weight loss medications including Phentermine, Qsymia, Diethylpropion, Contrave etc.
NYC weight loss doctor appointmentsNYC weight loss doctor appointments
Start your NYC weight loss journey today at our NYC medical weight loss and Philadelphia medical weight loss clinics.
- Call 718-946-5500 to lose weight in NYC or for medical weight loss in Philadelphia 215-676-2334.
- Tags:NYC medical weight loss, Philadelphia lose weight Zepbound NYC, Budget GLP1 weight loss injections, Wegovy Philadelphia, Wegovy NYC, Philadelphia medical weight loss, Brookly weight loss and Wegovy NYC
|
WikiMD's Wellness Encyclopedia |
| Let Food Be Thy Medicine Medicine Thy Food - Hippocrates |
Medical Disclaimer: WikiMD is not a substitute for professional medical advice. The information on WikiMD is provided as an information resource only, may be incorrect, outdated or misleading, and is not to be used or relied on for any diagnostic or treatment purposes. Please consult your health care provider before making any healthcare decisions or for guidance about a specific medical condition. WikiMD expressly disclaims responsibility, and shall have no liability, for any damages, loss, injury, or liability whatsoever suffered as a result of your reliance on the information contained in this site. By visiting this site you agree to the foregoing terms and conditions, which may from time to time be changed or supplemented by WikiMD. If you do not agree to the foregoing terms and conditions, you should not enter or use this site. See full disclaimer.
Credits:Most images are courtesy of Wikimedia commons, and templates, categories Wikipedia, licensed under CC BY SA or similar.
Translate this page: - East Asian
中文,
日本,
한국어,
South Asian
हिन्दी,
தமிழ்,
తెలుగు,
Urdu,
ಕನ್ನಡ,
Southeast Asian
Indonesian,
Vietnamese,
Thai,
မြန်မာဘာသာ,
বাংলা
European
español,
Deutsch,
français,
Greek,
português do Brasil,
polski,
română,
русский,
Nederlands,
norsk,
svenska,
suomi,
Italian
Middle Eastern & African
عربى,
Turkish,
Persian,
Hebrew,
Afrikaans,
isiZulu,
Kiswahili,
Other
Bulgarian,
Hungarian,
Czech,
Swedish,
മലയാളം,
मराठी,
ਪੰਜਾਬੀ,
ગુજરાતી,
Portuguese,
Ukrainian