有没有办法为xgboost型分类器设置不同的类权重?例如,在sklearn RandomForestClassifier中,这是由"class_weight“参数完成的。
发布于 2017-02-28 21:45:36
当使用sklearn包装器时,有一个weight参数。
示例:
import xgboost as xgb
exgb_classifier = xgboost.XGBClassifier()
exgb_classifier.fit(X, y, sample_weight=sample_weights_data)
其中参数应该是类似数组的长度N,等于目标长度
发布于 2018-06-28 09:04:45
我最近遇到了这个问题,所以我想留下一个我尝试过的解决方案
from xgboost import XGBClassifier
# manually handling imbalance. Below is same as computing float(18501)/392318
on the trainig dataset.
# We are going to inversely assign the weights
weight_ratio = float(len(y_train[y_train == 0]))/float(len(y_train[y_train ==
1]))
w_array = np.array([1]*y_train.shape[0])
w_array[y_train==1] = weight_ratio
w_array[y_train==0] = 1- weight_ratio
xgc = XGBClassifier()
xgc.fit(x_df_i_p_filtered, y_train, sample_weight=w_array)
不确定,为什么,但结果相当令人失望。希望这对某些人有帮助。
发布于 2020-08-05 22:43:24
这里的答案已经过时了。不再支持THe sample_weight参数。它被scale_pos_weight所取代
相反,scale_pos_weight =sum(负实例)/sum(正实例)
https://stackoverflow.com/questions/42192227
复制相似问题