網格搜索最佳參數GridSearchCV
class sklearn.model_selection.
GridSearchCV
(estimator, param_grid, scoring=None, fit_params=None, n_jobs=1,iid=True, refit=True, cv=None, verbose=0, pre_dispatch='2*n_jobs', error_score='raise',return_train_score=True)
對分類器的指定參數值進行詳盡搜索
重要的成員是fit和predict
分類器的參數通過參數網格上的交叉驗證網格搜索進行優化
參數: |
|
---|
scikit-learn分類器接口,估計者需要提供分數函數。如estimator = GradientBoostingClassifier(參數設置)
2.param_grid : dict or list of dictionaries
具有參數名稱(字符串)作爲鍵的字典和要實數值的參數設置的列表,或者這些字典的列表,在這種情況下,會探索列表中每個字典跨越的網格。 這樣可以根據任何參數設置的順序進行最優參數的搜索。
如param_test = {'n_estimators':range(20,81,10)}
param_dist = {"max_depth": [3, None],
"max_features": [1,5,7,11],
"min_samples_split": [1,5,7,11],
"min_samples_leaf": [1,5,7,11],
"bootstrap": [True, False],
"criterion": ["gini", "entropy"]}
3.scoring : string, callable or None, default=None
字符串(見模型評估文檔)或具有簽名記分器(estimator,X,y)的可調用對象/函數。 如果沒有,則使用估計器的分數法
如scoring='roc_auc'
4.fit_params : dict, optional
要傳遞給擬合方法的參數
5.n_jobs : int, default=1
並行運行的作業數
6.pre_dispatch : int, or string, optional
控制在並行執行期間調度的作業數。 減少這個數字可以有效地避免在調度更多的作業時不超過CPU可以處理的內存消耗。 該參數可以是:
None,在這種情況下,所有的作業立即被創建和產生。 將其用於輕量級和快速運行的作業,以避免由於按需生成作業導致的延遲
int,給出所產生的總作業的確切數量
string,給出一個表達式作爲n_jobs的函數,如'2 * n_jobs'
7.iid : boolean, default=True
如果爲True,則假定數據在摺疊中相同分佈,損失最小化是每個樣本的總損失,而不是摺疊的平均損失。
8.cv : int, cross-validation generator or an iterable, optional
確定交叉驗證分裂策略。 cv的可能輸入是:
無,使用默認的3折交叉驗證,
整數,用於指定一個(分層)KFold中的摺疊數。一個用作交叉驗證生成器的對象。一個可迭代的生成列,測試分割。對於整數/無輸入,如果估計器是分類器,y是 使用二進制或多類,使用StratifiedKFold。 在所有其他情況下,使用KFold。
cv=5
9.refit : boolean, default=True
使用整個數據集重新設計最佳的估計值。 如果爲“False”,則在擬合之後不可能使用此GridSearchCV實例進行預測。
10.verbose : integer
控制冗長度:越高,消息越多
11.error_score : ‘raise’ (default) or numeric
12.return_train_score : boolean, default=True
如果“False”,cv_results_屬性將不包括訓練分數。
屬性:
search.cv_results _ ['params'] [search.best_index_]中的dict給出了最佳模型的參數設置,給出了最高的平均分數(search.best_score_)。
方法
decision_function
(*args, **kwargs)調用具有最佳發現參數的估計器的decision_function[source]
Parameters: |
X : indexable, length n_samples
|
---|
fit
(X, y=None, groups=None)運行擬合所有參數集。[source]
Parameters: |
X : array-like, shape = [n_samples, n_features]
y : array-like, shape = [n_samples] or [n_samples, n_output], optional
groups : array-like, with shape (n_samples,), optional
|
---|
get_params
(deep=True)獲取此估計器的參數。[source]
Parameters: |
deep : boolean, optional
|
---|---|
Returns: |
params : mapping of string to any
|
inverse_transform
(*args, **kwargs)使用最好的參數調用估計器上的inverse_transform。[source]
Parameters: |
Xt : indexable, length n_samples
|
---|
predict
(*args, **kwargs)使用最好的參數調用估計器的預測。[source]
Parameters: |
X : indexable, length n_samples
|
---|
predict_log_proba
(*args, **kwargs)在具有最佳發現參數的估計器上調用predict_log_proba。[source]
Parameters: |
X : indexable, length n_samples
|
---|
predict_proba
(*args, **kwargs)[source]
Parameters: |
X : indexable, length n_samples
|
---|
score
(X, y=None)如果估計器已被重新設計,則返回給定數據的分數[source]
Parameters: |
X : array-like, shape = [n_samples, n_features]
y : array-like, shape = [n_samples] or [n_samples, n_output], optional
|
---|---|
Returns: |
score : float |
predictors
= [x for x in train.columns if x not in [target, IDcol]]
param_test1 = {'n_estimators':range(20,101,10)}#代表從20到81,間隔10(不包含81)
gsearch1 = GridSearchCV(estimator = GradientBoostingClassifier(learning_rate=0.1, min_samples_split=500,min_samples_leaf=50,max_depth=8,max_features='sqrt',subsample=0.8,random_state=10),
param_grid = param_test1, scoring='roc_auc',n_jobs=4,iid=False, cv=5)
gsearch1.fit(train[predictors],train[target])
gsearch1.grid_scores_, gsearch1.best_params_, gsearch1.best_score_