You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python 3中提取重要性列非0.000000的特征列值至列表

解决方法:提取importance非零对应的feature名称

你遇到的问题很典型——原代码只遍历了importance列的数值,自然只能收集到这些值,没法关联到对应的feature名称。这里给你两种实用的修正方案:

方法1:逐行遍历(直观易懂)

通过iterrows()遍历数据集的每一行,同时获取每行的feature和importance值,判断后收集符合条件的feature:

new_features = []
# 遍历每一行,idx是行索引,row是整行的数据
for idx, row in importance_ranking.iterrows():
    if row['importance'] > 0.0:
        new_features.append(row['feature'])

方法2:布尔索引(高效简洁)

利用pandas的向量化操作直接筛选行,再提取目标列转成列表,这种方法性能更好,尤其适合大数据集:

# 先筛选出importance大于0的行,再提取feature列并转为列表
new_features = importance_ranking[importance_ranking['importance'] > 0.0]['feature'].tolist()

两种方法都能精准收集到你需要的feature名称列表,按需选择就好~

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 09:19:23