比较在scikit-learn中调整超参数的方法

far*_*mer 9 python machine-learning scikit-learn cross-validation hyperparameters

这篇文章是关于LogisticRegressionCV,GridSearchCV和cross_val_score之间的区别。请考虑以下设置:

import numpy as np
from sklearn.datasets import load_digits
from sklearn.linear_model import LogisticRegression, LogisticRegressionCV
from sklearn.model_selection import train_test_split, GridSearchCV, \
     StratifiedKFold, cross_val_score
from sklearn.metrics import confusion_matrix

read = load_digits()
X, y = read.data, read.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.3)
Run Code Online (Sandbox Code Playgroud)

在惩罚逻辑回归中,我们需要设置控制正则化的参数C。scikit-learn中有3种通过交叉验证找到最佳C的方法。

Logistic回归

clf = LogisticRegressionCV (Cs = 10, penalty = "l1",
    solver = "saga", scoring = "f1_macro")
clf.fit(X_train, y_train)
confusion_matrix(y_test, clf.predict(X_test))
Run Code Online (Sandbox Code Playgroud)

旁注:文档指出,SAGA和LIBLINEAR是L1惩罚的唯一优化器,而SAGA对于大型数据集则更快。不幸的是,热启动仅适用于Newton-CG和LBFGS。

GridSearchCV

clf = LogisticRegression (penalty = "l1", solver = "saga", warm_start = True)
clf = GridSearchCV (clf, param_grid = {"C": np.logspace(-4, 4, 10)}, scoring = "f1_macro")
clf.fit(X_train, y_train)
confusion_matrix(y_test, clf.predict(X_test))
result = clf.cv_results_
Run Code Online (Sandbox Code Playgroud)

cross_val_score

cv_scores = {}
for val in np.logspace(-4, 4, 10):
    clf = LogisticRegression (C = val, penalty = "l1",
        solver = "saga", warm_start = True)
    cv_scores[val] = cross_val_score (clf, X_train, y_train,
        cv = StratifiedKFold(), scoring = "f1_macro").mean()

clf = LogisticRegression (C = max(cv_scores, key = cv_scores.get),
        penalty = "l1", solver = "saga", warm_start = True)
clf.fit(X_train, y_train)
confusion_matrix(y_test, clf.predict(X_test))
Run Code Online (Sandbox Code Playgroud)

问题

  1. 我是否以3种方式正确执行了交叉验证?
  2. 这三种方式都等效吗?如果不是,是否可以通过更改代码使其等效?
  3. 就优雅,速度或任何标准而言,哪种方法最好?(换句话说,为什么在scikit-learn中有3种交叉验证方式?)

欢迎对任何一个问题提供简单的答案;我意识到它们有些长,但是希望它们是scikit-learn中超参数选择的一个很好的总结。

Nik*_*ble 2

关于3 - 为什么scikit-learn中有3种交叉验证方式?

让我们用聚类来类比一下:scikit-learn 中实现了多种聚类算法。

为什么这样?难道不是一个比另一个更好吗?

您可能会回答: 嗯,它们是不同的算法,每种算法都有自己的优点和缺点。

Logistic回归CV

实现带有内置交叉验证支持的逻辑回归,根据评分属性找到最佳的 C 和 l1_ratio 参数。

因此,LogisticRegressionCV 是 Logistic 回归的“高级”版本,因为它不需要用户自己优化超参数 C l1_ratio。

网格搜索简历

用户指南指出:

GridSearchCV 提供的网格搜索从 param_grid 参数指定的参数值网格中详尽地生成候选者。例如,以下

param_grid = [
  {'C': [1, 10, 100, 1000], 'kernel': ['linear']},
  {'C': [1, 10, 100, 1000], 'gamma': [0.001, 0.0001], 'kernel': ['rbf']},
 ]
Run Code Online (Sandbox Code Playgroud)

在这里,您实际上可以指定要进行网格搜索的参数以及值/步骤。与 LogisticRegressionCV 相比,主要区别在于 GridSearchCV 可以用于任何分类器/回归器。最重要的是,您还可以将 GridSearchCV 用于不在 sklearn 上的任何模型,只要它们同时具有拟合和预测方法即可。

除了提供性能最佳的模型之外,还使用以下方法:

clf = GridSearchCV (clf, param_grid = {"C": np.logspace(-4, 4, 10)}, scoring = "f1_macro")
clf.fit(X_train, y_train)
Run Code Online (Sandbox Code Playgroud)

GridSearchCV 还包含对最佳模型的广泛评估:

cv_results_ :numpy(屏蔽)ndarrays 的字典 一个字典,其中键作为列标题,值作为列,可以导入到 pandas DataFrame 中。

交叉验证分数

您可能希望专门在保留数据集上评估您的模型。无需搜索参数,您就可以评估单个模型。这是您使用 cross_val_score 的时候。

TLDR:都是不同的方法,每种方法都有不同的用途。LogisticRegressionCV 仅与逻辑回归相关。GridSearchCV 是最详尽和最通用的变体,其中包括评估分数以及最佳分类器。cross_val_score 只是一个评估,并且在仅评估时首选使用。