I trained a LogisticRegression model in PySpark (ML package) and the result of the prediction is a PySpark DataFrame (cv_predictions
) (see [1]). The probability
column (see [2]) is a vector
type (see [3]).
[1]
type(cv_predictions_prod)
pyspark.sql.dataframe.DataFrame
[2]
cv_predictions_prod.select('probability').show(10, False)
+----------------------------------------+
|probability |
+----------------------------------------+
|[0.31559134817066054,0.6844086518293395]|
|[0.8937864350711228,0.10621356492887715]|
|[0.8615878905395029,0.1384121094604972] |
|[0.9594427633777901,0.04055723662220989]|
|[0.5391547673698157,0.46084523263018434]|
|[0.2820729747752462,0.7179270252247538] |
|[0.7730465873083118,0.22695341269168817]|
|[0.6346585276598942,0.3653414723401058] |
|[0.6346585276598942,0.3653414723401058] |
|[0.637279255218404,0.362720744781596] |
+----------------------------------------+
only showing top 10 rows
[3]
cv_predictions_prod.printSchema()
root
...
|-- rawPrediction: vector (nullable = true)
|-- probability: vector (nullable = true)
|-- prediction: double (nullable = true)
How do I create parse the vector
of the PySpark DataFrame, such that I create a new column that just pulls the first element of each probability
vector?
This question is similar to, but the solutions in the links below didn't work/weren't clear to me:
How to access the values of denseVector in PySpark
How to access element of a VectorUDT column in a Spark DataFrame?