重新采样数据集用于垃圾邮件分类



我有一个类不平衡的问题与以下数据集:

Text                             is_it_capital?     is_it_upper?      contains_num?   Label
an example of text                      0                  0               0            0
ANOTHER example of text                 1                  1               0            1
What's happening?Let's talk at 5        1                  0               1            1

和相似。我有5000行/文本(4500与类0和500类1)。

我需要重新采样我的类,但我不知道在我的模型中包含这一步的地方,所以如果你能看看并告诉我是否我错过了一些步骤,或者如果你发现方法中的任何不一致,我将不胜感激。

对于train,测试我使用以下内容:

X_train, X_test, y_train, y_test  = train_test_split(X, y, test_size=0.25, random_state=40) 

其中X

X=df[['Text','is_it_capital?', 'is_it_upper?', 'contains_num?']]
y=df['Label']
df_train= pd.concat([X_train, y_train], axis=1)
df_test = pd.concat([X_test, y_test], axis=1)

# Separating classes
spam = df_train[df_train.Label == 1]
not_spam = df_train[df_train.Label == 0]
# Oversampling  
oversampl = resample(spam,replace=True,n_samples=len(not_spam), random_state=42)
oversampled = pd.concat([not_spam, oversampl])
df_train = oversampled.copy()

输出(不对吗?):

precision    recall  f1-score   support
0.0       0.94      0.98      0.96      3600
1.0       0.76      0.52      0.62       400
accuracy                           0.93      4000
macro avg       0.86      0.77      0.80      4000
weighted avg       0.92      0.93      0.93      4000

你认为在我对数据集进行过采样的步骤中有什么错误吗,因为混淆矩阵给了我400而不是更高的支持?

很抱歉写了这么长一篇文章,但我认为为了更好地理解我所采取的方法,报告所有的步骤是值得的。

你的方法没有问题,评估报告显示数据不平衡是正常的。这是因为:

  • 重新采样(正确地)只在训练集上进行,以迫使模型更加重视少数类。
  • (正确地)对遵循原始不平衡分布的测试集进行评估。重新采样测试集也是错误的,因为必须对数据的真实分布进行评估。

最新更新