【问题标题】:How can I generate categorical synthetic samples with imblearn and SMOTE?如何使用 imblearn 和 SMOTE 生成分类合成样本?
【发布时间】:2017-03-27 14:42:44
【问题描述】:

我希望使用 imblearn 的 SMOTE 为机器学习算法生成合成样本。我有一些分类特征,我使用 sklearn preprocessing.LabelEncoder 将其转换为整数。

我遇到的问题是,当我使用 smote 生成合成数据时,数据点变成浮点数,而不是分类数据所需的整数。

from collections import Counter
from sklearn.datasets import make_classification
from imblearn.over_sampling import SMOTE
import pandas as pd
from matplotlib import pyplot as plt
from sklearn.decomposition import PCA
import numpy as np
from sklearn import preprocessing

if __name__ == '__main__':
    df = pd.read_csv('resample.csv')
    y = df['Result']
    accounts = df['Account Number']
    df.drop('Result',axis=1,inplace=True)
    df.drop('Account Number', axis=1, inplace=True)

    df.fillna(value=0, inplace=True)

    le = preprocessing.LabelEncoder()
    le.fit(df['Distribution Partner'])
    print(le.classes_)
    df['Distribution Partner'] = le.transform(df['Distribution Partner'])
    print('Original dataset shape {}'.format(Counter(y)))
    sm = SMOTE(kind='regular')
    X_resampled, y_resampled = sm.fit_sample(df, y)
    np.savetxt('output.csv', X_resampled, delimiter=",")
    print('New dataset shape {}'.format(Counter(y_resampled)))

无论如何我可以让 SMOTE 生成合成样本,但只能使用 0、1、2 等而不是 0.5、1.23、2.004 的值?

【问题讨论】:

    标签: python python-3.x scikit-learn imblearn


    【解决方案1】:

    不幸的是,imblearn 的 SMOTE 实现仅适用于连续数据。正在讨论here

    【讨论】:

      【解决方案2】:

      非常简单:使用 SMOTENC 代替 SMOTE。 SMOTENC 可以处理分类和非分类特征。

      示例代码:

      from imblearn.over_sampling import SMOTENC`
      obj = SMOTENC(categorical_features = [1,4])
      ovsersampled_features, ovsersampled_target = obj.fit_sample(Features, Target)
      

      [1,4] = 数据集分类列的索引。

      *索引从0开始。

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-04
      • 2019-08-23
      • 2019-09-29
      • 2019-10-01
      • 1970-01-01
      • 2018-12-02
      • 2019-04-09
      • 1970-01-01
      相关资源
      最近更新 更多