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

plot_tree参数报错:feature_names需为list或None类型求助

解决plot_tree的feature_names参数类型错误

直接修改代码,把X_encoded.columns转为列表即可:

plt.figure(figsize=(15,7.5))
plot_tree(clf_dt, filled=True, rounded=True, class_names=["No HD","Yes HD"], 
          feature_names=list(X_encoded.columns)) 

原因说明

X_encoded.columns返回的是pandas的Index对象,而plot_tree的feature_names参数明确要求必须是**列表(list)**或者None。用list()包装后,就能把Index对象转换成符合要求的普通列表,解决类型不匹配的问题。

你可以验证转换后的类型:

print(type(list(X_encoded.columns)))  # 输出 <class 'list'>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 00:32:37