【问题标题】:Saving oversampled dataset as csv file in pandas在 Pandas 中将过采样数据集保存为 csv 文件
【发布时间】:2020-12-12 20:26:06
【问题描述】:

我是 Python 新手,如果太简单,请提前道歉。找不到任何东西,this question 也没有帮助。

我的代码是

# Split data
y = starbucks_smote.iloc[:, -1]
X = starbucks_smote.drop('label', axis = 1)

# Count labels by type
counter = Counter(y)
print(counter)
Counter({0: 9634, 1: 2895})

# Transform the dataset
oversample = SMOTE()
X, y = oversample.fit_resample(X, y)

# Print the oversampled dataset
counter = Counter(y)
print(counter)
Counter({0: 9634, 1: 9634})

如何保存过采样数据集以供将来工作?

我试过了

data_res = np.concatenate((X, y), axis = 1)
data_res.to_csv('sample_smote.csv')

出错了

ValueError: all the input arrays must have same number of dimensions, 
but the array at index 0 has 2 dimension(s) and the array at index 1 has 1 dimension(s)

感谢任何提示!

【问题讨论】:

  • 尝试保存后出现什么结果或错误?

标签: python pandas numpy resampling smote


【解决方案1】:

您可以创建数据框:

data_res = pd.DataFrame(X)
data_res['y'] = y

然后将data_res 保存到 CSV。

基于连接 od numpy.arrays 的解决方案也是可能的,但需要 np.vstack 以使尺寸符合要求:

data_res = np.concatenate((X, np.vstack(y)), axis = 1)
data_res = pd.DataFrame(data_res)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-04
    • 1970-01-01
    • 2014-03-21
    • 1970-01-01
    • 2022-06-28
    • 2016-03-12
    • 1970-01-01
    相关资源
    最近更新 更多