0

我正在使用 SMOTE 对TARGET_FRAUD包含值 0 和 1 的二进制类进行重新采样。0 有大约 900 条记录,而 1 只有大约 100 条记录。我想将第 1 类过采样到 800 左右。

这是为了进行一些分类建模。

#fix imbalanced data
from imblearn.over_sampling import SMOTE

#bar plot of target_fraud distribution
sns.countplot('TARGET_FRAUD', data=df)
plt.title('Before Resampling')
plt.show()

#Synthetic Minority Over-Sampling Technique
sm = SMOTE()

# Fit the model to generate the data.
oversampled_trainX, oversampled_trainY = sm.fit_resample(df.drop('TARGET_FRAUD', axis=1), df['TARGET_FRAUD'])
resampled_df = pd.concat([pd.DataFrame(oversampled_trainY), pd.DataFrame(oversampled_trainX)], axis=1)
resampled_df.columns = df.columns

sns.countplot('TARGET_FRAUD', data=resampled_df)
plt.title('After Resampling')
plt.show()

这是重采样前的值计数:

TARGET_FRAUD:
0    898
1    102

这是重新采样后的值计数:

1.000000    1251
0.000000     439
0.188377       1
0.228350       1
0.577813       1
0.989742       1
0.316744       1
0.791926       1
0.970161       1
0.757886       1
0.089506       1
0.567179       1
0.331502       1
0.563530       1
0.882599       1
0.918105       1
0.613229       1
0.239910       1
0.487373       1
...

为什么它会产生 0 到 1 之间的随机浮点值?我只希望它返回 0 和 1 的 int 值。

4

1 回答 1

1

我没有您的数据集,但根据您的代码,我制作了一个可重现的示例。我无法复制你正在写的东西。


from collections import Counter

import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
from imblearn.over_sampling import SMOTE
from sklearn.datasets import make_classification

X, y = make_classification(random_state=0, weights=[0.9, 0.1])
df = pd.DataFrame(X)
df["TARGET_FRAUD"] = y

print("Before resampling")
print(Counter(df["TARGET_FRAUD"]))


sm = SMOTE()

# Fit the model to generate the data.
oversampled_trainX, oversampled_trainY = sm.fit_resample(
    df.drop("TARGET_FRAUD", axis=1), df["TARGET_FRAUD"]
)
resampled_df = pd.concat(
    [pd.DataFrame(oversampled_trainY), pd.DataFrame(oversampled_trainX)],
    axis=1,
)

print("Before resampling")
print(Counter(resampled_df["TARGET_FRAUD"]))

哪个打印

Before resampling
Counter({0: 90, 1: 10})
Before resampling
Counter({0: 90, 1: 90})
于 2020-01-31T16:41:04.203 回答