为模型选择变量的方法

Method to choose variable for model

我有 200 万人口和 700 个变量(其中有许多空值、零或 -9999),我为此开发了一个 python 模型,其工作方式如下

我生成了整个人口和变量的数据框

我去掉了不需要的变量(ID,名字例如)

我对数据进行分区并用 ks_2samp (scipy.stats) 计算其指标,然后过滤掉那些指标非常低的指标 (roc, ks)

然后,我获取所有变量的相关性并再次过滤。

紧接着,我使用 Xgboost 生成模型。使用 shap.TreeExplainer,我得到了最终留在我的模型中的所有变量的重要性(大约 90 个变量)

虽然我减少了变量的数量,但还是太多了。有谁知道有什么方法可以继续删除变量?我的想法是最多获取30个变量。

您可以使用函数找到具有最少特征的高精度最佳模型。

伪代码

1. Create model with features n.
2. Measure model's objective or accuracy for example.
3. Save accuracy, and features used.
4. If number of features is only 30 goto step 8.
5. Get feature importance.
6. Drop the feature with lowest value
7. Goto step 1.
8. Show saved accuracy and features and select what you want,
like a high accuracy but more features or a not so bad accuracy with less features.

您还可以使用 optuna 或其他超参数调谐器。它将尝试通过准确性(或您想要的其他 objective)找到最佳模型,确定要使用的特征以及要使用的特征数量。