我知道class_weight
0.17 版本中有一个参数sklearn.ensemble.RandomForestClassifier
。
我无法安装 0.17。如何在 0.14 版本中访问此参数?
或者,是否有另一种方法来处理y values
a 中不平衡的标签 ( ) RandomForestClassifier
?我有一个二元分类器,其负数多于正数,这自然会扭曲结果,因此我想设置类权重来抵消这一点。
我知道class_weight
0.17 版本中有一个参数sklearn.ensemble.RandomForestClassifier
。
我无法安装 0.17。如何在 0.14 版本中访问此参数?
或者,是否有另一种方法来处理y values
a 中不平衡的标签 ( ) RandomForestClassifier
?我有一个二元分类器,其负数多于正数,这自然会扭曲结果,因此我想设置类权重来抵消这一点。
查看源代码,它看起来不像是在0.14
. 或者,您可以对负类进行下采样以获得平衡:
import numpy as np
# Fake class labels skewed toward negative class:
real_p = 0.01 # true probability of class 1 (unknown in real cases)
Y = (np.random.rand(10000) < real_p).astype(np.int)
# Use average number of pos examples as an estimate of P(Y=1)
p = (Y==1).mean()
print "Label balance: %.3f pos / %.3f neg" % (p,1-p)
# Resample the training set:
inds = np.zeros(Y.shape[0], dtype=np.bool)
inds[np.where(Y==1)] = True
inds[np.where(Y==0)] = np.random.rand((Y==0).sum()) < p
resample_p = (Y[inds]==1).mean()
print "After resampling:"
print "Label balance: %.3f pos / %.3f neg" % (resample_p,1-resample_p)
输出:
Label balance: 0.013 pos / 0.987 neg
After resampling:
Label balance: 0.531 pos / 0.469 neg
请注意,这是对负类进行下采样的一种非常简单的方法。更好的方法可能是将下采样或加权集成到学习方案中——也许是增强或级联方法?