簡體   English   中英

RandomizedSearchCV 是否自動包含傳遞給構造函數的默認 model 參數?

[英]does RandomizedSearchCV automatically include the default model parameters that are passed to the constructor?

假設我像這樣創建了一個RandomizedSearchCV

searcher = model_selection.RandomizedSearchCV(estimator = RandomForestClassifier(),
                                            param_distributions = random_grid,
                                            n_iter = 20, # Number of parameter combinations to try
                                            cv     = 3,  # Number of folds for k-fold validation 
                                            n_jobs = -1) # Use all processors to compute in parallel
search = searcher.fit(x_train, y_train)
search.best_params_

n_iter告訴我們搜索將測試多少個組合。 對我來說,知道作為 20 種組合的一部分或除了這 20 種組合之外,還包括默認的 model 參數非常重要。 有誰知道這是真的還是假的?

他們不是(可以說,如果是這樣的話,那就太奇怪了)。

嘗試的參數組合的詳細值在擬合的RandomizedSearchCV object 的屬性cv_results_中返回。 改編文檔中的示例(使用默認的n_iter = 10 ),我們得到:

from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import RandomizedSearchCV
from scipy.stats import uniform

iris = load_iris()
logistic = LogisticRegression(solver='saga', tol=1e-2, max_iter=200,
                               random_state=0)
distributions = dict(C=uniform(loc=0, scale=4),
                      penalty=['l2', 'l1'])
clf = RandomizedSearchCV(logistic, distributions, random_state=0)

search = clf.fit(iris.data, iris.target)
search.cv_results_

您可以直接檢查search.cv_results_返回的字典,也可以將其導入 pandas dataframe 以獲得更緊湊的表示:

import pandas as pd
df = pd.DataFrame.from_dict(search.cv_results_)
df['params']
# result:
0      {'C': 2.195254015709299, 'penalty': 'l1'}
1     {'C': 3.3770629943240693, 'penalty': 'l1'}
2     {'C': 2.1795327319875875, 'penalty': 'l1'}
3     {'C': 2.4942547871438894, 'penalty': 'l2'}
4       {'C': 1.75034884505077, 'penalty': 'l2'}
5    {'C': 0.22685190926977272, 'penalty': 'l2'}
6     {'C': 1.5337660753031108, 'penalty': 'l2'}
7     {'C': 3.2486749151019727, 'penalty': 'l2'}
8     {'C': 2.2721782443757292, 'penalty': 'l1'}
9       {'C': 3.34431505414951, 'penalty': 'l2'}

從這里可以清楚地看出, LogisticRegression默認C=1.0包含在搜索網格中。

如果您有任何理由使用其默認參數評估 model 的性能,您應該單獨進行 - 可以說它非常簡單(只需 2 行代碼)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM