对数据进行上采样和下采样

import numpy as np
import pandas as pd
from imblearn.over_sampling import SMOTE
from imblearn.over_sampling import RandomOverSampler
from imblearn.under_sampling import RandomUnderSampler
import warnings
#忽略警告
warnings.filterwarnings('ignore')
pd.set_option('display.max_columns',None)#显示所有列
pd.set_option('display.width',110)#最多显示110行信息

path = '../dataset/lympho1.csv'
data = pd.read_csv(path)
x = data.iloc[:,:-1]
y = data.iloc[:,-1]
groupByLabel = data.groupby('y0').count()
print(groupByLabel)


"""
使用SMOTE进行过采样
"""
# model_smote = SMOTE()
# x_smote_resampled,y_smote_resampled = model_smote.fit_sample(x,y)
# x_smote_resampled = pd.DataFrame(x_smote_resampled,columns=data.columns[:-1])
# y_smote_resampled = pd.DataFrame(y_smote_resampled,columns=['y0'])
# smote_resampled = pd.concat([x_smote_resampled,y_smote_resampled],axis=1)
# groupby_data_smote = smote_resampled.groupby('y0').count()
# print(groupby_data_smote)

"""
进行下采样
"""
model_RandomUnderSample = RandomUnderSampler()
x_RandomUnderSample,y_RandomUnderSample = model_RandomUnderSample.fit_sample(x,y)
# print(x_RandomUnderSample)
x_RandomUnderSample = pd.DataFrame(x_RandomUnderSample,columns=data.columns[:-1])
y_RandomUnderSample = pd.DataFrame(y_RandomUnderSample,columns=['y0'])
randomUnderSample = pd.concat([x_RandomUnderSample,y_RandomUnderSample],axis=1)
groupbyRandomUnderSample = randomUnderSample.groupby('y0').count()
print(groupbyRandomUnderSample)
#RandomOverSampler库的采样方法和上面集中类似

你可能感兴趣的:(Python)