【发布时间】:2017-12-05 14:18:17
【问题描述】:
我正在使用 Spark ML 运行一些 ML 实验,在一个 20MB (Poker dataset) 的小数据集和一个带有参数网格的随机森林上,需要 1 小时 30 分钟才能完成。与 scikit-learn 类似,所需的时间要少得多。
在环境方面,我使用 2 个从属设备,每个 15GB 内存,24 个内核进行测试。我认为它不应该花那么长时间,我想知道问题是否出在我的代码中,因为我对 Spark 还很陌生。
这里是:
df = pd.read_csv(http://archive.ics.uci.edu/ml/machine-learning-databases/poker/poker-hand-testing.data)
dataframe = sqlContext.createDataFrame(df)
train, test = dataframe.randomSplit([0.7, 0.3])
columnTypes = dataframe.dtypes
for ct in columnTypes:
if ct[1] == 'string' and ct[0] != 'label':
categoricalCols += [ct[0]]
elif ct[0] != 'label':
numericCols += [ct[0]]
stages = []
for categoricalCol in categoricalCols:
stringIndexer = StringIndexer(inputCol=categoricalCol, outputCol=categoricalCol+"Index")
stages += [stringIndexer]
assemblerInputs = map(lambda c: c + "Index", categoricalCols) + numericCols
assembler = VectorAssembler(inputCols=assemblerInputs, outputCol="features")
stages += [assembler]
labelIndexer = StringIndexer(inputCol='label', outputCol='indexedLabel', handleInvalid='skip')
stages += [labelIndexer]
estimator = RandomForestClassifier(labelCol="indexedLabel", featuresCol="features")
stages += [estimator]
parameters = {"maxDepth" : [3, 5, 10, 15], "maxBins" : [6, 12, 24, 32], "numTrees" : [3, 5, 10]}
paramGrid = ParamGridBuilder()
for key, value in parameters.iteritems():
paramGrid.addGrid(estimator.getParam(key), value)
estimatorParamMaps = (paramGrid.build())
pipeline = Pipeline(stages=stages)
crossValidator = CrossValidator(estimator=pipeline, estimatorParamMaps=estimatorParamMaps, evaluator=MulticlassClassificationEvaluator(labelCol='indexedLabel', predictionCol='prediction', metricName='f1'), numFolds=3)
pipelineModel = crossValidator.fit(train)
predictions = pipelineModel.transform(test)
evaluator = pipeline.getEvaluator().evaluate(predictions)
在此先感谢,非常感谢任何 cmets/建议 :)
【问题讨论】:
-
交叉验证是一项繁重而漫长的任务,因为它与您的 3 个超参数乘以折叠次数乘以训练每个模型所花费的时间的组合成正比。您可能希望先缓存每个示例的数据,但它仍然不会为您赢得太多时间。我相信火花对于如此大量的数据来说是一种过度杀伤力。您可能想改用 scikit learn 并使用 github.com/databricks/spark-sklearn 进行分布式本地模型训练
-
嗨@eliasah 谢谢你的评论。事实上,我正在使用 spark-sklearn 做到这一点,并取得了不错的成绩。但是,我只是想比较 sklearn 和 spark 之间的执行时间,但这些数字对我来说似乎很奇怪,因为一个需要几秒钟,另一个需要几个小时
-
因为spark会在假设数据是分布式且大的假设下,分别依次学习每个模型。
-
当您读取优化的文件格式(如镶木地板)时,您可以从性能上受益。也可以调整 spark 本身,但在这里讨论太广泛了。
-
是的。这是可能的。
标签: apache-spark pyspark apache-spark-mllib apache-spark-ml