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
相关产品推荐
相关产品推荐

