微调超参数不会提高分类器的分数
Fine Tuning hyperparameters doesn't improve score of classifiers
我遇到了一个问题,即使用 GridSearchCV 微调超参数并没有真正改进我的分类器。我认为改进应该比这更大。我目前的代码对分类器的最大改进是大约 +-0.03。我有一个包含八列的数据集和一个不平衡的二进制结果。对于评分,我使用 f1 并使用 KFold 进行 10 次拆分。我希望是否有人能发现一些不对劲的东西,我应该看看?谢谢!
我使用以下代码:
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
如果你的class不平衡,做Kfold的时候要保持两个目标的比例。
折叠不平衡会导致非常糟糕的结果
检查Stratified K-Folds cross-validator
Provides train/test indices to split data in train/test sets.
This cross-validation object is a variation of KFold that returns
stratified folds. The folds are made by preserving the percentage of
samples for each class.
处理不平衡数据集的技术也有很多。基于上下文:
- 对少数class进行上采样(例如使用resample from sklearn)
- 对大多数 class 进行抽样(这 lib 也有一些有用的工具可以同时进行 under\up 抽样)
- 使用您的特定 ML 模型处理不平衡
比如在SVC中,创建模型的时候有一个参数,class_weight='balanced'
clf_3 = SVC(kernel='linear',
class_weight='balanced', # penalize
probability=True)
这将对少数人的错误进行更多惩罚 class。
您可以这样更改您的配置:
"SVM": {
'kernel': ["poly"],
'C': np.linspace(0, 15, 30),
'class_weight': 'balanced'
}
对于 LogisticRegression,您可以改为设置权重,以反映您的 classes
的比例
LogisticRegression(class_weight={0:1, 1:10}) # if problem is a binary one
以这种方式更改网格搜索字典:
"LogisticRegression": {
'C': np.linspace(0, 15, 30),
'penalty': ["l1", "l2", "elasticnet", "none"],
'class_weight':{0:1, 1:10}
}
无论如何,方法取决于使用的模型。以神经网络为例,你可以改变损失函数来惩罚少数人 class 加权计算(与逻辑回归相同)
我遇到了一个问题,即使用 GridSearchCV 微调超参数并没有真正改进我的分类器。我认为改进应该比这更大。我目前的代码对分类器的最大改进是大约 +-0.03。我有一个包含八列的数据集和一个不平衡的二进制结果。对于评分,我使用 f1 并使用 KFold 进行 10 次拆分。我希望是否有人能发现一些不对劲的东西,我应该看看?谢谢!
我使用以下代码:
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
如果你的class不平衡,做Kfold的时候要保持两个目标的比例。
折叠不平衡会导致非常糟糕的结果
检查Stratified K-Folds cross-validator
Provides train/test indices to split data in train/test sets.
This cross-validation object is a variation of KFold that returns stratified folds. The folds are made by preserving the percentage of samples for each class.
处理不平衡数据集的技术也有很多。基于上下文:
- 对少数class进行上采样(例如使用resample from sklearn)
- 对大多数 class 进行抽样(这 lib 也有一些有用的工具可以同时进行 under\up 抽样)
- 使用您的特定 ML 模型处理不平衡
比如在SVC中,创建模型的时候有一个参数,class_weight='balanced'
clf_3 = SVC(kernel='linear',
class_weight='balanced', # penalize
probability=True)
这将对少数人的错误进行更多惩罚 class。
您可以这样更改您的配置:
"SVM": {
'kernel': ["poly"],
'C': np.linspace(0, 15, 30),
'class_weight': 'balanced'
}
对于 LogisticRegression,您可以改为设置权重,以反映您的 classes
的比例LogisticRegression(class_weight={0:1, 1:10}) # if problem is a binary one
以这种方式更改网格搜索字典:
"LogisticRegression": {
'C': np.linspace(0, 15, 30),
'penalty': ["l1", "l2", "elasticnet", "none"],
'class_weight':{0:1, 1:10}
}
无论如何,方法取决于使用的模型。以神经网络为例,你可以改变损失函数来惩罚少数人 class 加权计算(与逻辑回归相同)