I'm trying to index back the prediction probability in a classification prediction in Spark. I have an input data for multiclass classifier with labels red, green, blue.
Input dataframe:
+-----+---+---+---+---+---+---+---+---+---+----+----+----+----+
| _c0|_c1|_c2|_c3|_c4|_c5|_c6|_c7|_c8|_c9|_c10|_c11|_c12|_c13|
+-----+---+---+---+---+---+---+---+---+---+----+----+----+----+
| red| 0| 0| 0| 1| 0| 0| 0| 2| 3| 2| 2| 0| 5|
|green| 5| 6| 0| 14| 0| 5| 0| 95| 2| 120| 0| 0| 9|
|green| 6| 1| 0| 3| 0| 4| 0| 21| 22| 11| 0| 0| 23|
| red| 0| 1| 0| 1| 0| 4| 0| 1| 4| 2| 0| 0| 5|
|green| 37| 9| 0| 19| 0| 31| 0| 87| 9| 108| 0| 0| 170|
+-----+---+---+---+---+---+---+---+---+---+----+----+----+----+
only showing top 5 rows
I use StringIndexer to index the label column and VectorAssembler to create feature vector from feature columns.
Parsed Dataframe:
+-----+--------------------+
|label| features|
+-----+--------------------+
| 1.0|(13,[3,7,8,9,10,1...|
| 0.0|[5.0,6.0,0.0,14.0...|
| 0.0|[6.0,1.0,0.0,3.0,...|
| 1.0|(13,[1,3,5,7,8,9,...|
| 0.0|[37.0,9.0,0.0,19....|
+-----+--------------------+
only showing top 5 rows
A Random Forest Classification model is generated with this data. While querying I will be providing the feature columns to predict the label and its probability.
Query dataframe:
+---+---+---+---+---+---+---+---+---+---+----+----+----+
|_c0|_c1|_c2|_c3|_c4|_c5|_c6|_c7|_c8|_c9|_c10|_c11|_c12|
+---+---+---+---+---+---+---+---+---+---+----+----+----+
| 11| 11| 0| 23| 0| 7| 2| 70| 81| 76| 7| 0| 23|
| 4| 0| 0| 0| 0| 0| 2| 2| 3| 2| 7| 0| 2|
+---+---+---+---+---+---+---+---+---+---+----+----+----+
Parsed query dataframe:
+--------------------+--------------------+
| queryValue| features|
+--------------------+--------------------+
|11,11,0,23,0,7,2,...|[11.0,11.0,0.0,23...|
|4,0,0,0,0,0,2,2,3...|(13,[0,6,7,8,9,10...|
+--------------------+--------------------+
Raw prediction from the RFCModel:
+--------------------+--------------------+--------------------+----------+
| queryValue| features| probability|prediction|
+--------------------+--------------------+--------------------+----------+
|11,11,0,23,0,7,2,...|[11.0,11.0,0.0,23...| [0.67, 0.32]| 0.0|
|4,0,0,0,0,0,2,2,3...|(13,[0,6,7,8,9,10...| [0.05, 0.94]| 1.0|
+--------------------+--------------------+--------------------+----------+
In the raw prediction, probability column is an array of double with probabilities in the coresponding class index. Say if a row in probability column is [0.67,0.32], it means class 0.0 has probability of 0.67 and class 1.0 has probability of 0.32. The probability column makes sense only when the labels are 0,1,2... In this case, when I use IndexToString to index back the predictions to original labels, probability column will make no sense.
Indexed dataframe:
+--------------------+--------------------+--------------------+----------+
| queryValue| features| probability|prediction|
+--------------------+--------------------+--------------------+----------+
|11,11,0,23,0,7,2,...|[11.0,11.0,0.0,23...| [0.67, 0.32]| green|
|4,0,0,0,0,0,2,2,3...|(13,[0,6,7,8,9,10...| [0.05, 0.94]| red|
+--------------------+--------------------+--------------------+----------+
I want to index back probability column like below,
+--------------------+--------------------+--------------------------+----------+
| queryValue| features| probability |prediction|
+--------------------+--------------------+--------------------------+----------+
|11,11,0,23,0,7,2,...|[11.0,11.0,0.0,23...|{"red":0.32,"green":0.67} | green|
|4,0,0,0,0,0,2,2,3...|(13,[0,6,7,8,9,10...|{"red":0.94,"green":0.05} | red|
+--------------------+--------------------+--------------------------+----------+
For now I'm indexing the probability column by converting the dataframe to List. Is there any feature transformer available in spark to do this?