【发布时间】:2017-06-13 15:07:24
【问题描述】:
所以我试图将某些文本文档分为三类。
我为 spark 中的交叉验证编写了以下代码
from pyspark.ml.tuning import CrossValidator, ParamGridBuilder
from pyspark.ml.evaluation import MulticlassClassificationEvaluator
# Define a grid of hyperparameters to test:
# - maxDepth: max depth of each decision tree in the GBT ensemble
# - maxIter: iterations, i.e., number of trees in each GBT ensemble
# In this example notebook, we keep these values small. In practice, to get the highest accuracy, you would likely want to try deeper trees (10 or higher) and more trees in the ensemble (>100).
paramGrid = ParamGridBuilder()\
.addGrid(jpsa.rf.maxDepth, [2,4,10])\
.addGrid(jpsa.rf.numTrees, [100, 250, 600,800,1000])\
.build()
# We define an evaluation metric. This tells CrossValidator how well we are doing by comparing the true labels with predictions.
evaluator = MulticlassClassificationEvaluator(metricName="f1", labelCol=jpsa.rf.getLabelCol(), predictionCol=jpsa.rf.getPredictionCol())
# Declare the CrossValidator, which runs model tuning for us.
cv = CrossValidator(estimator=pipeline, evaluator=evaluator, estimatorParamMaps=paramGrid,numFolds=5)
cvModel=cv.fit(jpsa.jpsa_train)
evaluator.evaluate(cvModel.transform(jpsa.jpsa_train))
我没有太多数据。 115 总观察(带标签的文件)。我将它们分成 80:35 的训练和测试。在训练中,我使用上面的代码使用 5 折交叉验证。
上面的评估者给了我关于整个训练数据的以下信息。
evaluator.evaluate(cvModel.transform(jpsa.jpsa_train))
0.9021290600237969
我在这里使用 f1,因为我无法在 Spark 中找到用于 MulticlassEvaluator 的 aucROC 作为评估器的选项。它确实有二进制。我知道 AUC 是针对二元类的,但是我们可以通过绘制各种二元类并获取它们的 AUC 来获得多类的组合或平均 AUC。 Sri-kit learn 对多类 AUC 执行相同的操作。
但是,当我在测试数据上使用评估器时,我的 f1 分数很差。
evaluator.evaluate(cvModel.transform(jpsa.jpsa_test))
0.5830903790087463
这表明它过度拟合。此外,如果我不在超参数搜索空间中使用 1000 和 800 棵树,而将其保持为 600 和 800,我的测试准确度为 69%。那么这意味着更多的树会导致过度拟合吗?这很奇怪,因为这与随机森林的工作方式和工作方式相反。更多的树丛会减少方差并导致过度拟合(事实上,人们甚至建议有时随机森林不会过度拟合,尽管我不同意它可以使用非常少的数据和复杂的森林)。
这就是这里发生的事情吗?更少的数据和更多的没有。导致过度拟合的树?
另外,我如何衡量交叉验证的准确性?目前评估者正在训练数据上。我不希望将其作为选择算法的衡量标准。我想要验证数据。是否可以从这个 CV 估计器内部获得这个 OOB 估计值?
【问题讨论】:
标签: python apache-spark machine-learning pyspark random-forest