+-
python – 在sklearn中使用RandomForestClassifier进行不平衡分类
我有一个数据集,其中类是不平衡的.类为’1’或’0′,其中类’1’:’0’的比例为5:1.你如何计算每个类的预测误差和相应的重新平衡权重在sklearn中随机森林,类似于以下链接: http://www.stat.berkeley.edu/~breiman/RandomForests/cc_home.htm#balance
最佳答案
您可以将样本权重参数传递给Random Forest fit method

sample_weight : array-like, shape = [n_samples] or None

Sample weights. If None, then samples are equally weighted. Splits
that would create child nodes with net zero or negative weight are
ignored while searching for a split in each node. In the case of
classification, splits are also ignored if they would result in any
single class carrying a negative weight in either child node.

在旧版本中,有一个preprocessing.balance_weights方法可以为给定的样本生成平衡权重,从而使类变得均匀分布.它仍在那里,在内部但仍然可用的preprocessing._weights模块,但已弃用,将在未来版本中删除.不知道具体原因.

更新

一些澄清,因为你似乎很困惑. sample_weight使用很简单,一旦你记住它的目的是平衡训练数据集中的目标类.也就是说,如果你有X作为观察值而y作为类(标签),那么len(X)== len(y)== len(sample_wight),并且样本的每个元素一维数组代表相应的权重(观察,标签)对.对于您的情况,如果1个类被表示为0类,那么0类是平衡类分布,您可以使用简单

sample_weight = np.array([5 if i == 0 else 1 for i in y])

为所有0个实例分配权重5,为所有1个实例分配权重1.请参阅上面的链接,了解更加狡猾的balance_weights权重评估功能.

点击查看更多相关文章

转载注明原文:python – 在sklearn中使用RandomForestClassifier进行不平衡分类 - 乐贴网