【问题标题】:How do I save the data that has been randomly undersampled?如何保存随机欠采样的数据?
【发布时间】:2021-10-12 22:43:55
【问题描述】:

我正在尝试通过使用多数类的随机欠采样来平衡数据帧。它已经成功了,但是,我还想将从数据框中删除的数据(欠采样)保存到新的数据框中。我该如何做到这一点?

这是我用来对数据框进行欠采样的代码

from imblearn.under_sampling import RandomUnderSampler

rus = RandomUnderSampler(sampling_strategy=1)
X_res, y_res = rus.fit_resample(X, y)

df1 = pd.concat([X_res, y_res], axis=1)

【问题讨论】:

    标签: python dataframe machine-learning


    【解决方案1】:

    RandomUnderSampler 有一个属性sample_indices_,表示保留子样本的索引。所以应该这样做:

    dropped_ids = [i for i in range(X.shape[0]) if i not in rus.sample_indices_]
    X.iloc[dropped_ids]  # for dataframes
    X[dropped_ids, :]  # for numpy arrays
    

    【讨论】:

    • 效果很好,非常感谢!
    猜你喜欢
    • 2021-08-20
    • 2020-06-04
    • 2017-09-10
    • 2023-01-29
    • 2017-10-29
    • 1970-01-01
    • 1970-01-01
    • 2019-09-03
    • 2012-01-06
    相关资源
    最近更新 更多