【问题标题】:dot product of a combination of elements of an RDD using pySpark使用 pySpark 的 RDD 元素组合的点积
【发布时间】:2017-06-30 15:46:37
【问题描述】:

我有一个 RDD,其中每个元素都是一个形式为

的元组
[ (index1,SparseVector({idx1:1,idx2:1,idx3:1,...})) , (index2,SparseVector() ),... ]

我想通过使用mllib.linalg.SparseVector 类提供的 SparseVector1.dot(SparseVector2) 方法对该 RDD 中的每个值进行点积。我知道 python 有一个itertools.combinations 模块,可用于实现要计算的点积组合。有人可以提供一个 code-sn-p 来实现同样的目标吗?我只能做一个RDD.collect(),所以我收到了RDD中所有元素的列表,然后在这个列表上运行itertools.combinations,但根据我的理解,这将在根上执行所有计算并且不会分布式本身。有人可以建议一种更分散的方式来实现这一点吗?

【问题讨论】:

    标签: apache-spark pyspark rdd combinatorics


    【解决方案1】:
    def computeDot(sparseVectorA, sparseVectorB):
        """
        Function to compute dot product of two SparseVectors
        """
        return sparseVectorA.dot(sparseVectorB)
    
    # Use Cartesian function on the RDD to create tuples containing 
    # 2-combinations of all the rows in the original RDD
    
    combinationRDD = (originalRDD.cartesian(originalRDD))
    
    # The records in combinationRDD will be of the form 
    # [(Index, SV1), (Index, SV1)], therefore, you need to
    # filter all the records where the index is not equal giving
    # RDD of the form [(Index1, SV1), (Index2, SV2)] and so on,
    # then use the map function to use the SparseVector's dot function
    
    dottedRDD = (combinationRDD
                 .filter(lambda x: x[0][0] != x[1][0])
                 .map(lambda x: computeDot(x[0][1], x[1][1])
                 .cache())
    

    这个问题的解决方案应该沿着这条路线。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-04
      • 1970-01-01
      • 2021-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-18
      • 1970-01-01
      相关资源
      最近更新 更多