pyspark randomForest功能重要性:如何从列号中获取列名

时间:2017-07-11 02:01:47

标签: pyspark apache-spark-mllib random-forest apache-spark-ml

我在spark中使用标准(字符串索引器+一个热编码器+ randomForest)管道,如下所示

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])

如何将其映射回某些列名或列名+值格式?
基本上是为了获得随机森林的特征重要性以及列名。

3 个答案:

答案 0 :(得分:1)

嘿,为什么不通过列表扩展将其映射回原始列。这是一个例子:

# in your case: trainingData.columns 
data_frame_columns = ["A", "B", "C", "D", "E", "F"]
# in your case: print(cvModel.bestModel.stages[-2].featureImportances)
feature_importance = (1, [1, 3, 5], [0.5, 0.5, 0.5])

rf_output = [(data_frame_columns[i], feature_importance[2][j]) for i, j in zip(feature_importance[1], range(len(feature_importance[2])))]
dict(rf_output)

{'B': 0.5, 'D': 0.5, 'F': 0.5}

答案 1 :(得分:1)

转换后的数据集metdata具有必需的属性。这是一种简单的方法-

  1. 创建一个熊猫数据框(通常功能列表不会很大,因此在存储熊猫DF时不会出现内存问题)

    pandasDF = pd.DataFrame(dataset.schema["features"].metadata["ml_attr"] 
    ["attrs"]["binary"]+dataset.schema["features"].metadata["ml_attr"]["attrs"]["numeric"]).sort_values("idx")
    
  2. 然后创建要映射的广播词典。在分布式环境中广播是必须的。

    feature_dict = dict(zip(pandasDF["idx"],pandasDF["name"])) 
    
    feature_dict_broad = sc.broadcast(feature_dict)
    

您还可以查看herehere

答案 2 :(得分:0)

在ml算法之后,我无法找到任何方法来获取列的真正初始列表,我将此作为当前的解决方法。

print(len(cols_now))

FEATURE_COLS=[]

for x in cols_now:

    if(x[-6:]!="catVar"):

        FEATURE_COLS+=[x]

    else:

        temp=trainingData.select([x[:-7],x[:-6]+"tmp"]).distinct().sort(x[:-6]+"tmp")

        temp_list=temp.select(x[:-7]).collect()

        FEATURE_COLS+=[list(x)[0] for x in temp_list]



print(len(FEATURE_COLS))

print(FEATURE_COLS)

我在所有索引器(_tmp)&上保留了一致的后缀命名。编码器(_catVar)喜欢:

column_vec_in = str_col

column_vec_out = [col+"_catVar" for col in str_col]



indexers = [StringIndexer(inputCol=x, outputCol=x+'_tmp')

            for x in column_vec_in ]


encoders = [OneHotEncoder(dropLast=False, inputCol=x+"_tmp", outputCol=y)

for x,y in zip(column_vec_in, column_vec_out)]



tmp = [[i,j] for i,j in zip(indexers, encoders)]

tmp = [i for sublist in tmp for i in sublist]

这可以进一步改进和推广,但目前这项繁琐的工作效果最好