KNN with class weights in SKLearn [closed]

≯℡__Kan透↙ 提交于 2019-12-07 18:18:29

问题


Is it possible to define class weights for a K-nearest neighbour classifier in SKLearn? I have looked at the API but cannot work it out. I have a knn problem which has very imbalanced numbers of classes (10000 of some, to 1 of others).


回答1:


The original knn in sklearn does not seem to offer that option. You can alter the source code though by adding coefficients (weights) to the distance equation such that the distance is amplified for records belonging to the majority class (e.g., with a coefficient of 1.5).

https://github.com/scikit-learn/scikit-learn/blob/7b136e9/sklearn/neighbors/classification.py#L23

Alternatively, the imbalanced-learn module, which is part of scikit-learn-contrib projects, can be used for data sets with high between-class imbalance:

http://contrib.scikit-learn.org/imbalanced-learn/stable/introduction.html

(in case of binary classification, you may alternatively treat the problem as an unsupervised outlier detection problem, and use methods like one-class SVM in sklearn to perform the classification)



来源:https://stackoverflow.com/questions/37876280/knn-with-class-weights-in-sklearn

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!