【发布时间】:2018-05-19 13:27:05
【问题描述】:
我有一个非常宽的带有标签列的数据框。我想独立地为每一列运行逻辑回归。我正在尝试找到并行运行的最有效方法。
+----------+--------+--------+--------+-----+------------+
| features | label1 | label2 | label3 | ... | label30000 |
+----------+--------+--------+--------+-----+------------+
我最初的想法是使用ThreadPoolExecutor,获取每一列的结果,然后加入:
extract_prob = udf(lambda x: float(x[1]), FloatType())
def lr_for_column(argm):
col_name = argm[0]
test_res = argm[1]
lr = LogisticRegression(featuresCol="features", labelCol=col_name, regParam=0.1)
lrModel = lr.fit(tfidf)
res = lrModel.transform(test_tfidf)
test_res = test_res.join(res.select('id', 'probability'), on="id")
test_res = test_res.withColumn(col_name, extract_prob('probability')).drop("probability")
return test_res.select('id', col_name)
with futures.ThreadPoolExecutor(max_workers=100) as executor:
future_results = [executor.submit(lr_for_column, [colname, test_res]) for colname in list_of_label_columns]
futures.wait(future_results)
for future in future_results:
test_res = test_res.join(future.result(), on="id")
但是这种方法的性能不是很好。有没有更快的方法来做到这一点?
【问题讨论】:
-
@user9613318 大约 500000 行,接近 300000 个特征。
-
数据有多少个分区,你总共分配了多少个核心?还有多少内存/核心?
-
@user9613318 200 个分区,8 节点集群,每个节点有 4 核和 28 GB RAM
标签: python apache-spark pyspark apache-spark-ml