所以我一直在尝试几天来在Spark的map函数里面运行ML算法。我贴一个更具体的question但引用星火的ML算法使我有以下错误:在Spark中运行ML算法里面的map函数
AttributeError: Cannot load _jvm from SparkContext. Is SparkContext initialized?
很明显,我不能引用SparkContext
的apply_classifier
函数内。 我的代码是类似于在前面的问题,我问建议,但至今还没有找到一个解决我所期待的:
def apply_classifier(clf):
dt = DecisionTreeClassifier(labelCol="indexedLabel", featuresCol="indexedFeatures", maxDepth=3)
if clf == 0:
clf = DecisionTreeClassifier(labelCol="indexedLabel", featuresCol="indexedFeatures", maxDepth=3)
elif clf == 1:
clf = RandomForestClassifier(labelCol="indexedLabel", featuresCol="indexedFeatures", numTrees=5)
classifiers = [0, 1]
sc.parallelize(classifiers).map(lambda x: apply_classifier(x)).collect()
我一直在使用flatMap
代替map
尝试,但我得到NoneType object is not iterable
。
我还想在apply_classifier
函数中传递一个广播数据集(这是一个DataFrame)作为参数。 最后,我有可能做我想做的事情吗?有什么选择?
谢谢你的回答。我会检查这些外部管道经理! –