'How can i change Rdd to Vectors.dense pyspark
Im new to pyspark I need to change my rdd :
tfidf.collect()
output:
[('fuel', 0.06190145817054232),
('months', 0.03095072908527116),
('lasting', 0.03095072908527116),
('noticeably', 0.03095072908527116),
('gravitational', 0.06190145817054232),
('minor', 0.03095072908527116),
('mass', 0.03095072908527116),
('apollo', 0.03095072908527116),
('missions', 0.03095072908527116),
('possible', 0.03095072908527116),
('perturbations', 0.03095072908527116),
('quite', 0.03095072908527116),
('crash', 0.03095072908527116),
('mapped', 0.03095072908527116),
('irregular', 0.03095072908527116),
('field', 0.06190145817054232),
('none', 0.03095072908527116),
('earth', 0.03095072908527116),
('issues', 0.03095072908527116),
('altitudes', 0.03095072908527116),
('know', 0.03095072908527116),
('big', 0.03095072908527116),
('problem', 0.03095072908527116)]
To something similar to this that i found in another exemple:
#this is a whole other exemple
data = [(Vectors.dense([0.0, 1.0, 0.0, 7.0, 0.0]),), (Vectors.dense([2.0, 0.0, 3.0, 4.0, 5.0]),), (Vectors.dense([4.0, 0.0, 0.0, 6.0, 7.0]),)]
i have no idea how to make tfidf rdd to a similar data
Solution 1:[1]
Use RDD's map
from pyspark.ml.linalg import Vectors
tfidf.map(lambda x: (x[0], Vectors.dense(x[1]))).collect()
[('fuel', DenseVector([0.0619])),
('months', DenseVector([0.031])),
('lasting', DenseVector([0.031])),
('noticeably', DenseVector([0.031])),
('gravitational', DenseVector([0.0619])),
('minor', DenseVector([0.031])),
('mass', DenseVector([0.031])),
('apollo', DenseVector([0.031])),
('missions', DenseVector([0.031])),
('possible', DenseVector([0.031])),
('perturbations', DenseVector([0.031])),
('quite', DenseVector([0.031])),
('crash', DenseVector([0.031])),
('mapped', DenseVector([0.031])),
('irregular', DenseVector([0.031])),
('field', DenseVector([0.0619])),
('none', DenseVector([0.031])),
('earth', DenseVector([0.031])),
('issues', DenseVector([0.031])),
('altitudes', DenseVector([0.031])),
('know', DenseVector([0.031])),
('big', DenseVector([0.031])),
('problem', DenseVector([0.031]))]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | pltc |
