【发布时间】:2018-04-15 05:04:24
【问题描述】:
我目前正在研究一个包含 24,000 个样本的随机森林分类模型,其中 20,000 个属于 class 0,其中 4,000 个属于 class 1。我制作了一个train_test_split,其中 test_set 是整个数据集的0.2(test_set 中的大约 4,800 个样本)。由于我正在处理不平衡的数据,因此我查看了旨在解决此问题的超参数class_weight。
我在设置class_weight='balanced' 时遇到的问题,看看训练集的confusion_matrix,我得到了类似的结果:
array([[13209, 747],
[ 2776, 2468]])
如您所见,下方数组对应False Negative = 2776,后跟True Positive = 2468,而上方数组对应True Negative = 13209,后跟False Positive = 747。问题是样本量属于class 1,根据confusion_matrix是2,776 (False Negative) + 2,468 (True Positive),加起来5,244 samples属于class 1。这没有任何意义,因为整个数据集仅包含 4,000 个属于 class 1 的样本,其中只有 3,200 个位于 train_set 中。看起来confusion_matrix 返回了矩阵的Transposed 版本,因为在training_set 中属于class 1 的样本的实际数量应该在train_set 中总计3,200 个样本,在test_set 中总计800 个样本。一般来说,正确的数字应该是 747 + 2468,总和为 3,215,这是属于class 1 的正确样本数量。
有人可以解释一下我使用class_weight 时会发生什么吗? confusion_matrix 返回矩阵的transposed 版本是真的吗?我看错了吗?
我尝试寻找答案并访问了几个在某种程度上相似的问题,但没有一个真正涵盖了这个问题。
这些是我查看的一些来源:
scikit-learn: Random forest class_weight and sample_weight parameters
How to tune parameters in Random Forest, using Scikit Learn?
using sample_weight and class_weight in imbalanced dataset with RandomForest Classifier
任何帮助将不胜感激,谢谢。
【问题讨论】:
标签: python machine-learning scikit-learn random-forest confusion-matrix