【发布时间】:2023-01-26 21:46:27
【问题描述】:
我正在开发一个使用数据扩充的分类问题。为此,我已经通过添加噪声和其他特征从副本中提取特征。但是,我想避免数据泄漏,例如,当副本在训练集中而原始数据在测试集中时,可能会发生这种情况。
我开始测试一些解决方案,然后我得到了下面的代码。但是,我不知道目前的解决方案是否可以防止这个问题。
基本上,我有原始基础(df)和具有副本特征的基础(df2)。当我在训练和测试中拆分 df 时,我会在 df2 中寻找副本,以便它们与原始数据在一起,无论是在训练还是在测试中。
有人能帮我吗?
这是代码:
df = pd.read_excel("/content/drive/MyDrive/data/audio.xlsx")
df2 = pd.read_excel("/content/drive/MyDrive/data/audioAUG.xlsx")
X = df.drop('emotion', axis = 1)
y = df['emotion']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state= 42, stratify=y)
X_train_AUG = df2[df2['id'].isin(X_train.id.to_list())]
X_test_AUG = df2[df2['id'].isin(X_test.id.to_list())]
X_train = X_train.append(X_train_AUG.loc[:, ~X_train_AUG.columns.isin(['emotion'])])
X_test = X_test.append(X_test_AUG.loc[:, ~X_test_AUG.columns.isin(['emotion'])])
y_train_AUG = X_train_AUG.loc[:, X_train_AUG.columns.isin(['emotion'])]
y_test_AUG = X_test_AUG.loc[:, X_test_AUG.columns.isin(['emotion'])]
y_train_AUG = y_train_AUG.squeeze()
y_test_AUG = y_test_AUG.squeeze()
y_train = y_train.append(y_train_AUG)
y_test = y_test.append(y_test_AUG)
【问题讨论】:
标签: python pandas machine-learning scikit-learn data-augmentation