我正在尝试从Spark MLib的随机森林回归器中获取特征重要性。问题是我使用Pipeline
对象进行训练,我不知道如何将此对象转换为RandomForestRegressorModel
来获取featureImportance
。
我的代码中有趣的部分如下:
val rf = new RandomForestRegressor(). setLabelCol( "label" ). setFeaturesCol( "features" ). setNumTrees( numTrees ). setFeatureSubsetStrategy( featureSubsetStrategy ). setImpurity( impurity ). setMaxDepth( maxDepth ). setMaxBins( maxBins ). setMaxMemoryInMB( maxMemoryInMB )val pipeline = new Pipeline().setStages(Array(rf))var model = pipeline.fit( trainingDataCached )// 获取特征重要性val featImp = model.featureImportance
我遗漏了什么?
谢谢。
编辑
这可能是正确的答案吗?
val featImp = model .asInstanceOf[RandomForestRegressionModel] .featureImportances
回答:
这可能是正确的答案吗?
差不多了。
model .stages .head .asInstanceOf[RandomForestRegressionModel] .featureImportances