PySpark - SparseVector Column to Matrix

时间:2017-12-06 10:03:47

标签: python pyspark apache-spark-sql

我是使用PySpark的新手。我的PySpark数据框中有一列SparseVectors。

rescaledData.select('features').show(5,False)
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|features                                                                                                                                                            |
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|(262144,[43953,62425,66522,148962,174441,249180],[3.9219733362813143,3.9219733362813143,1.213923135179104,3.9219733362813143,3.9219733362813143,0.5720692490067093])|
|(262144,[57925,66522,90939,249180],[3.5165082281731497,1.213923135179104,3.9219733362813143,0.5720692490067093])                                                    |
|(262144,[23366,45531,73408,211290],[2.6692103677859462,3.005682604407159,3.5165082281731497,3.228826155721369])                                                     |
|(262144,[30913,81939,99546,137643,162885,249180],[3.228826155721369,3.9219733362813143,3.005682604407159,3.005682604407159,3.228826155721369,1.1441384980134186])   |
|(262144,[108134,152329,249180],[3.9219733362813143,2.6692103677859462,2.8603462450335466])                                                                          |
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+

我需要将上面的数据帧转换为矩阵,其中矩阵中的每一行都对应于数据帧中该行中的SparseVector。

例如,

+-----------------+
|features         |
+-----------------+
|(7,[1,2],[45,63])|
|(7,[3,5],[85,69])|
|(7,[1,2],[89,56])|
+-----------------+

必须转换为

[[0,45,63,0,0,0,0]
[0,0,0,85,0,69,0]
[0,89,56,0,0,0,0]]

我已阅读下面的链接,其中显示有一个函数toArray()可以完全符合我的要求。 https://mingchen0919.github.io/learning-apache-spark/pyspark-vectors.html

但是,我在使用它时遇到了麻烦。

vector_udf = udf(lambda vector: vector.toArray())
rescaledData.withColumn('features_', vector_udf(rescaledData.features)).first()

我需要它将每行转换为数组,然后将PySpark数据帧转换为矩阵。

2 个答案:

答案 0 :(得分:3)

转换为RDDmap

vectors = df.select("features").rdd.map(lambda row: row.features)

将结果转换为分布式矩阵:

from pyspark.mllib.linalg.distributed import RowMatrix

matrix = RowMatrix(vectors)

如果你想要DenseVectors(内存要求!):

vectors = df.select("features").rdd.map(lambda row: row.features.toArray())

答案 1 :(得分:1)

toArray()将返回numpy数组。我们可以转换为列表然后收集数据帧。

from pyspark.sql.types import *
vector_udf = udf(lambda vector: vector.toArray().tolist(),ArrayType(DoubleType()))

df.show() ## my sample dataframe
+-------------------+
|           features|
+-------------------+
|(4,[1,3],[3.0,4.0])|
|(4,[1,3],[3.0,4.0])|
|(4,[1,3],[3.0,4.0])|
+-------------------+

colvalues = df.select(vector_udf('features').alias('features')).collect()

list(map(lambda x:x.features,colvalues))
[[0.0, 3.0, 0.0, 4.0], [0.0, 3.0, 0.0, 4.0], [0.0, 3.0, 0.0, 4.0]]