所以我一直在嘗試幾天來在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)作爲參數。 最後,我有可能做我想做的事情嗎?有什麼選擇?
謝謝你的回答。我會檢查這些外部管道經理! –