gpt4 book ai didi

python - 使用 sklearn RandomForestClassifier 进行分类

转载 作者:行者123 更新时间:2023-11-30 09:56:51 28 4
gpt4 key购买 nike

我正在使用 Scikit RandomForestClassifier 对不平衡数据进行分类。目标类数据为“1”或“0”(99% 的值为 0)。

我想分配一个权重。我怎样才能做到这一点。

我在文档中发现:

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.

我需要增加“1”的影响力

我应该这样做吗:

s_weight = np.array([100 if i == 1 else 1 for i in y_train]) 

或者这样:

s_weight = y_train[:, 1:100].T.ravel()

.

clf.fit(X_train, y_train, sample_weight=s_weights)

由于我没有得到预期的结果,有人可以确认吗?

最佳答案

技术上

s_weight = np.array([100 if i == 1 else 1 for i in y_train]) 

是正确的,尽管 RF 中的加权并不像 SVM 那样简单。您必须进行交叉验证才能找到最佳权重(可能比 100 小得多)。

关于python - 使用 sklearn RandomForestClassifier 进行分类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23304383/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com