KNN with class weights in SKLearn [closed]

给你一囗甜甜゛ 提交于 2019-12-06 01:53:02

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)

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