Spark GBTClassifier 始终以 100% 的准确率进行预测
Spark GBTClassifier always predicts with 100% accuracy
我使用 SparkML GBTClassifier 在宽特征数据集上训练二元分类问题:
Xtrain.select(labelCol).groupBy(labelCol).count().orderBy(labelCol).show()
+-----+------+
|label| count|
+-----+------+
| 0|631608|
| 1| 18428|
+-----+------+
va = VectorAssembler(inputCols=col_header, outputCol="features")
tr = GBTClassifier(labelCol=labelCol, featuresCol="features", maxIter=30, maxDepth=5, seed=420)
pipeline = Pipeline(stages=[va, tr])
model = pipeline.fit(Xtrain)
分类器运行速度非常快(不寻常)并且以 100% 的准确率学习,更多的测试集也以 100% 的准确率预测。当我打印
model.stages[1].featureImportances
SparseVector(29, {0: 0.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0, 5: 0.0, 6: 0.0, 7: 0.0, 8: 0.0, 9: 0.0, 10: 0.0, 11: 0.0, 12: 0.0, 13: 0.0, 14: 0.0, 15: 0.0, 16: 0.0, 17: 0.0, 18: 0.0, 19: 0.0, 20: 0.0, 21: 0.0, 22: 0.0, 23: 0.0, 24: 1.0, 25: 0.0, 26: 0.0, 27: 0.0, 28: 0.0})
我注意到我的 DataFrame 中的一个特征(在本例中为 #24)为模型贡献了 100% 的权重。当我删除这个字段并重新训练时,我看到了相同的图片,唯一的区别是第二个字段现在对模型有贡献并且我获得了 100% 的准确度。显然这里有些不对,这是什么?
在非退化数据集上出现这种行为的最常见原因是数据泄漏。数据泄露可能有不同的形式,但考虑到
that one feature (#24 in this case) in my DataFrame contributed 100% weight
我们可以大大缩小范围:
- 一个简单的编码错误 - 您在特征中包含了标签(或转换后的标签)。您应该仔细检查您的处理管道。
- 原始数据包含已用于派生标签或从标签派生的特征。您应该检查数据字典(如果存在)或其他可用来源,以确定应该从您的模型中丢弃哪些特征(通常寻找您不会在原始数据中期望的任何东西)。
我使用 SparkML GBTClassifier 在宽特征数据集上训练二元分类问题:
Xtrain.select(labelCol).groupBy(labelCol).count().orderBy(labelCol).show()
+-----+------+
|label| count|
+-----+------+
| 0|631608|
| 1| 18428|
+-----+------+
va = VectorAssembler(inputCols=col_header, outputCol="features")
tr = GBTClassifier(labelCol=labelCol, featuresCol="features", maxIter=30, maxDepth=5, seed=420)
pipeline = Pipeline(stages=[va, tr])
model = pipeline.fit(Xtrain)
分类器运行速度非常快(不寻常)并且以 100% 的准确率学习,更多的测试集也以 100% 的准确率预测。当我打印
model.stages[1].featureImportances
SparseVector(29, {0: 0.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0, 5: 0.0, 6: 0.0, 7: 0.0, 8: 0.0, 9: 0.0, 10: 0.0, 11: 0.0, 12: 0.0, 13: 0.0, 14: 0.0, 15: 0.0, 16: 0.0, 17: 0.0, 18: 0.0, 19: 0.0, 20: 0.0, 21: 0.0, 22: 0.0, 23: 0.0, 24: 1.0, 25: 0.0, 26: 0.0, 27: 0.0, 28: 0.0})
我注意到我的 DataFrame 中的一个特征(在本例中为 #24)为模型贡献了 100% 的权重。当我删除这个字段并重新训练时,我看到了相同的图片,唯一的区别是第二个字段现在对模型有贡献并且我获得了 100% 的准确度。显然这里有些不对,这是什么?
在非退化数据集上出现这种行为的最常见原因是数据泄漏。数据泄露可能有不同的形式,但考虑到
that one feature (#24 in this case) in my DataFrame contributed 100% weight
我们可以大大缩小范围:
- 一个简单的编码错误 - 您在特征中包含了标签(或转换后的标签)。您应该仔细检查您的处理管道。
- 原始数据包含已用于派生标签或从标签派生的特征。您应该检查数据字典(如果存在)或其他可用来源,以确定应该从您的模型中丢弃哪些特征(通常寻找您不会在原始数据中期望的任何东西)。