简体   繁体   English

微调超参数不会提高分类器的分数

[英]Fine Tuning hyperparameters doesn't improve score of classifiers

I am experiencing a problem where f.netuning the hyperparameters using GridSearchCV doesn't really improve my classifiers.我遇到一个问题,即使用 GridSearchCV 对超参数进行 f.netuning 并没有真正改进我的分类器。 I figured the improvement should be bigger than that.我认为改进应该比这更大。 The biggest improvement for a classifier I've gotten with my current code is around +-0.03.我目前的代码对分类器的最大改进是大约 +-0.03。 I have a dataset with eight columns and an unbalanced binary outcome.我有一个包含八列的数据集和一个不平衡的二进制结果。 For scoring I use f1 and I use KFold with 10 splits.对于评分,我使用 f1 并使用 KFold 进行 10 次拆分。 I was hoping if someone could spot something which is off and I should look at?我希望是否有人能发现一些不对劲的东西,我应该看看? Thank you!谢谢!

I use the following code:我使用以下代码:

model_parameters = {
    "GaussianNB": {     
    },
    "DecisionTreeClassifier": {
        'min_samples_leaf': range(5, 9),
        'max_depth': [None, 0, 1, 2, 3, 4]
    },
    "KNeighborsClassifier": {
        'n_neighbors': range(1, 10),
        'weights': ["distance", "uniform"]
    },
    "SVM": {
        'kernel': ["poly"],
        'C': np.linspace(0, 15, 30)
    },
    "LogisticRegression": {
        'C': np.linspace(0, 15, 30),
        'penalty': ["l1", "l2", "elasticnet", "none"]
    }
}

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4)
n_splits = 10
scoring_method = make_scorer(lambda true_target, prediction: f1_score(true_target, prediction, average="micro"))
cv = KFold(n_splits=n_splits, random_state=random_state, shuffle=True)

for model_name, parameters in model_parameters.items():

    # Models is a dict with 5 classifiers
    model = models[model_name]
    grid_search = GridSearchCV(model, parameters, cv=cv, n_jobs=-1, scoring=scoring_method, verbose=False).fit(X_train, y_train)
    
    cvScore = cross_val_score(grid_search.best_estimator_, X_test, y_test, cv=cv, scoring='f1').mean()
    classDict[model_name] = cvScore

If your classes are unbalanced, when you do Kfold you should keep the proportion between the two targets.如果你的类不平衡,当你做 Kfold 时,你应该保持两个目标之间的比例。

Having folds unbalanced can lead to very poor results折叠不平衡会导致非常糟糕的结果

check Stratified K-Folds cross-validator检查分层 K 折交叉验证器

Provides train/test indices to split data in train/test sets.提供训练/测试索引以拆分训练/测试集中的数据。

This cross-validation object is a variation of KFold that returns stratified folds.此交叉验证 object 是返回分层折叠的 KFold 的变体。 The folds are made by preserving the percentage of samples for each class.通过保留每个 class 的样本百分比来进行折叠。

There are also a lot of techniques to handle unbalanced dataset.还有很多技术可以处理不平衡的数据集。 Based on the context:基于上下文:

  • upsampling the minority class (using for example the resample from sklearn )对少数 class 进行上采样(例如使用来自 sklearn的重采样)
  • under sampling the majority class (also this lib has some useful tools to do both under\up sampling)对大多数 class 进行采样(此也有一些有用的工具可以进行欠\上采样)
  • handle the unbalance with your specific ML model使用您的特定 ML model 处理不平衡

For example, in SVC, there is an argument when you create the model, class_weight='balanced'比如在SVC中,创建model时有一个参数, class_weight='balanced'

clf_3 = SVC(kernel='linear', 
            class_weight='balanced', # penalize
            probability=True)

which will penalize more the errors on minority class.这将对少数 class 的错误进行更多的惩罚。

You can change your config as such:您可以这样更改您的配置:

"SVM": {
        'kernel': ["poly"],
        'C': np.linspace(0, 15, 30),
        'class_weight': 'balanced'

    }

For LogisticRegression you can set the weights instead, reflecting the proportion of your classes对于 LogisticRegression,您可以改为设置权重,以反映类别的比例

LogisticRegression(class_weight={0:1, 1:10}) # if problem is a binary one

changing the grid search dict in such way:以这种方式更改网格搜索字典:

"LogisticRegression": {
        'C': np.linspace(0, 15, 30),
        'penalty': ["l1", "l2", "elasticnet", "none"],
        'class_weight':{0:1, 1:10}
    }

Anyway the approach depends on the used model. For neural.network for example, you can change the loss function to penalize the minority class with a weighted calculation (the same of the logistic regression)无论如何,该方法取决于所使用的 model。例如,对于 neural.network,您可以通过加权计算(与逻辑回归相同)更改损失 function 以惩罚少数 class

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM