Bry*_*ind 3 python apache-spark-sql pyspark
我对使用 PySpark 很陌生。我的 PySpark 数据框中有一列 SparseVectors。
rescaledData.select('features').show(5,False)
Run Code Online (Sandbox Code Playgroud)
rescaledData.select('features').show(5,False)
Run Code Online (Sandbox Code Playgroud)
我需要将上述数据帧转换为矩阵,其中矩阵中的每一行都对应于数据帧中该行中的 SparseVector。
例如,
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|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]) |
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+
Run Code Online (Sandbox Code Playgroud)
必须转换为
+-----------------+
|features |
+-----------------+
|(7,[1,2],[45,63])|
|(7,[3,5],[85,69])|
|(7,[1,2],[89,56])|
+-----------------+
Run Code Online (Sandbox Code Playgroud)
我已经阅读了下面的链接,它表明有一个功能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()
Run Code Online (Sandbox Code Playgroud)
我需要它将每一行转换为数组,然后将 PySpark 数据帧转换为矩阵。
转换为RDD和map:
vectors = df.select("features").rdd.map(lambda row: row.features)
Run Code Online (Sandbox Code Playgroud)
将结果转换为分布式矩阵:
from pyspark.mllib.linalg.distributed import RowMatrix
matrix = RowMatrix(vectors)
Run Code Online (Sandbox Code Playgroud)
如果你想要DenseVectors(内存要求!):
vectors = df.select("features").rdd.map(lambda row: row.features.toArray())
Run Code Online (Sandbox Code Playgroud)
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]]
Run Code Online (Sandbox Code Playgroud)