这是在pyspark上进行乘法的正确方法吗?

问题描述 投票:0回答:1

pyspark新手。这是我的代码:

def sparkApp():
    spark = SparkSession \
        .builder \
        .appName("Python Spark SQL basic example") \
        .config("spark.sql.catalogImplementation", "hive") \
        .config("spark.executor.memory", "4g") \
        .config("spark.driver.memory", "16g") \
        .config("spark.executor.instances", "5") \
        .config("spark.executor.cores", "5") \
        .getOrCreate()
    return spark

def my_f(x, w):
 return np.array(x).dot(w).sum()

w = [1,2]
x = sparkApp().("select x1, x2 from my_table")
x.rdd.map(lambda row: my_f(row, w)

我的问题是: 1.我知道这可以并行化x的读数,但是它会并行化x和w的乘法吗?如果是这样,它会返回与输入相同的索引中的值吗?如果没有,我怎么能并行运行呢? 2.我播放w还是将其作为参数传递?

谢谢

python-2.7 apache-spark pyspark apache-spark-mllib
1个回答
0
投票

你的方法实际上并不是一个dot产品。相反,你的答案只返回(x1 + x2)* w。它确实使用并行性,但效率不高。

如果要手动计算点积,可以创建包含x和w元素的对RDD。像[(x1,w1),(x2,w2)]这样的东西,然后并行化对RDD对中每个元素的计算产品的过程,然后将结果相加。

最后一个选项是使用模块pyspark.ml.linalg中的点函数。如果从spark数据帧/ rdd / dataset访问矢量,它应该使用spark提供的并行性。

© www.soinside.com 2019 - 2024. All rights reserved.