超参数调优后精度保持不变

Posted

技术标签:

【中文标题】超参数调优后精度保持不变【英文标题】:After hyperparameter tuning accuracy remains the same 【发布时间】:2021-12-31 17:27:32 【问题描述】:

我试图对参数进行超调,但是在我这样做之后,准确度分数根本没有改变,我做错了什么?

 # Log reg
from sklearn.linear_model import LogisticRegression
logreg = LogisticRegression(C=0.3326530612244898,max_iter=100,tol=0.01)
logreg.fit(X_train,y_train)

from sklearn.metrics import confusion_matrix
​
y_pred = logreg.predict(X_test)
​
print('Accuracy of log reg is: ', logreg.score(X_test,y_test))
​
confusion_matrix(y_test,y_pred)
 # 0.9181286549707602 - acurracy before tunning

输出:

Accuracy of log reg is:  0.9181286549707602
array([[ 54,   9],
       [  5, 103]])

这是我使用 Grid Search CV:

from sklearn.model_selection import GridSearchCV
params ='tol':[0.01,0.001,0.0001],
        'max_iter':[100,150,200],
        'C':np.linspace(1,20)/10

grid_model = GridSearchCV(logreg,param_grid=params,cv=5)
grid_model_result = grid_model.fit(X_train,y_train)
print(grid_model_result.best_score_,grid_model_result.best_params_)

输出:

0.8867405063291139 'C': 0.3326530612244898, 'max_iter': 100, 'tol': 0.01

【问题讨论】:

所以第一次acc是:0.9181286549707602,第二次acc是:0.8867405063291139? 在 GridSearchCV 期间,您执行 5 倍交叉验证,这意味着 80% 的 X_train 将用于训练您的逻辑回归算法,而第一个输出基于 100% 训练的模型X_火车。因此,训练期间这 20% 的数据差异可能会导致评估准确度的差异。 @meistef 所以 GridSearchCV 总是有可能导致精度降低?我虽然总是应该提高准确性 @RuslanPylypiuk GridSearchCV 的原理是找到可以确保获得最高准确度的最佳超参数集。但是在这里,您采用了最优参数集,然后使用这些最优参数在更多数据上重新训练模型(完整的 X_train 集而不是 80% X_train 集)。第一个模型有机会从更多数据以及最佳超参数中学习。两个实例之间的准确性差异很小,我并不感到惊讶。 @meistef 我该如何训练剩下的 80%? 【参考方案1】:

问题在于,在第一个块中,您评估模型在测试集上的性能,而在 GridSearchCV 中,您只查看超参数优化后在训练集上的性能。

下面的代码显示,当用于预测测试集标签时,这两个过程在准确度方面表现同样出色(~0.93)

注意,您可能需要考虑将超参数网格与其他求解器和更大范围的max_iter 一起使用,因为我收到了收敛警告。

# Load packages
import numpy as np
import pandas as pd 
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import GridSearchCV
from sklearn import metrics

# Load the dataset and split in X and y
df = pd.read_csv('Breast_cancer_data.csv')
X = df.iloc[:, 0:5]
y = df.iloc[:, 5]

# Perform train and test split (80/20)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Initialize a model
Log = LogisticRegression(n_jobs=-1)

# Initialize a parameter grid
params = ['tol':[0.01,0.001,0.0001],
        'max_iter':[100,150,200],
        'C':np.linspace(1,20)/10]

# Perform GridSearchCV and store the best parameters
grid_model = GridSearchCV(Log,param_grid=params,cv=5)
grid_model_result = grid_model.fit(X_train,y_train)
best_param = grid_model_result.best_params_

# This step is only to prove that both procedures actually result in the same accuracy score
Log2 = LogisticRegression(C=best_param['C'], max_iter=best_param['max_iter'], tol=best_param['tol'], n_jobs=-1)
Log2.fit(X_train, y_train)

# Perform two predictions one straight from the GridSearch and the other one with manually inputting the best params
y_pred1 = grid_model_result.best_estimator_.predict(X_test)
y_pred2 = Log2.predict(X_test)

# Compare the accuracy scores and see that both are the same
print("Accuracy:",metrics.accuracy_score(y_test, y_pred1))
print("Accuracy:",metrics.accuracy_score(y_test, y_pred2))

【讨论】:

如果我在培训之前使用过,我应该在测试中执行 GridSearch 吗? 我不明白你在说什么,但是你执行 GridSearchCV 来找到在训练期间产生最高分数的超参数组合。因此,您只在训练集上执行 GridSearchCV。 我仍然很难理解为什么它们是相同的,正如您在新问题中看到的那样,我修复并完成了正确的 GridSearchCV 但也没有改变 也许可以创建一个关于 GridSearchCV 的新问题,并尝试更深入地解释您不了解的内容。

以上是关于超参数调优后精度保持不变的主要内容,如果未能解决你的问题,请参考以下文章

[Spark2.0]ML 调优:模型选择和超参数调优

Spark2.0机器学习系列之2:基于Pipeline交叉验证ParamMap的模型选择和超参数调优

hbase写ES丢数据参数调优总结

hbase写ES丢数据参数调优总结

机器学习超参数调优

Tensorflow 模型的超参数调优