There is indeed another way, and it's inbuilt into scikit-learn (so should be quicker). You can use the wminkowski metric with weights. Below is an example with random weights for the features in your training set.
knn = KNeighborsClassifier(metric='wminkowski', p=2,
metric_params={'w': np.random.random(X_train.shape[1])})
Answer from piman314 on Stack OverflowVisual 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.
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
05:11
9. Weighted KNN Solved example Weighted K-Nearest Neighbors (KNN) ...
13:13
Distance Weighted K nearest Neighbor Learning Algorithm Discrete ...
03:16
K Nearest Neighbors Part 10 - Weighted KNN - YouTube
03:43
Weighted K Nearest Neighbour || Lesson 60 || Machine Learning || ...
08:12
Weighted K-Nearest Neighbor (W-KNN) | MATLAB - YouTube
14:42
Machine Learning | Weighted KNN - YouTube
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(" .
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
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)))
Stack Overflow
stackoverflow.com › questions › 47149085 › python-knn-weighting-during-predict
scikit learn - Python KNN weighting during .predict()? - Stack Overflow
Later in the script, I collect the signal strengths of the 6 local WIFI router Mac addresses and try to predict the location of my pi using knn.predict() and hope to get the location of the pi, Location1 for example. The results aren't great, it does a relatively poor job of figuring out where it is. I was wondering if there was a way to weight the function of knn.predict() so that the neighbors of the most recent location are weighted more heavily, the pi won't move to the other side of the floor without crossing the other points.
Kenzo's Blog
kenzotakahashi.github.io › k-nearest-neighbor-from-scratch-in-python.html
K-Nearest Neighbor from Scratch in Python
January 5, 2016 - import numpy as np class KNeighborsClassifier(object): def __init__(self, n_neighbors=5): self.n_neighbors = n_neighbors def fit(self, X, y): self.X = X self.y = y return self def _distance(self, data1, data2): return sum(abs(data1 - data2)) def _compute_weights(self, distances): return [(1, y) for d, y in distances] def _predict_one(self, test): distances = sorted((self._distance(x, test), y) for x, y in zip(self.X, self.y)) weights = self._compute_weights(distances[:self.n_neighbors]) return weights def predict(self, X): return [self._predict_one(i) for i in X]
Pysal
pysal.org › libpysal › generated › libpysal.weights.KNN.html
libpysal.weights.KNN — libpysal v4.13.0 Manual
>>> import libpysal >>> import numpy as np >>> points = [(10, 10), (20, 10), (40, 10), (15, 20), (30, 20), (30, 30)] >>> kd = libpysal.cg.KDTree(np.array(points)) >>> wnn2 = libpysal.weights.KNN(kd, 2) >>> [1,3] == wnn2.neighbors[0] True >>> wnn2 = KNN(kd,2) >>> wnn2[0] {1: 1.0, 3: 1.0} >>> wnn2[1] {0: 1.0, 3: 1.0}
Real Python
realpython.com › knn-python
The k-Nearest Neighbors (kNN) Algorithm in Python – Real Python
April 7, 2021 - Here, you test whether it makes sense to use a different weighing using your GridSearchCV. Applying a weighted average rather than a regular average has reduced the prediction error from 2.17 to 2.1634. Although this isn’t a huge improvement, it’s still better, which makes it worth it. ... As a third step for kNN tuning, you can use bagging.
Stack Overflow
stackoverflow.com › questions › 70140382 › how-to-add-weighted-for-knn
python - How to add weighted for KNN? - Stack Overflow
import numpy as np import matplotlib.pyplot as plt from sklearn import neighbors, datasets import pandas as pd from sklearn.model_selection import train_test_split class KNearestNeighbor(): def __init__(self , k): self.k = k def train(self,X,y): self.X_train = X self.y_train = y def predict(self,X_test): distances= self.euclidean_distance(X_test) return self.predict_labels(distances) def euclidean_distance(self,X_test): num_test = X_test.shape[0] num_train= self.X_train.shape[0] distances= np.zeros((num_test,num_train)) for i in range(num_test): for j in range(num_train): distances[i,j]= np.sq
Stack Overflow
stackoverflow.com › questions › 57743955 › setting-the-weights-on-the-knn-classifier
python - Setting the weights on the Knn classifier - Stack Overflow
Copyknn2 = KNeighborsClassifier(n_neighbors = 5, weights=my_weight_function) knn2.fit(X_train, y_train)