使用帶有交叉驗證的網格搜索進行參數估計?

此案例顯示了如何通過交叉驗證來優化分類器。交叉驗證是通過在開發集上(development set)使用sklearn.model_selection.GridSearchCV對象完成的,開發集是已有標簽的數據集的一半數據。

然后,在模型選擇步驟中未使用的是評估集(evalution set),我們在評估集上測量所選超參數和訓練后模型的性能。

之后,在模型選擇步驟中未使用的數據是評估集evalutiaon上測量所選超參數和訓練后模型的性能。

有關可用于模型選擇的工具的更多詳細信息,請參見交叉驗證:評估估計器性能調整估計器的超參數部分。

輸出:

# Tuning hyper-parameters for precision

Best parameters set found on development set:

{'C'10'gamma'0.001'kernel''rbf'}

Grid scores on development set:

0.986 (+/-0.016for {'C'1'gamma'0.001'kernel''rbf'}
0.959 (+/-0.028for {'C'1'gamma'0.0001'kernel''rbf'}
0.988 (+/-0.017for {'C'10'gamma'0.001'kernel''rbf'}
0.982 (+/-0.026for {'C'10'gamma'0.0001'kernel''rbf'}
0.988 (+/-0.017for {'C'100'gamma'0.001'kernel''rbf'}
0.983 (+/-0.026for {'C'100'gamma'0.0001'kernel''rbf'}
0.988 (+/-0.017for {'C'1000'gamma'0.001'kernel''rbf'}
0.983 (+/-0.026for {'C'1000'gamma'0.0001'kernel''rbf'}
0.974 (+/-0.012for {'C'1'kernel''linear'}
0.974 (+/-0.012for {'C'10'kernel''linear'}
0.974 (+/-0.012for {'C'100'kernel''linear'}
0.974 (+/-0.012for {'C'1000'kernel''linear'}

Detailed classification report:

The model is trained on the full development set.
The scores are computed on the full evaluation set.

              precision    recall  f1-score   support

           0       1.00      1.00      1.00        89
           1       0.97      1.00      0.98        90
           2       0.99      0.98      0.98        92
           3       1.00      0.99      0.99        93
           4       1.00      1.00      1.00        76
           5       0.99      0.98      0.99       108
           6       0.99      1.00      0.99        89
           7       0.99      1.00      0.99        78
           8       1.00      0.98      0.99        92
           9       0.99      0.99      0.99        92

    accuracy                           0.99       899
   macro avg       0.99      0.99      0.99       899
weighted avg       0.99      0.99      0.99       899


# Tuning hyper-parameters for recall

Best parameters set found on development set:

{'C'10'gamma'0.001'kernel''rbf'}

Grid scores on development set:

0.986 (+/-0.019for {'C'1'gamma'0.001'kernel''rbf'}
0.957 (+/-0.028for {'C'1'gamma'0.0001'kernel''rbf'}
0.987 (+/-0.019for {'C'10'gamma'0.001'kernel''rbf'}
0.981 (+/-0.028for {'C'10'gamma'0.0001'kernel''rbf'}
0.987 (+/-0.019for {'C'100'gamma'0.001'kernel''rbf'}
0.982 (+/-0.026for {'C'100'gamma'0.0001'kernel''rbf'}
0.987 (+/-0.019for {'C'1000'gamma'0.001'kernel''rbf'}
0.982 (+/-0.026for {'C'1000'gamma'0.0001'kernel''rbf'}
0.971 (+/-0.010for {'C'1'kernel''linear'}
0.971 (+/-0.010for {'C'10'kernel''linear'}
0.971 (+/-0.010for {'C'100'kernel''linear'}
0.971 (+/-0.010for {'C'1000'kernel''linear'}

Detailed classification report:

The model is trained on the full development set.
The scores are computed on the full evaluation set.

              precision    recall  f1-score   support

           0       1.00      1.00      1.00        89
           1       0.97      1.00      0.98        90
           2       0.99      0.98      0.98        92
           3       1.00      0.99      0.99        93
           4       1.00      1.00      1.00        76
           5       0.99      0.98      0.99       108
           6       0.99      1.00      0.99        89
           7       0.99      1.00      0.99        78
           8       1.00      0.98      0.99        92
           9       0.99      0.99      0.99        92

    accuracy                           0.99       899
   macro avg       0.99      0.99      0.99       899
weighted avg       0.99      0.99      0.99       899

(譯者注:輸出中的英文為打印內容,是由代碼中打印的英文所致,故不進行翻譯)

from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import classification_report
from sklearn.svm import SVC

print(__doc__)

# 導入數據集
digits = datasets.load_digits()

# 要將分類器應用于此數據,我們需要將圖像拉平(譯者注:即降維),以將數據轉換為(樣本,特征)結構的矩陣:
n_samples = len(digits.images)
X = digits.images.reshape((n_samples, -1))
y = digits.target

# 將數據集分割為兩個等大的部分
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.5, random_state=0)

# 使用交叉驗證設置參數
tuned_parameters = [{'kernel': ['rbf'], 'gamma': [1e-31e-4],
                     'C': [1101001000]},
                    {'kernel': ['linear'], 'C': [1101001000]}]

scores = ['precision''recall']

for score in scores:
    print("# Tuning hyper-parameters for %s" % score)
    print()

    clf = GridSearchCV(
        SVC(), tuned_parameters, scoring='%s_macro' % score
    )
    clf.fit(X_train, y_train)

    print("Best parameters set found on development set:")
    print()
    print(clf.best_params_)
    print()
    print("Grid scores on development set:")
    print()
    means = clf.cv_results_['mean_test_score']
    stds = clf.cv_results_['std_test_score']
    for mean, std, params in zip(means, stds, clf.cv_results_['params']):
        print("%0.3f (+/-%0.03f) for %r"
              % (mean, std * 2, params))
    print()

    print("Detailed classification report:")
    print()
    print("The model is trained on the full development set.")
    print("The scores are computed on the full evaluation set.")
    print()
    y_true, y_pred = y_test, clf.predict(X_test)
    print(classification_report(y_true, y_pred))
    print()

# 請注意,這個問題太容易了:超參數平穩段太平坦了,并且輸出模型對于精度和召回率都具有相同的預測質量。

腳本的總運行時間:(0分鐘5.437秒)