Hi! Im using sci-kit learn for fit and test a KNN method, and the dataset is unbalanced/desbalanced classes and i need apply weight to class but i dont know the function to use in KNN, in other methods you can use weight=balance_weights(y_train) but in KNN i think you need a function for calculate the distances but i dont know exacly how to the method use this function and how to define correctly the weights for unbalanced problem.

Sorry for my english.

asked Jun 12 '13 at 12:31

byfnk's gravatar image

byfnk
21336


One Answer:

Hi, in scikit's KNN, you can define the weights hyper-parameter of KNeighborsClassifier. One popular weight method for unbalanced datasets is to weight points by their inverse distances to the query point. An example of this is seen below,

neigh = KNeighborsClassifier(n_neighbors=3,weights='distance')

There are several other weight methods for KNN that are explained here,

http://scikit-learn.org/dev/modules/generated/sklearn.neighbors.KNeighborsClassifier.html

Typically, the distances are calculated using the euclidean distance, however, you can use other distance functions by setting the 'p' hyper-parameter of KNeighborsClassifier.

answered Jun 16 '13 at 14:35

Issam%20Laradji's gravatar image

Issam Laradji
1217912

Your answer
toggle preview

powered by OSQA

User submitted content is under Creative Commons: Attribution - Share Alike; Other things copyright (C) 2010, MetaOptimize LLC.