We can view nearest neighbor as a voting process where we consult our nearest neighbor.

We give the -th data point a voting weight .

In your example, each data point in class has weight and each data point in class has weight . There are votes from class and votes from class . We give class a score of and class a score of . Class has a higher score, hence we assign it to class .

Answer from Siong Thye Goh on Stack Exchange
🌐
Visual Studio Magazine
visualstudiomagazine.com › articles › 2019 › 04 › 01 › weighted-k-nn-classification.aspx
Weighted k-NN Classification Using Python -- Visual Studio Magazine
For example, you might want to predict the political party affiliation (democrat, republican, independent) of a person based on their age, annual income, gender, years of education and so on.
🌐
Medium
medium.com › @lakshmiteja.ip › understanding-weighted-k-nearest-neighbors-k-nn-algorithm-3485001611ce
Understanding Weighted k-Nearest Neighbors (k-NN) Algorithm | by Lakshmi Teja Illuri | Medium
September 26, 2023 - On the other hand, weighted k-NN introduces the concept of assigning different weights to neighbors based on their proximity to the query point, which can lead to improved performance.
🌐
Univr
profs.sci.univr.it › ~bicego › papers › 2016_ICPR.pdf pdf
Weighted K-Nearest Neighbor Revisited M. Bicego University of Verona
Fig. 1: Example of (a) K-Nearest Neighbor and (b) Weighted · K-Nearest Neighbor (K = 3). With KNN, every neighbor
🌐
GeeksforGeeks
geeksforgeeks.org › machine learning › weighted-k-nn
Weighted K-NN - GeeksforGeeks
July 12, 2025 - Predict the class of the query point, using distance-weighted voting. The v represents the class labels. Use the following formula Implementation: Consider 0 as the label for class 0 and 1 as the label for class 1. Below is the implementation of weighted-kNN algorithm.
🌐
YouTube
youtube.com › watch
9. Weighted KNN Solved example Weighted K-Nearest Neighbors (KNN) Classification Vidya Mahesh Huddar - YouTube
9. Weighted KNN Solved example Weighted K-Nearest Neighbors (KNN) Classification by Vidya Mahesh HuddarConsider the student performance training dataset of 8...
Published: May 28, 2026
🌐
Open Access LMU
epub.ub.uni-muenchen.de › 1769 › 1 › paper_399.pdf pdf
Hechenbichler, Schliep: Weighted k-Nearest-Neighbor Techniques and Ordinal
One special combination of these two extensions, a weighted mean estimation, builds the connection to the local regression technique LOESS and especially · to the Nadaraya-Watson estimator. Both are nicely summarized for example · in Chen et al. (2004) and Cleveland and Loader (1995). 1 · After a short description of the common kNN classification method in section 2, we introduce our weighted technique in section 3 and the extension to ordinal ·
🌐
James D. McCaffrey
jamesmccaffreyblog.com › home › weighted k-nearest neighbors classification example using python
Weighted k-Nearest Neighbors Classification Example Using Python - James D. McCaffreyJames D. McCaffrey
July 25, 2025 - # weighted_knn.py # k-nearest neighbors demo # Anaconda3-2020.02 (Python 3.7.6) import numpy as np def dist_func(item, data_point): sum = 0.0 for i in range(2): diff = item[i] - data_point[i+1] # no ID sum += diff * diff return np.sqrt(sum) def make_weights(k, distances): result = np.zeros(k, dtype=np.float32) sum = 0.0 for i in range(k): result[i] += 1.0 / distances[i] sum += result[i] result /= sum return result def show(v): print("idx = = (%3.2f %3.2f) class = - " \ % (v[0], v[1], v[2], v[3]), end="") def main(): print("\nBegin weighted k-NN demo \n") print("Normalized income-education data looks like: ") print("[id = 0, 0.32, 0.43, class = 0]") print(" .
Find elsewhere
🌐
Wikipedia
en.wikipedia.org › wiki › K-nearest_neighbors_algorithm
k-nearest neighbors algorithm - Wikipedia
August 27, 2026 - In statistics and machine learning, the k-nearest neighbors algorithm (k-NN) is a non-parametric supervised learning method that assigns weightage only to the k (number of) nearest neighbors of an entity in making a decision about the entity. It is used both in classification -- where a new example ...
🌐
University of Washington
courses.washington.edu › ling572 › winter2017 › teaching_slides › class5_kNN.pdf pdf
K nearest neighbor LING 572 Fei Xia 1
Weighted voting allows us to use more training examples: e.g., wi = 1/dist(x, xi) We can use all the training examples. 16 · Summary of kNN algorithm · • Decide k, feature weights, and similarity · measure · • Given a test instance x · – Calculate the distances between x and all the ·
🌐
Medium
vishvaasswaminathan.medium.com › weighted-knn-algorithm-c43b400346bf
Weighted KNN Algorithm. Weighted k-NN is a modified version of… | by Vishvaasswaminathan | Medium
September 28, 2023 - Any function can be used as a kernel function for the weighted knn classifier whose value decreases as the distance increases.
🌐
YouTube
youtube.com › watch
Machine Learning | Weighted KNN - YouTube
Weighted kNN is a modified version of k nearest neighbours. One of the many issues that affect the performance of the kNN algorithm is the choice of the hype...
Published: March 28, 2020
🌐
RStudio
rstudio-pubs-static.s3.amazonaws.com › 248376_c3c699140f4b4d7ba3f3e496dcedc99b.html
Instance-Based Learning: kNN, weighted kNN
November 2, 2016 - There are several modifications to this algorithms - for example, distance weighted knn and attribute weighted knn.
🌐
Medium
medium.com › analytics-vidhya › feature-engineering-experiment-weighted-knn-3f28dfdf30e1
Feature Engineering Experiment- Weighted KNN | by Abhijeet Pokhriyal | Analytics Vidhya | Medium
March 15, 2020 - For all the observations in the dataset it calculates similarity between each observation and all other observations. How it differs from plain vanilla KNN is that the similarity is weighted.
🌐
ResearchGate
researchgate.net › publication › 266872328_A_New_Distance-weighted_k_-nearest_Neighbor_Classifier
(PDF) A New Distance-weighted k -nearest Neighbor Classifier
November 30, 2011 - In this paper, we develop a novel Distance-weighted k -nearest Neighbor rule (DWKNN), using the dual distance-weighted function. The proposed DWKNN is motivated by the sensitivity problem of the selection of the neighborhood size k that exists in k -nearest Neighbor rule (KNN), with the aim of improving classification performance.
🌐
MDPI
mdpi.com › 1996-1073 › 12 › 5 › 916
Application of the Weighted K-Nearest Neighbor Algorithm for Short-Term Load Forecasting
March 9, 2019 - It has been widely applied in real-world problems, such as analyzing the structure of the stock market [14], fault detection and diagnosis for photovoltaic systems [15], and social images recognition in social networks [16]. In addition, several ...
🌐
GitHub
github.com › MNoorFawi › weighted-knn-in-python
GitHub - MNoorFawi/weighted-knn-in-python: Predict house prices using Weighted KNN Algorithm with KDTree for faster nearest neighbors search in Python. · GitHub
def inverseweight(dist, num = 1.0, const = 0.1): return num / (dist + const) def gaussian(dist, sigma = 10.0): return math.e ** (- dist ** 2 / ( 2 * sigma ** 2)) def subtractweight(dist, const = 2.0): if dist > const: return 0.001 else: return const - dist def weighted_knn(kdtree, test_point, target, k = 25, weight_fun = inverseweight): nearest_dist, nearest_ind = kdtree.query(test_point, k = k) avg = 0.0 totalweight = 0.0 for i in range(k): dist = nearest_dist[0][i] idx = nearest_ind[0][i] weight = weight_fun(dist) avg += weight * target[idx] totalweight += weight avg = round(avg / totalweight) return avg def testalgorithm(algo, kdtree, testset, target, test_target): error = 0.0 for row in range(len(testset)): guess = algo(kdtree, testset[row].reshape(1, -1), target) error += (test_target[row] - guess) ** 2 return round(np.sqrt(error / len(testset)))
Starred by 10 users
Forked by 2 users
Languages: Python