【问题标题】:SMOTE in ML classifcationML 分类中的 SMOTE
【发布时间】:2018-12-20 07:20:50
【问题描述】:

我正在 Jupyter 中使用 sklearn 运行分类算法。 我想使用 SMOTE,因为我的一组仅占其他两组的 35%。所以我想对那个组(第 1 组)进行过采样,但我不知道如何整合它。 (编辑:我知道 SMOTE 脚本,但我想知道它在下面的脚本中的位置)。 帮忙?

from sklearn.model_selection import train_test_split
    X_train, X_test, y_train, y_test = train_test_split (X, y, test_size = 0.1) 
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform (X_test)
from sklearn.svm import SVC
clf = SVC(kernel = 'linear')
clf.fit (X_train, y_train.ravel())
y_pred = clf.predict(X_test)
from sklearn.metrics import confusion_matrix
cm = confusion_matrix (y_test, y_pred)
from sklearn.model_selection import cross_val_score
accuracies = cross_val_score (estimator = clf, X = X_train, y = y_train, cv = 10)
accuracies.mean()
accuracies.std()
from sklearn.model_selection import GridSearchCV
parameters = [{'C':[1, 10, 100], 'kernel':['linear']}, 
              {'C':[1, 10, 100], 
               'kernel':['rbf'], 
               'gamma': [0.05, 0.001, 0.005]}]
grid_search = GridSearchCV (estimator = clf, param_grid = parameters, scoring = 'accuracy', cv = 10)
grid_search = grid_search.fit (X_train,y_train)
best_accuracy =  grid_search.best_score_ 
print (best_accuracy)
best_parameters = grid_search.best_params_
print (best_parameters)

【问题讨论】:

    标签: machine-learning scikit-learn classification


    【解决方案1】:

    您必须对数据集执行 SMOTE 并使用生成的平衡数据集来训练您的模型。

    因此,您必须像在问题中未显示的代码中那样加载数据并对其应用 SMOTE。

    就代码而言,可以这样完成

    X = # train data
    y = # train labels
    
    # applying SMOTE
    
    from imblearn.over_sampling import SMOTE
    
    sm = SMOTE(random_state=42)
    X_balanced, y_balanced = sm.fit_sample(X, y)
    
    from sklearn.model_selection import train_test_split
    X_train, X_test, y_train, y_test = train_test_split (X_balanced, y_balanced, test_size = 0.1) 
    from sklearn.preprocessing import StandardScaler
    sc = StandardScaler()
    X_train = sc.fit_transform(X_train)
    X_test = sc.transform (X_test)
    from sklearn.svm import SVC
    clf = SVC(kernel = 'linear')
    clf.fit (X_train, y_train.ravel())
    y_pred = clf.predict(X_test)
    from sklearn.metrics import confusion_matrix
    cm = confusion_matrix (y_test, y_pred)
    from sklearn.model_selection import cross_val_score
    accuracies = cross_val_score (estimator = clf, X = X_train, y = y_train, cv = 10)
    accuracies.mean()
    accuracies.std()
    from sklearn.model_selection import GridSearchCV
    parameters = [{'C':[1, 10, 100], 'kernel':['linear']}, 
                  {'C':[1, 10, 100], 
                   'kernel':['rbf'], 
                   'gamma': [0.05, 0.001, 0.005]}]
    grid_search = GridSearchCV (estimator = clf, param_grid = parameters, scoring = 'accuracy', cv = 10)
    grid_search = grid_search.fit (X_train,y_train)
    best_accuracy =  grid_search.best_score_ 
    print (best_accuracy)
    best_parameters = grid_search.best_params_
    print (best_parameters)
    

    【讨论】:

      【解决方案2】:

      您可以像这样使用imbalanced learn中的SMOTE

      from imblearn.over_sampling import SMOTE
      
      sm = SMOTE(random_state=42)
      X_balanced, y_balanced = sm.fit_sample(X, y) #where X and y are your original features and labels
      

      然后分别使用X_balancedy_balanced 作为您的Xy

      【讨论】:

      • 非常感谢您的回答。也许我的问题不清楚。我想知道如何将 SMOTE 集成到我的脚本中。我知道 smote 的脚本,但我不知道可以在我的脚本中添加到哪里。我编辑了我的 Q。
      • 加载数据集后,像这样处理,其余的都一样
      【解决方案3】:

      在进行 SMOTE 之前,您可以将数据分成训练和测试,以避免过度拟合。 正确的方法:只对训练数据进行过采样。 https://beckernick.github.io/oversampling-modeling/

      【讨论】:

        猜你喜欢
        • 2018-12-02
        • 1970-01-01
        • 2018-12-27
        • 2021-03-01
        • 2020-12-21
        • 2018-05-19
        • 2019-11-30
        • 2019-04-09
        • 1970-01-01
        相关资源
        最近更新 更多