0

我的数据不平衡,如下所示,每当我尝试使用 ADASYN 时,它都会显示错误,我们是否需要为此提供任何参数条目?有时它会运行很长时间,但即使在代码运行 40 分钟后也没有响应。

                     counts  percentage
Enquiry Assigned      91284   75.902382
Test Drive Provided   25274   21.015258
Test Drive Arranged    3434    2.855361
Booked                  266    0.221178
Test Ride Provided        7    0.005820

请建议我们如何继续使用 python 代码来解决问题。从其他人的推荐中,我听说过

  1. 可以一次在两个级别之间进行采样,然后可以在同一级别上进行迭代
  2. 下采样 75% 的那个可能有帮助吗?
  3. 或使用 skmultilearn 的任何解决方案?

代码:

def makeOverSamplesADASYN(X,y):

    #X →Independent Variable in DataFrame\
     #y →dependent Variable in Pandas DataFrame format
     from imblearn.over_sampling import ADASYN 
     sm = ADASYN(sampling_strategy='all', random_state=None, n_neighbors=5, n_jobs=1, ratio=None)
    
     X_adassin, y_adassin = sm.fit_resample(X, y)

 makeOverSamplesADASYN(X,data_dummyvar['Sales Stage'])

 print(X_adassin.shape)
 print(y_adassin.shape)'''   

o/p=== > 这运行了很长时间,之后没有结果,请建议

4

1 回答 1

0

我曾经使用下面的代码对顶部条目进行了下采样。

### " data_dummyvar " is my dataframe with the shape of (120265, 894)

df_majority=data_dummyvar[data_dummyvar['Sales Stage']=='Enquiry Assigned']
df_majority.shape
from sklearn.utils import resample

# Downsample majority class
df_majority_downsampled = resample(df_majority,replace=False,n_samples=25289,random_state=123)                                   
#replace: sample without replacement
# n_samples: to match minority class
#random_state: reproducible results
df_majority_downsampled.shape
df_minority=data_dummyvar[data_dummyvar['Sales Stage'] !='Enquiry Assigned']
df_minority['Sales Stage'].value_counts()
df_first_scaling = pd.concat([df_majority_downsampled,df_minority],ignore_index=True)
g = df_first_scaling['Sales Stage']
df = pd.concat([g.value_counts(),              
g.value_counts(normalize=True).mul(100)],axis=1, keys=('counts','percentage'))
print (df)

上面的代码将给出如下结果: o/p ===>>

                        counts  percentage
Enquiry Assigned      25289   46.598489
Test Drive Provided   25281   46.583748
Test Drive Arranged    3434    6.327621
Booked                  266    0.490142

现在,“已分配的查询”条目已在此处下采样。

现在我们需要对我们的数据“df_first_scaling”运行 SMOTE/ADASYN 算法两次,因为我们还有三个条目,如下所示

def makeOverSamplesADASYN(X,y):
   #input DataFrame
   #X →Independent Variable in DataFrame\
   #y →dependent Variable in Pandas DataFrame format
   from imblearn.over_sampling import ADASYN 
   sm = ADASYN(sampling_strategy='minority', random_state=None, n_neighbors=5, n_jobs=1, ratio=None)
   global X_adassin_1
   global y_adassin_1
   X_adassin_1, y_adassin_1 = sm.fit_resample(X, y)

makeOverSamplesADASYN(X,df_first_scaling['Sales Stage']) # function call

print(X_adassin_1.shape)
print(y_adassin_1.shape)

这给出了形状为 ==> 的 o/p

(79334, 893)
(79334,) 

在更新的数据集上再次运行相同的方法后,我们可以得到形状为 (101229, 893) & (101229,) 的样本 df

于 2019-11-08T06:43:43.753 回答