0

Blue will be predicted as a result of this case when k is 5 because there are 3 out of 5 blue dots. And, I know how to score the accuracy. But What I want to know is the ratio of each Blue and Red dots like picture below.

enter image description here

Is there any tools to do this in sklearn or tensorflow? or should I make my own k-nn model?

1 Answer 1

3

Sklearn does that ! Check this out. Predict_proba is the function you want.

You will have your probabilities for each class, just multiply it by K to have the actual number you want :

X = [[0], [1], [2], [3]]
y = [0, 0, 1, 1]
from sklearn.neighbors import KNeighborsClassifier

K = 3

neigh = KNeighborsClassifier(n_neighbors=K)
neigh.fit(X, y)

print(neigh.predict([[1.1]]))

predicted = neigh.predict_proba([[0.9]]) # -> [[0.66666667 0.33333333]]

whatYouWant = K*predicted

print(whatYouWant) #-> [[2,1]]
print("Number of 0 : ",whatYouWant[0][0]) # -> Number of 0 :  2.0
print("Number of 1 : ",whatYouWant[0][1]) # -> Number of 1 :  1.0
print("Total : ",sum(whatYouWant[0])) # -> Total :  3.0 which is K
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.