This should work:

# compute inverses of distances
# suppress division by 0 warning, 
# replace np.inf with a very large number
with np.errstate(divide='ignore'):
    dinv = np.nan_to_num(1 / distances)
    
# an array with distinct class labels
distinct_labels = np.array(list(set(labels)))
# an array with labels of neighbors
neigh_labels = labels[indices]
# compute the weighted score for each potential label
weighted_scores = ((neigh_labels[:, :, np.newaxis] == distinct_labels) * dinv[:, :, np.newaxis]).sum(axis=1)
# choose the label with the highest score
predictions = distinct_labels[weighted_scores.argmax(axis=1)]
Answer from bb1 on Stack Overflow
Starred by 10 users
Forked by 2 users
Languages: Python
🌐
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. In this article I explain how to implement the weighted k-nearest neighbors algorithm using Python.
🌐
Minimatech
minimatech.org › weighted-knn-python
Weighted KNN with Python – Minimatech
December 16, 2020 - 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)))
🌐
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(" .
🌐
GitHub
github.com › florianlenz96 › weighted-knn
GitHub - florianlenz96/weighted-knn: Weighted KNN Algorithm with Python
Weighted KNN Algorithm with Python. Contribute to florianlenz96/weighted-knn development by creating an account on GitHub.
Author: florianlenz96
🌐
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 - 3. Now as in KNN we use the neighbors to predict the label of the of the observation. 4. Based on the predictions we can calculate the performance metrics like accuracy or AUC or Log Loss. This in turn will act as the loss function for our overall Feature Learning Algorithm. ie- We will keep changing the weights using an optimization algorithm like Gradient Descent till we are able to minimize the loss function. Let’s run some experiments in Python- Code is here #github
Find elsewhere
🌐
GitHub
github.com › topics › weighted-knn
weighted-knn · GitHub Topics · GitHub
Weighted K Nearest Neighbors (kNN) algorithm implemented on python from scratch.
🌐
GeeksforGeeks
geeksforgeeks.org › machine learning › weighted-k-nn
Weighted K-NN - GeeksforGeeks
July 12, 2025 - The intuition behind weighted kNN, is to give more weight to the points which are nearby and less weight to the points which are farther away. Any function can be used as a kernel function for the weighted knn classifier whose value decreases ...
🌐
Medium
medium.com › @alexanderaharon › unraveling-the-power-of-weighted-k-nearest-neighbor-cf1dd1a9b5da
Unraveling the Power of Weighted K-Nearest Neighbor: | by Alexanderaharon | Medium
September 25, 2023 - Introduction: In the world of machine ... (KNN) algorithm stands as a simple yet versatile tool for classification and regression tasks. However, did you know that KNN can be enhanced even further with the introduction of weighted K-Nearest Neighbor? In this comprehensive guide, we’ll delve into the theory behind weighted KNN, explore its implementation in Python, and demonstrate ...
🌐
Real Python
realpython.com › knn-python
The k-Nearest Neighbors (kNN) Algorithm in Python – Real Python
April 7, 2021 - Adding Weighted Average of Neighbors Based on Distance · Further Improving on kNN in scikit-learn With Bagging · Comparison of the Four Models · Conclusion · Remove ads · Recommended Course · Using k-Nearest Neighbors (kNN) in Python (57m) In this tutorial, you’ll get a thorough introduction to the k-Nearest Neighbors (kNN) algorithm in Python.
🌐
Stack Overflow
stackoverflow.com › questions › 47149085 › python-knn-weighting-during-predict
scikit learn - Python KNN weighting during .predict()? - Stack Overflow
It's a little bit hacky, but you can do this using the weights parameter in KNeighborsClassifier. If you add your times as an extra feature and then write a custom distance function you can weight the distance between samples using time.
🌐
GitHub
github.com › MNoorFawi › weighted-knn-in-python › blob › master › weighted_knn.py
weighted-knn-in-python/weighted_knn.py at master · MNoorFawi/weighted-knn-in-python
Predict house prices using Weighted KNN Algorithm with KDTree for faster nearest neighbors search in Python. - MNoorFawi/weighted-knn-in-python
Author: MNoorFawi
🌐
scikit-learn
scikit-learn.org › stable › modules › neighbors.html
1.6. Nearest Neighbors — scikit-learn 1.9.1 documentation
This can be accomplished through the weights keyword. The default value, weights = 'uniform', assigns uniform weights to each neighbor. weights = 'distance' assigns weights proportional to the inverse of the distance from the query point.
🌐
Kenwuyang
kenwuyang.com › posts › 2022_11_02_k_nearest_neighbors_knn_classifier_step_by_step_python_implementation_from_scratch
K-Nearest Neighbors (KNN) Classifier: Step-by-Step Python Implementation from Scratch – Yang (Ken) Wu
The proximity of each neighbor can be weighted by its distance to ensure closer neighbors have a greater influence on the prediction. In this post, we’ll break down how to implement this intuitive and flexible algorithm from scratch in Python. In K-Nearest Neighbors (KNN) classification, ...