什么是Spark在写入CSV时对矢量值做了什么?

Eva*_*mir 6 apache-spark pyspark apache-spark-mllib

以下是将LogisticRegression模型中的预测写入json 的一些代码的结果:

    (predictions
        .drop(feature_col)
        .rdd
        .map(lambda x: Row(weight=x.weight,
                           target=x[target],
                           label=x.label,
                           prediction=x.prediction,
                           probability=DenseVector(x.probability)))
        .coalesce(1)
        .toDF()
        .write
        .json(
        "{}/{}/summary/predictions".format(path, self._model.bestModel.uid)))
Run Code Online (Sandbox Code Playgroud)

以下是一个生成JSON对象的示例:

{"label":1.0,"prediction":0.0,"probability":{"type":1,"values":[0.5835784358591029,0.4164215641408972]},"target":"Male","weight":99}
Run Code Online (Sandbox Code Playgroud)

我希望能够将相同的数据输出到CSV文件(最好只用probability.values[0](值数组的第一个元素).但是当我使用与上面相同的代码片段,但替换.json.csv,我得到以下结果:

1.0,0.0,"[6,1,0,0,280000001c,c00000002,af154d3100000014,a1d5659f3fe2acac,3fdaa6a6]",Male,99
Run Code Online (Sandbox Code Playgroud)

第3列发生了什么(一个字符串中引用了一堆值的数组)?

Ale*_*oux 0

“概率”不仅仅是一个向量,它是 json 格式,所以你看到的是一个奇怪的对象序列化。

首先尝试将其转储为字符串

withColumn("probability", col("probability").cast("string"))
Run Code Online (Sandbox Code Playgroud)