【问题标题】:Training Error is Lower than Testing error in a Random Forest Model随机森林模型中的训练误差低于测试误差
【发布时间】:2019-04-24 12:45:37
【问题描述】:

我一直在研究机器学习模型,我对选择哪种模型或是否应该尝试任何其他技术感到困惑。我正在研究随机森林,以预测使用高度不平衡的数据集进行转换的倾向。目标变量的类平衡如下。

   label   count                                                                
0    0.0  1,021,095
1    1.0    4459

我训练的两个模型是使用 UpSampling 和 Undersampling。以下是我用于上采样和欠采样的代码

train_initial, test = new_data.randomSplit([0.7, 0.3], seed = 2018)
train_initial.groupby('label').count().toPandas()
test.groupby('label').count().toPandas()

#Sampling Techniques --- Should be done one of these
#Upsampling ----
df_class_0 = train_initial[train_initial['label'] == 0]
df_class_1 = train_initial[train_initial['label'] == 1]
df_class_1_over = df_class_1.sample(True, 100.0, seed=99)
train_up = df_class_0.union(df_class_1_over)
train_up.groupby('label').count().toPandas()

#Down Sampling
stratified_train = train_initial.sampleBy('label', fractions={0: 3091./714840, 1: 1.0}).cache()
stratified_train.groupby('label').count().toPandas()

下面是我如何训练我的模型

labelIndexer = StringIndexer(inputCol='label',
                             outputCol='indexedLabel').fit(new_data)


featureIndexer = VectorIndexer(inputCol='features',
                               outputCol='indexedFeatures',
                               maxCategories=2).fit(new_data)

from pyspark.ml.classification import RandomForestClassifier
rf_model = RandomForestClassifier(labelCol="indexedLabel", featuresCol="indexedFeatures")

labelConverter = IndexToString(inputCol="prediction", outputCol="predictedLabel",
                               labels=labelIndexer.labels)

# Chain indexers and tree in a Pipeline
pipeline = Pipeline(stages=[labelIndexer, featureIndexer, rf_model, labelConverter])

# Search through random forest maxDepth parameter for best model
paramGrid = ParamGridBuilder() \
    .addGrid(rf_model.numTrees, [ 200, 400,600,800,1000]) \
    .addGrid(rf_model.impurity,['entropy','gini']) \
    .addGrid(rf_model.maxDepth,[2,3,4,5]) \
    .build()


# Set up 5-fold cross validation
crossval = CrossValidator(estimator=pipeline,
                          estimatorParamMaps=paramGrid,
                          evaluator=BinaryClassificationEvaluator(),
                          numFolds=5)    

train_model = crossval.fit(train_up/stratified_train)

以下是两种方法的结果

#UpSampling - Training                                 
Train Error = 0.184633
precision: 0.8565508112679312
recall: 0.6597217024736883                                            
auroc: 0.9062348758176568
f1 : 0.7453609484359377

#Upsampling - Test                                
Test Error = 0.0781619                             
precision: 0.054455645977569946
recall: 0.6503868471953579
auroc: 0.8982212236597943
f1 : 0.10049688048716704

#UnderSampling - Training                               
Train Error = 0.179293           
precision: 0.8468290542023261
recall: 0.781807131280389
f1 : 0.8130201200884863                                          
auroc: 0.9129391668636556

#UnderSamping - Test                               
Test Error = 0.147874
precision: 0.034453223699706645
recall: 0.778046421663443
f1 : 0.06598453935901905
auroc: 0.8989720777537427

参考 StackOverflow 上的各种文章,我了解到,如果测试误差低于训练误差,则实施中可能会出错。但是,为了训练我的模型,我不太确定在哪里出错。此外,在这种高度不平衡的类的情况下,使用哪种采样更好。如果我进行欠采样,我担心是否会丢失信息。

我希望有人可以帮助我解决这个模型并帮助我消除疑虑。

提前非常感谢!!

【问题讨论】:

    标签: machine-learning random-forest sampling


    【解决方案1】:

    测试错误低于训练错误并不一定意味着实施错误。您可以增加训练模型的迭代次数,并且根据您的数据集,训练误差可能会低于测试误差。但是,您最终可能会过度拟合。因此,目标还应该是检查测试集的其他性能指标,例如准确度、精确度、召回率等。

    过采样和欠采样是相反但大致相同的技术。如果您有很多数据点,那么最好进行欠采样。否则进行过采样。 SMOTE 是一种很好的过采样技术,它通过创建合成数据点而不是多次重复相同的数据点来实现。

    https://imbalanced-learn.readthedocs.io/en/stable/generated/imblearn.over_sampling.SMOTE.html

    另一个提示,用不同的种子对数据进行洗牌,看看训练误差是否大于测试误差。我怀疑您的数据差异很大。阅读有关方差-偏差权衡的信息。

    从结果来看,您似乎已经建立了一个相当不错的模型。也尝试使用 XGBoost 并将结果与​​随机森林进行比较。

    【讨论】:

    • 非常感谢您的回复。正如建议的那样,我用不同的种子值对数据进行了洗牌。使用我在问题中提到的相同代码执行下采样。几乎每次我的测试错误率都比训练错误高 0.05%。您对错误率的这种变化有何看法。我使用了相同的训练模型,将数据打乱了
    猜你喜欢
    • 1970-01-01
    • 2016-01-24
    • 2018-12-21
    • 1970-01-01
    • 2019-12-20
    • 2014-09-28
    • 2021-07-10
    • 2021-03-21
    • 2020-10-31
    相关资源
    最近更新 更多