打乱数据行时 100% 的分类器准确率

100% classifier accuracy when shuffling data rows

我正在处理蘑菇分类数据集(在此处找到:https://www.kaggle.com/uciml/mushroom-classification

我已经对数据进行了一些预处理(删除了冗余属性,将分类数据更改为数字),我正在尝试使用我的数据来训练分类器。

每当我手动或使用 train_test_split 打乱数据时,我使用的所有模型(XGB、MLP、LinearSVC、决策树)都具有 100% 的准确性。每当我在未改组的数据上测试模型时,准确度都在 50-85% 左右。

以下是我拆分数据的方法:

x = testing.copy()
x_train, x_test, y_train, y_test = train_test_split(x,y, test_size=0.3, shuffle=True)

和手动

x = testing.copy()
x = x.sample(frac=1)

testRatio = 0.3
testCount = int(len(x)*testRatio)

x_train = x[testCount:]
x_test = x[0:testCount]
y_train = y[testCount:]
y_test = y[0:testCount]

有没有我做的完全错误和遗漏的事情?

编辑: 在拆分数据时有和没有改组行时我能看到的唯一区别是 类.

的分布

没有洗牌:

x_train, x_test, y_train, y_test = train_test_split(x,y, test_size=0.3, shuffle=False)

print(y_test.value_counts())
print(y_train.value_counts())

结果:

0    1828
1     610
Name: class, dtype: int64
1    3598
0    2088
Name: class, dtype: int64

洗牌时:

x_train, x_test, y_train, y_test = train_test_split(x,y, test_size=0.3, shuffle=True)

print(y_test.value_counts())
print(y_train.value_counts())

结果:

0    1238
1    1200
Name: class, dtype: int64
1    3008
0    2678
Name: class, dtype: int64

不过我看不出这会对模型的准确性产生如此大的影响。

编辑2: 按照 PV8 的建议,我尝试使用交叉验证来验证我的结果,它似乎成功了,我通过这种方式得到了更合理的结果。

model = LinearSVC()
scores = cross_val_score(model,x,y,cv=5)
print(scores)
print("Accuracy: %0.2f (+/- %0.2f)" % (scores.mean(), scores.std() * 2))

输出:

[1.         1.         1.         1.         0.75246305]
Accuracy: 0.95 (+/- 0.20)

这可能是正常行为,您尝试了多少次随机播放?

这表明您的数据的拆分方式非常不稳定。我希望你测量的是测试精度而不是训练精度?

我建议您使用cross validation,这将帮助您验证您的总体结果。