我有一个奇怪的问题,我有一个包含 4 个集群的模型,数据不平衡的比例如下:75%、15%、7% 和 3%。我将它分成训练和测试,比例为 80/20,然后我训练一个有 5 个邻居的 KNN,给我 1 的准确率。
sss = StratifiedShuffleSplit(n_splits=5, test_size=0.2, random_state=42)
train_index, test_index = next(sss.split(X, y))
x_train, y_train = X[train_index], y[train_index]
x_test, y_test = X[test_index], y[test_index]
KNN_final = KNeighborsClassifier()
KNN_final.fit(x_train, y_train)
y_pred = KNN_final.predict(x_test)
print('Avg. accuracy for all classes:', metrics.accuracy_score(y_test, y_pred))
print('Classification report: \n',metrics.classification_report(y_test, y_pred, digits=2))
Avg. accuracy for all classes: 1.0
Classification report:
precision recall f1-score support
0 1.00 1.00 1.00 140
1 1.00 1.00 1.00 60
2 1.00 1.00 1.00 300
3 1.00 1.00 1.00 1500
accuracy 1.00 2000
macro avg 1.00 1.00 1.00 2000
weighted avg 1.00 1.00 1.00 2000
虽然看起来很奇怪,但我继续,获取新数据并尝试根据这个模型对其进行分类,但它永远不会找到百分比较小的类,它总是将其错误分类为第二低类。所以我尝试使用带有 SMOTEENN 算法的不平衡学习库来平衡数据:
Original dataset shape Counter({3: 7500, 2: 1500, 0: 700, 1: 300})
sme = SMOTEENN(sampling_strategy='all', random_state=42)
X_res, y_res = sme.fit_resample(X, y)
print('Resampled dataset shape %s' % Counter(y_res))
Resampled dataset shape Counter({0: 7500, 1: 7500, 2: 7500, 3: 7500})
然后我做同样的事情,将它分成相同比例 80/20 的训练和测试,并用 5 个邻居训练一个新的 KNN 分类器。但分类报告现在似乎更糟了:
Avg. accuracy for all classes: 1.0
Classification report:
precision recall f1-score support
0 1.00 1.00 1.00 1500
1 1.00 1.00 1.00 500
accuracy 1.00 2000
macro avg 1.00 1.00 1.00 2000
weighted avg 1.00 1.00 1.00 2000
我看不出我做错了什么,在训练新分类器之前,除了拆分和洗牌之外,在重新采样数据之后我还需要做什么流程吗?为什么我的 KNN 现在没有看到 4 个类?