Python IMBREAND SMOTE:如何为多类数据集设置示例_策略参数?

Python IMBREAND SMOTE:如何为多类数据集设置示例_策略参数?,python,pandas,data-processing,imblearn,smote,Python,Pandas,Data Processing,Imblearn,Smote,我正在尝试处理具有以下形状的网络攻击的数据集: df.shape (1074992, 42) 攻击和正常行为的标签具有以下计数: df['Label'].value_counts() normal 812814 neptune 242149 satan 5019 ipsweep 3723 portsweep 3564 smurf 3007 nmap

我正在尝试处理具有以下形状的网络攻击的数据集:

df.shape
(1074992, 42)
攻击和正常行为的标签具有以下计数:

df['Label'].value_counts()
normal            812814
neptune           242149
satan               5019
ipsweep             3723
portsweep           3564
smurf               3007
nmap                1554
back                 968
teardrop             918
warezclient          893
pod                  206
guesspasswd           53
bufferoverflow        30
warezmaster           20
land                  19
imap                  12
rootkit               10
loadmodule             9
ftpwrite               8
multihop               7
phf                    4
perl                   3
spy                    2
Name: Label, dtype: int64
接下来,我将数据集拆分为特征和标签

labels = df['Label']
features = df.loc[:, df.columns != 'Label'].astype('float64')
然后尝试平衡我的数据集

print("Before UpSampling, counts of label Normal: {}".format(sum(labels == "normal")))
print("Before UpSampling, counts of label Attack: {} \n".format(sum(labels != "normal")))
Before UpSampling, counts of label Normal: 812814
Before UpSampling, counts of label Attack: 262178 
因此,正如您所注意到的,攻击的数量与正常行为的数量不成比例

我尝试使用SMOTE将少数(攻击)类设置为与多数(正常)类相同的值

我不明白的是,为什么我在应用SMOTE后得到18694722个值

print("After UpSampling, counts of label Normal: {}".format(sum(labels_res == "normal")))
print("After UpSampling, counts of label Attack: {} \n".format(sum(labels_res != "normal")))
After UpSampling, counts of label Normal: 812814
After UpSampling, counts of label Attack: 17881908 
就我的情况而言,是减少普通类的样本还是增加攻击类的样本更好? 关于如何正确地做这件事,你有什么想法吗


非常感谢。

默认情况下,
采样策略是
而不是多数

“非多数”:对除多数类以外的所有类重新采样

所以,如果大多数类的样本是812814,那么

(812814*23)=18694722
样品

尝试传递一个dict,其中包含少数类所需的样本数。从

当dict时,键对应于目标类。这些值对应于每个目标类所需的样本数

示例

根据,在本例中,我们对少数类中的一个进行了上采样,使其具有与多数类相同的样本数

从sklearn.dataset导入make_分类
从收款进口柜台
从IMBRearn.over_采样导入SMOTE
十、 y=进行分类(n类=5,
类_sep=2,
权重=[0.15,0.15,0.1,0.1,0.5],
n_=4,
n_冗余=1,
翻转y=0,
n_特征=20,
每类n个簇=1,
n_样本=1000,
随机状态=10)
样本_策略={4:500,0:500,1:150,2:100,3:100}
sm=SMOTE(采样策略=采样策略,随机状态=0)
X_res,y_res=sm.fit_重采样(X,y)
从收款进口柜台
打印('重新采样的数据集形状%s'%Counter(y\u res))
>>>
重新采样的数据集形状计数器({4:500,0:500,1:150,3:100,2:100})

Thank you@Miguel我以前用字典探索过这个选项,但因为我有23个目标类,所以我试图寻找其他的。但我认为这个选择也适用于我的情况。
print("After UpSampling, counts of label Normal: {}".format(sum(labels_res == "normal")))
print("After UpSampling, counts of label Attack: {} \n".format(sum(labels_res != "normal")))
After UpSampling, counts of label Normal: 812814
After UpSampling, counts of label Attack: 17881908