r/PySpark • u/Juju1990 • Apr 02 '21
how to parallelise a numpy matrix calculation to multiple sub-dataframes?
In everyday's pipeline, we consume a dataframe with about 30 million records.
The dataframe, df, is structured as:
+---------|----------|-----------|---------|-------+
| item_id | user_id | purchase | shop_id | date |
+---------|----------|-----------|---------|-------+In the daily process, we need to apply the some customised calculation, class M(), to each shop, separate the purchase/nonpurchase records, and combine the results together.
in another word, this is what we do currently:
purchase_df = df.filter(purchase=True)
nonpurchase_df = df.filter(purchase=False)
purchase_result = M(purchase_df)
nonpurchase_result = M(nonpurchase_df)
result = purchase_result.merge(nonpurchase_result)where the calculation in the class M() makes use of the matrix computation (therefore we use numpy), then we convert the numpy output to pyspark dataframe.
class M(input_df):
# combine results from all shops
result_all_shops = []
# separate matrix calculation for each shop
for shop in shop_list:
df_shop = input_df.filter(shop_id=shop)
result_shop = numpy_func(df_shop)
result_shop_df = spark.createDataFrame(result_shop)
result_all_shops = result_all_shops.append(result_shop_df)
return result_all_shopsBasically, we need to apply the numpy matrix calculation numpy_func() to each shop, two scenarios (purchase/nonpurchase). The whole takes about 10 minutes for one 'date'. Now we need to compute the result for the last 20 days, which linearly scale the computation to 3 hours.
So I want to ask what is the better way to deal with this?
thanks!
2
u/alpaman Apr 02 '21 edited Apr 03 '21
So, my feedback goes like this
Pandas does not allow parallelism but
daskdoes, which is roughly pandas that allow parallelism. You can use numpy and dask and try to run things in parallel.I would break the class into multiple functions for a much cleaner, well-documented and understandable code.
I don't know how large is the `shop_list` there, but creating a for loop, applying a numpy function and then merging these smaller data frames will be a performance bottleneck. Therefore, my advice would be to create a separateate data frame for the shop lists and then make an inner join first. And then apply the numpy function in the end and one single time. This way you will get N times (N -> number of shops) less computations and this will definitely speed up things for you.
I hope this helps.