apache-spark - “CrossValidatorModel”对象没有属性 'featureImportances'

标签 apache-spark machine-learning pyspark apache-spark-mllib random-forest

我正在尝试提取使用 Pyspark 训练过的随机森林分类器模型的特征重要性。我引用了下面的文章来获取我训练的随机森林模型的特征重要性得分。

PySpark & MLLib: Random Forest Feature Importances

但是,当我使用本文中描述的方法时,出现以下错误

'CrossValidatorModel' object has no attribute 'featureImportances'

这是我用来训练模型的代码

cols = new_data.columns
stages = []
label_stringIdx = StringIndexer(inputCol = 'Bought_Fibre', outputCol = 'label')
stages += [label_stringIdx]
numericCols = new_data.schema.names[1:-1]
assembler = VectorAssembler(inputCols=numericCols, outputCol="features")
stages += [assembler]

pipeline = Pipeline(stages = stages)
pipelineModel = pipeline.fit(new_data)
new_data.fillna(0, subset=cols)
new_data = pipelineModel.transform(new_data)
new_data.fillna(0, subset=cols)
new_data.printSchema()


train_initial, test = new_data.randomSplit([0.7, 0.3], seed = 1045)
train_initial.groupby('label').count().toPandas()
test.groupby('label').count().toPandas()

train_sampled = train_initial.sampleBy("label", fractions={0: 0.1, 1: 1.0}, seed=0)
train_sampled.groupBy("label").count().orderBy("label").show()



labelIndexer = StringIndexer(inputCol='label',
                             outputCol='indexedLabel').fit(train_sampled)

featureIndexer = VectorIndexer(inputCol='features',
                               outputCol='indexedFeatures',
                               maxCategories=2).fit(train_sampled)

from pyspark.ml.classification import RandomForestClassifier
rf_model = RandomForestClassifier(labelCol="indexedLabel", featuresCol="indexedFeatures")

labelConverter = IndexToString(inputCol="prediction", outputCol="predictedLabel",
                               labels=labelIndexer.labels)


pipeline = Pipeline(stages=[labelIndexer, featureIndexer, rf_model, labelConverter])

paramGrid = ParamGridBuilder() \
    .addGrid(rf_model.numTrees, [ 200, 400,600,800,1000]) \
    .addGrid(rf_model.impurity,['entropy','gini']) \
    .addGrid(rf_model.maxDepth,[2,3,4,5]) \
    .build()

crossval = CrossValidator(estimator=pipeline,
                          estimatorParamMaps=paramGrid,
                          evaluator=BinaryClassificationEvaluator(),
                          numFolds=5)    


train_model = crossval.fit(train_sampled)

请帮助解决上述错误并帮助提取特征

最佳答案

这是因为 CrossValidatorModel 没有特征重要性属性,但 RandomForestModel 模型有。

由于您使用PipelineCrossValidator来拟合数据,因此您需要获得最佳拟合的底层阶段型号:

# '2' is the index of your RandomForestModel inside of the Pipeline
your_model = cvModel.bestModel.stages[2] 
var_imp = your_model.featureImportances

关于apache-spark - “CrossValidatorModel”对象没有属性 'featureImportances',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53586875/

相关文章:

java - 如何从行值创建新列

scala - Apache Spark 中的大型 RDD [MatrixEntry] 超出了 GC 开销限制

python - 获取 Pyspark 中缺失评级的评级列表列,其中 0 已到位

apache-spark - 将具有 UTC 偏移量的字符串转换为 Spark 时间戳

java - Apache Spark 简单连接导致神秘错误

apache-spark - 在哪里可以找到 Spark SQL 语法引用?

machine-learning - sklearn.preprocessing中standardscaler和Normalizer的区别

python - 为什么数据清洗会降低准确性?

android - 是否可以在 Android 中运行 python libsvm?

python - 将 PySpark 数据帧写入 MongoDB 插入字段作为 ObjectId