【发布时间】:2018-01-25 19:43:50
【问题描述】:
我注意到计算模型精度的时间几乎与创建模型本身的时间一样长,这似乎不对。我有一个包含六个虚拟机的集群。最昂贵的时间是“for item in range(numClasses)”循环的第一次迭代。这背后应该发生什么 rdd 操作?
代码:
%pyspark
from pyspark.sql.types import DoubleType
from pyspark.sql.functions import UserDefinedFunction
from pyspark.mllib.regression import LabeledPoint
from pyspark.mllib.tree import DecisionTree
from pyspark.mllib.evaluation import MulticlassMetrics
from timeit import default_timer
def decision_tree(train,test,numClasses,CatFeatInf):
ref = default_timer()
training_data = train.rdd.map(lambda row: LabeledPoint(row[-1], row[:-1])).persist(StorageLevel.MEMORY_ONLY)
testing_data = test.rdd.map(lambda row: LabeledPoint(row[-1], row[:-1])).persist(StorageLevel.MEMORY_ONLY)
print 'transformed in dense data in: %.3f seconds'%(default_timer()-ref)
ref = default_timer()
model = DecisionTree.trainClassifier(training_data,
numClasses=numClasses,
maxDepth=7,
categoricalFeaturesInfo=CatFeatInf,
impurity='entropy', maxBins=max(CatFeatInf.values()))
print 'model created in: %.3f seconds'%(default_timer()-ref)
ref = default_timer()
predictions_and_labels = model.predict(testing_data.map(lambda r: r.features)).zip(testing_data.map(lambda r: r.label))
print 'predictions made in: %.3f seconds'%(default_timer()-ref)
ref = default_timer()
metrics = MulticlassMetrics(predictions_and_labels)
res = {}
for item in range(numClasses):
try:
res[item] = metrics.precision(item)
except:
res[item] = 0.0
print 'accuracy calculated in: %.3f seconds'%(default_timer()-ref)
return res
在密集数据中转换:0.074 秒
模型创建时间:355.276 秒
预测时间:0.095 秒
计算精度:346.497 秒
【问题讨论】:
标签: pyspark