python - 如何找到决策树分类的特征名称?
问题描述
我正在尝试查找决策树的特征信息。更具体地说,如果 183 出现在我的树形可视化中,我希望能够分辨出它是什么特征。我试过 dtModel.getInputCol() 但收到以下错误。
AttributeError: 'DecisionTreeClassificationModel' object has no attribute 'getInputCol'
这是我当前的代码:
from pyspark.ml.classification import DecisionTreeClassifier
# Create initial Decision Tree Model
dt = DecisionTreeClassifier(labelCol="label", featuresCol="features", maxDepth=3)
# Train model with Training Data
dtModel = dt.fit(trainingData)
display(dtModel)
如果您可以提供帮助或需要更多信息,请告诉我。谢谢你。
解决方案
请参阅此示例取自Spark 文档(我尝试使名称与您的代码一致,尤其是featuresCol="features"
)。
我假设您有一些这样的代码(在您在问题中发布的代码之前):
featureIndexer = VectorIndexer(inputCol="inputFeatures", outputCol="features", maxCategories=4).fit(data)
在此步骤之后,您拥有"features"
索引功能,然后您将其提供给DecisionTreeClassifier
(就像您发布的代码一样):
# Train a DecisionTree model.
dt = DecisionTreeClassifier(labelCol="indexedLabel", featuresCol="features")
你要找的是inputFeatures
上面的,这是被索引之前的原始特征。如果要打印它,只需执行以下操作:
sc.parallelize(inputFeatures, 1).saveAsTextFile("absolute_path")
推荐阅读
- excel - VBA,自动填充系列而不是复制单元格
- kubernetes - 如何将现有的 kubernetes 清单文件添加到 Helm Chart
- javascript - 一部分是先编译的
- canvas - 如何在fabricjs中添加图案以适合圆圈?
- python - 职员匹配查询不存在
- python - 如何在 Fast API Python 中读取 URL 参数和正文类型
- react-admin - React-admin:登录后useQuery未在仪表板上获取数据
- java - Java Swing GridLayout 看起来不像我期望的那样
- powershell - 在 Windows power shell 中,如何提取属性文件值并将其保存到 env var?
- node.js - MongoDB 聚合,获取一组值并获取它们的数量