Question

The KNeighborsClassifier has a method for predicting class probabilities. However, I cannot find any documentation describing how these probabilities are computed.

Here is a toy example that returns class probabilites:

from sklearn.neighbors import KNeighborsClassifier
import numpy as np
N = 100
np.random.seed(1)
X = np.random.random((N,2))
Y = np.random.randint(2, size=N)
model = KNeighborsClassifier(5, weights='distance')
model.fit(X, Y)
print(model.predict_proba([[0.5, 0.5]]))
#>>> [[0.55486525 0.44513475]]

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with datascience.stackexchange
scroll top