【问题标题】:SMOTE resampling produces nan valuesSMOTE 重采样产生 nan 值
【发布时间】:2021-03-05 11:35:49
【问题描述】:

我正在使用 SMOTE 对少数数据集进行过采样。我的代码如下:

from imblearn.over_sampling import SMOTE

X_train, X_test, y_train, y_test = train_test_split(features_coded, labels, test_size=0.2, random_state=42)

sm = SMOTE(random_state=42, sampling_strategy='all')
# also tried the following, same result
# sm = SMOTE(random_state=42, sampling_strategy=0.5)
X_train, y_train = sm.fit_resample(X_train, y_train)

我使用如下语句检查 features_coded、labels、X_train 和 y_train:

features_coded[features_coded.isnull().any(axis=1)]

我很确定它们在过采样之前不包含任何 nan 值。但是,重采样后,X_train 数据帧中有很多 nan 值。

以防万一您想知道: 这是我在过采样之前的数据框(保存为 csv 文件),没有任何遗漏。

这是我过采样后的数据框(保存为 csv 文件),有很多空值!

有什么事吗?

【问题讨论】:

  • salvolapa 发布了一个 Answer 说“SMOTE 及其除 RandomOverSampler 之外的许多变体无法处理分类值。这是你的情况吗?imbalanced-learn.org/stable/over_sampling.html#smote-variants
  • 我猜不是,我发现了一个丑陋的黑客来避免它:如果我将数据帧保存到 csv 文件,然后再次将文件读取到数据帧,问题就会消失。我认为这是关于数据帧的内部结构而不是 SMOTE 包。

标签: machine-learning logistic-regression smote


【解决方案1】:

我遇到了类似的问题,我使用 X_arr = numpy.array(X)y_arr = numpy.array(Y) 行将我的输入 XY 转换为数组并将它们提供给 train_test_split(),如下所示:

X_train, X_test, y_train, y_test = train_test_split(X_arr, y_arr, test_size = 0.2, random_state = 2)

smote = SMOTE(random_state=2)

X_train_balanced, Y_train_balanced = smote.fit_resample(X_train, y_train)

【讨论】:

    猜你喜欢
    • 2016-01-26
    • 1970-01-01
    • 2019-05-24
    • 2020-07-05
    • 1970-01-01
    • 2019-02-25
    • 1970-01-01
    • 2015-10-29
    • 1970-01-01
    相关资源
    最近更新 更多