【发布时间】:2020-07-16 12:29:57
【问题描述】:
使用 XGBoost 特征重要性我得到了我的数据帧 X_train 的特征重要性。我的 X_train 最初有 49 个功能。 XGBoost 特征重要性告诉我,在这 49 个特征中,每个特征的重要性得分是多少。现在我想知道在我的机器学习模型中要使用多少特征。各种阈值如每个特征对应的阈值数组中所述。我想知道包含功能应该采取的最低阈值。我应该包括所有高于 0.3 或 0.4 分数等的功能吗?但是我收到一个错误:
from numpy import sort
from sklearn.feature_selection import SelectFromModel
xgb_model = xgb.XGBClassifier(max_depth=5, learning_rate=0.08, n_jobs=-1).fit(X_train, y_train)
thresholds = sort(xgb_model.feature_importances_)
所有特征的阈值如下:
[IN]thresholds
[OUT] array([0. , 0. , 0. , 0. , 0. ,
0. , 0. , 0. , 0. , 0. ,
0. , 0. , 0. , 0. , 0. ,
0.00201289, 0.00362736, 0.0036676 , 0.00467797, 0.00532952,
0.00591741, 0.00630169, 0.00661084, 0.00737418, 0.00741502,
0.00748773, 0.00753344, 0.00773079, 0.00852909, 0.00859741,
0.00906814, 0.00929257, 0.00980796, 0.00986394, 0.01056027,
0.01154695, 0.01190695, 0.01203871, 0.01258377, 0.01301482,
0.01383268, 0.01390096, 0.02001457, 0.02699436, 0.03168892,
0.03543754, 0.03578222, 0.13946259, 0.48038903], dtype=float32)
仅选择最重要的特征并创建包含相同特征的数据框 select_X_train 的功能。
for thresh in thresholds:
# select features using threshold
selection = SelectFromModel(xgb_model, threshold=thresh, prefit=True)
select_X_train = selection.transform(X_train)
# train model
selection_model = XGBClassifier()
selection_model.fit(select_X_train, y_train)
# eval model
select_X_test = selection.transform(X_test)
y_pred = selection_model.predict(select_X_test)
predictions = [round(value) for value in y_pred]
accuracy = accuracy_score(y_test, predictions)
print("Thresh=%.3f, n=%d, Accuracy: %.2f%%" % (thresh, select_X_train.shape[1], accuracy*100.0))
我遇到以下错误:
----> 4 select_X_train = selection.transform(X_train)
KeyError: 'weight'
没有按名称权重的列。如何解决此错误。
预期输出
Thresh=0.00201289, n=33, Accuracy: 77.95%
#33 features with threshold above 0.002
Thresh=0.00362736, n=34, Accuracy: 76.38%
#34 features with threshold above 0.003
Thresh=0.0036676 , n=35, Accuracy: 77.56%
#35 features with threshold above 0.003 and so on
所以基本上采用每个阈值并运行 XGBoost 并计算具有指定最小阈值分数的所有特征的准确度。 例如,在第一种情况下,XGBoost 将考虑所有得分至少为 0.00201289 的特征,并计算准确度。将考虑阈值至少为 0.003 及以上的下一个特征,依此类推。
【问题讨论】: