我使用的是标准的(字符串索引+一个热编码器+随机森林)火花管道得到的列名,如下图所示pyspark随机森林功能的重要性:如何从列编号
labelIndexer = StringIndexer(inputCol = class_label_name, outputCol="indexedLabel").fit(data)
string_feature_indexers = [
StringIndexer(inputCol=x, outputCol="int_{0}".format(x)).fit(data)
for x in char_col_toUse_names
]
onehot_encoder = [
OneHotEncoder(inputCol="int_"+x, outputCol="onehot_{0}".format(x))
for x in char_col_toUse_names
]
all_columns = num_col_toUse_names + bool_col_toUse_names + ["onehot_"+x for x in char_col_toUse_names]
assembler = VectorAssembler(inputCols=[col for col in all_columns], outputCol="features")
rf = RandomForestClassifier(labelCol="indexedLabel", featuresCol="features", numTrees=100)
labelConverter = IndexToString(inputCol="prediction", outputCol="predictedLabel", labels=labelIndexer.labels)
pipeline = Pipeline(stages=[labelIndexer] + string_feature_indexers + onehot_encoder + [assembler, rf, labelConverter])
crossval = CrossValidator(estimator=pipeline,
estimatorParamMaps=paramGrid,
evaluator=evaluator,
numFolds=3)
cvModel = crossval.fit(trainingData)
现在经过适合我可以使用cvModel.bestModel.stages[-2].featureImportances
获得随机森林和特征重要性,但是这不会给我功能/列名称,而只是功能号码。
下面我得到的是:
print(cvModel.bestModel.stages[-2].featureImportances)
(1446,[3,4,9,18,20,103,766,981,983,1098,1121,1134,1148,1227,1288,1345,1436,1444],[0.109898803421,0.0967396441648,4.24568235244e-05,0.0369705839109,0.0163489685127,3.2286694534e-06,0.0208192703688,0.0815822887175,0.0466903663708,0.0227619959989,0.0850922269211,0.000113388896956,0.0924779490403,0.163835022713,0.118987129392,0.107373548367,3.35577640585e-05,0.000229569946193])
我该如何映射回一些列名或列名+值格式?
基本上可以获得随机森林和列名的重要性。
是的,但是您错过了在stringindexer/onehotencoder之后列名更改的点。由汇编程序组合的那个,我想映射到它们。我肯定可以做到这一点,但我更关心spark(ml)是否有一些更短的方式,像scikit学习一样:) – Abhishek
啊好吧我的坏。但是你们长远来说还是有效的。我不认为目前存在短期解决方案。 Spark ML API不像scikit学习的那样强大和冗长。 –
是的,我知道:),只是想保持这个问题的建议:)。谢谢Dat – Abhishek