オーバーサンプリングされたデータセットをパンダにcsvファイルとして保存する

Aug 24 2020

私はPythonを初めて使用しますが、単純すぎる場合は事前に謝罪します。何も見つかりませんでした。この質問は役に立ちませんでした。

私のコードは

# 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)

ヒントに感謝します!

回答

3 ipj Aug 24 2020 at 08:14

データフレームを作成できます。

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)