【问题标题】:PySpark - SparseVector Column to MatrixPySpark - SparseVector 列到矩阵
【发布时间】:2023-03-26 09:27:01
【问题描述】:

我对使用 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 数据帧转换为矩阵。

【问题讨论】:

  • 没有一个答案好到可以接受,或者至少投票有用??

标签: python pyspark apache-spark-sql


【解决方案1】:

转换为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())

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2015-12-04
      • 2021-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-07
      • 2019-05-28
      • 2017-03-26
      • 1970-01-01
      相关资源
      最近更新 更多