You need to enable JavaScript to run this app. 处理模型高相关特征时如何通过索引获取对应特征的名称
优惠活动
大模型
产品
解决方案
定价
更多

处理模型高相关特征时如何通过索引获取对应特征的名称

实现方法
  • 若特征原始数据存储为Pandas DataFrame
    直接调用DataFrame的columns属性按索引取值即可,示例代码如下:
    # 你需要移除的特征索引
    remove_idx = [0, 3, 5, 6, 8, 9, 10, 17]
    # 假设X是你训练模型用的特征DataFrame
    remove_feature_names = X.columns[remove_idx]
    
  • 若特征存储为Numpy数组,单独存储了特征名列表
    直接遍历索引列表从特征名列表中取值即可:
    # 假设你预先定义了和特征数组列顺序一一对应的特征名列表
    feature_names = ["特征1", "特征2", "特征3", ...]
    remove_idx = [0, 3, 5, 6, 8, 9, 10, 17]
    remove_feature_names = [feature_names[i] for i in remove_idx]
    
  • 若你使用了Scikit-learn的特征处理管道(如ColumnTransformer、OneHotEncoder等)
    先调用管道的get_feature_names_out()方法获取处理后的全量特征名列表,再按索引取值:
    # 假设preprocessor是你定义的特征预处理管道/转换器
    processed_feature_names = preprocessor.get_feature_names_out()
    remove_idx = [0, 3, 5, 6, 8, 9, 10, 17]
    remove_feature_names = [processed_feature_names[i] for i in remove_idx]
    

拿到remove_feature_names后如果需要直接过滤特征,调用X.drop(columns=remove_feature_names)即可得到剔除高相关特征后的数据集。

内容的提问来源于stack exchange,提问作者LdM

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.21 01:54:05