sklearn.ensemble.GradientBoostingClassifier?

class sklearn.ensemble.GradientBoostingClassifier(*, loss='deviance', learning_rate=0.1, n_estimators=100, subsample=1.0, criterion='friedman_mse', min_samples_split=2, min_samples_leaf=1, min_weight_fraction_leaf=0.0, max_depth=3, min_impurity_decrease=0.0, min_impurity_split=None, init=None, random_state=None, max_features=None, verbose=0, max_leaf_nodes=None, warm_start=False, presort='deprecated', validation_fraction=0.1, n_iter_no_change=None, tol=0.0001, ccp_alpha=0.0)

源碼

梯度增強分類。

GB采用正向階段的方式建立加性模型;它允許優化任意可微的損失函數。在每一階段,n_classes_回歸樹都擬合在二項或多項偏差損失函數的負梯度上。二元分類是一種特殊的情況,其中只有一個回歸樹被誘導。

用戶指南中閱讀更多內容。

參數 說明
loss {‘deviance’, ‘exponential’}, default=’deviance’
待優化的損失函數。“偏差”是指具有概率輸出的分類偏差(= logistic regression)。對于損失“指數”梯度增強恢復AdaBoost算法。
learning_rate float, default=0.1
學習率通過learning_rate縮小每棵樹的貢獻。learning_rate和n_estimators之間存在權衡。
n_estimators int, default=100
要執行的推進階段的數量。梯度增強對過擬合具有較強的魯棒性,因此大量的梯度增強通常能取得較好的效果。
subsample float, default=1.0
用于擬合單個基本學習者的樣本比例。如果小于1.0,會導致隨機梯度提升。subsample與參數n_estimators交互。選擇subsample < 1.0會導致方差的減少和偏差的增加。
criterion {‘friedman_mse’, ‘mse’, ‘mae’}, default=’friedman_mse’
該函數用來測量分割的質量。支持的標準是' friedman_mse ',即由Friedman提出的具有改進分數的均方誤差, mse的平均平方誤差,和' mae '的平均絕對誤差。默認值' friedman_mse '通常是最好的,因為在某些情況下它可以提供更好的近似值。
版本0.18中的新功能
min_samples_leaf 分割一個內部節點所需的最小樣本數:
- 如果為int,則認為min_samples_leaf是最小值。
- 如果為float,min_samples_leaf則為分數, 是每個節點的最小樣本數。
ceil(min_samples_leaf * n_samples)
在版本0.18中更改:添加了分數的浮點值。
min_samples_split int or float, default=1
一個葉節點上所需的最小樣本數。只有當它在每個左右分支中都留下至少min_samples_leaf訓練樣本時,任何深度的分割點才會被考慮。這可能會產生平滑模型的效果,特別是在回歸中。
- 如果為int,則認為min_samples_split是最小值。
- 如果為float,min_samples_split則為分數, 是每個拆分的最小樣本數。
ceil(min_samples_split * n_samples)
在版本0.18中更改:添加了分數的浮點值。
min_weight_fraction_leaf float, default=0.0
一個葉節點上所需的(所有輸入樣本的)總權重的最小加權分數。當不提供sample_weight時,樣本的重量相等。
max_depth int, default=3
單個回歸估計量的最大深度。最大深度限制了樹中的節點數。優化此參數以獲得最佳性能;最佳值取決于輸入變量的相互作用。
min_impurity_decrease float, default=0.0
如果節點分裂會導致雜質的減少大于或等于該值,則該節點將被分裂。
加權減少雜質的方程式如下:
N_t / N * (impurity - N_t_R / N_t * right_impurity - N_t_L / N_t * left_impurity)
其中,N是樣本總數,N_t是當前節點上N_t_L的樣本數,是左子節點中的樣本N_t_R數,是右子節點中的樣本數。
NN_tN_t_R并且N_t_L都指的是加權和,如果sample_weight獲得通過。
版本0.19中的新功能。
min_impurity_split float, default=None
樹提升提前停止的閾值。如果節點的雜質高于閾值,則該節點將分裂,否則為葉。
- 從版本0.19min_impurity_split開始不推薦使用:在版本0.19中不再推薦使用 min_impurity_decrease。的默認值 min_impurity_split在0.23中從1e-7更改為0,并將在0.25中刪除。使用min_impurity_decrease代替。
init estimator or ‘zero’, default=None
用于計算初始預測的估計對象。init必須提供fitpredict_proba。如果為“零”,初始的原始預測被設置為零。默認情況下,使用了預測類先驗的DummyEstimator
random_state int, RandomState, default=None
控制每個增強迭代器上的樹估計器隨機種子。此外,它還可以在每次拆分時控制特性的隨機排列(更多細節請參見注釋)。如果n_iter_no_change不是None,它還控制訓練數據的隨機分割以獲得驗證集。在多個函數調用之間傳遞可重復輸出的int
有關更多詳細信息,請參見Glossary
max_features {“auto”, “sqrt”, “log2”}, int or float, default=”auto”
尋找最佳分割時要考慮的功能數量:
- 如果為int,則max_features在每個分割處考慮特征。
- 如果為float,max_features則為小數,并 在每次拆分時考慮要素。
- 如果為auto,則為max_features=sqrt(n_features)
- 如果是sqrt,則max_features=sqrt(n_features)
- 如果為log2,則為max_features=log2(n_features)
- 如果為None,則max_features=n_features
注意:直到找到至少一個有效的節點樣本分區,分割的搜索才會停止,即使它需要有效檢查多于max_features個數的要素也是如此。
verbose int, default=0
啟用詳細輸出。如果是1,那么它會在一段時間內打印進度和性能(樹越多,頻率越低)。如果大于1,則輸出每棵樹的進度和性能。
max_leaf_nodes int, default=None
以最佳優先的方式使用max_leaf_nodes來種植樹。最佳節點定義為雜質的相對減少。如果沒有,則葉節點數沒有限制。
warm_start bool, default=False
當設置為True時,重用前面調用的解決方案來適應并向集成添加更多的評估器,否則,只會擬合完整的新森林。有關更多詳細信息,請參見Glossary
presort deprecated, default=’deprecated’
此參數不建議使用,將在v0.24中刪除。
自版本0.22以來已棄用。
validation_fraction float, default=0.1
為提前停止而預留作為驗證集的訓練數據的比例。必須在0和1之間。僅在n_iter_no_change被設置為整數時使用。
0.20版中的新功能。
n_iter_no_change int, default=None
n_iter_no_change用于決定在驗證分數沒有提高時是否使用早期停止來終止訓練。默認情況下,它被設置為None來禁用提前停止。如果設置為一個數字,它將把訓練數據的validation_fraction大小放在一邊作為驗證,并在之前所有的n_iter_no_change迭代次數中驗證分數沒有提高時終止訓練。分裂是分層的。
0.20版中的新功能。
tol float, default=1e-4
提前停止的的容忍度。當n_iter_no_change迭代器(如果設置為一個數字)對tol的損失不再增加時,訓練停止。
0.20版中的新功能。
ccp_alpha non-negative float, default=0.0
復雜度參數用于最小代價復雜度剪枝。將選擇代價復雜度最大且小于ccp_alpha的子樹。默認情況下,不執行修剪。
有關更多詳細信息,請參見Minimal Cost-Complexity Pruning
0.22版中的新功能。
屬性 說明
n_estimators_ int
通過提前停止選擇的估計器的數量(如果指定了n_iter_no_change)。否則,它將被設置為n_estimators
0.20版中的新功能。
feature_importances_ ndarray of shape (n_features,)
基于雜質的特性重要性。
oob_improvement_ ndarray of shape (n_estimators,)
相對于之前的迭代,袋外樣本損失(=偏差)的改進。oob_improvement_[0]init估計器的第一階段損失的改進。僅當subsample < 1.0時可用。
train_score_ ndarray of shape (n_estimators,)
i個得分train_score_[i]是模型在第i次迭代時對袋裝樣本的偏差度(= loss)。如果subsample== 1,這便是訓練數據上的偏差。
loss_ LossFunction
具體的LossFunction對象。
init_ estimator
提供最初預測的估計器。通過init參數或loss.init_estimator設置。
estimators_ ndarray of DecisionTreeRegressor of shape (n_estimators, loss_.K)
擬合的次估計值的集合。loss_.K對于二進制分類為1,否則為n_classes
classes_ ndarray of shape (n_classes,)
類標簽。
n_features_ int
數據的特征數。
n_classes_ int or list
類的數量。
max_features_ int
max_features的推斷值。

另見:

sklearn.ensemble.HistGradientBoostingClassifier

sklearn.tree.DecisionTreeClassifier, RandomForestClassifier

AdaBoostClassifier

注意:

在每次分割時,特性總是隨機地進行分配。因此,即使在相同的訓練數據和max_features=n_features的情況下,如果搜索最佳分割時列舉的幾個分割對準則的改進相同,那么找到的最佳分割也會有所不同。為了在擬合過程中獲得確定性行為,必須修復random_state

參考文獻

J. Friedman, Greedy Function Approximation: A Gradient Boosting Machine, The Annals of Statistics, Vol. 29, No. 5, 2001.

? 10. Friedman, Stochastic Gradient Boosting, 1999

T. Hastie, R. Tibshirani and J. Friedman. Elements of Statistical Learning Ed. 2, Springer, 2009.

實例

>>> from sklearn.datasets import make_classification
>>> from sklearn.ensemble import GradientBoostingClassifier
>>> from sklearn.model_selection import train_test_split
>>> X, y = make_classification(random_state=0)
>>> X_train, X_test, y_train, y_test = train_test_split(
...     X, y, random_state=0)
>>> clf = GradientBoostingClassifier(random_state=0)
>>> clf.fit(X_train, y_train)
GradientBoostingClassifier(random_state=0)
>>> clf.predict(X_test[:2])
array([10])
>>> clf.score(X_test, y_test)
0.88

方法

方法 說明
apply(X) 將森林中的樹應用于X,返回葉子索引。
decision_function(X) 計算X的決策函數。
fit(X, y[, sample_weight, monitor]) 擬合gradient boosting模型。
get_params([deep]) 獲取此估計器的參數。
predict(X) 預測X的類。
predict_log_proba(X) 預測X的類對數概率。
predict_proba(X) 預測X的類概率。
score(X, y[, sample_weight]) 返回給定測試數據和標簽上的平均準確度。
set_params(**params) 設置此估算器的參數。
staged_decision_function(X) 每次迭代計算X的決策函數。
staged_predict(X) 預測X在每個階段的分類。
staged_predict_proba(X) 預測X在每個階段的類概率。
__init__(*, loss='deviance', learning_rate=0.1, n_estimators=100, subsample=1.0, criterion='friedman_mse', min_samples_split=2, min_samples_leaf=1, min_weight_fraction_leaf=0.0, max_depth=3, min_impurity_decrease=0.0, min_impurity_split=None, init=None, random_state=None, max_features=None, verbose=0, max_leaf_nodes=None, warm_start=False, presort='deprecated', validation_fraction=0.1, n_iter_no_change=None, tol=0.0001, ccp_alpha=0.0)

[源碼]

apply(X)

[源碼]

將森林中的樹應用于X,返回葉子索引。

0.17版本的新功能

參數 說明
X {array-like, sparse matrix} of shape (n_samples, n_features)
輸入樣本。在內部,它的dtype將被轉換為dtype=np.float32。如果提供了一個稀疏矩陣,它將被轉換為一個csr_matrix
返回值 說明
X_leaves array-like of shape (n_samples, n_estimators, n_classes)
對于X中的每個數據點x和集合中的每棵樹,返回葉節點x的索引,最后返回到每個估計器中。對于二進制分類,n_classes是1。
decision_function(X)

[源碼]

計算X的決策函數。

計算決策函數X

參數 說明
X {array-like, sparse matrix} of shape (n_samples, n_features)
輸入樣本。在內部,它將被轉換為dtype=np.float32。以及是否將稀疏矩陣提供給稀疏csr_matrix
返回值 說明
score ndarray of shape (n_samples, n_classes) or (n_samples,)
輸入樣本的決策函數,它對應于從集合樹中預測的原始值。類的順序對應于屬性classes_中的順序。回歸和二分類產生一個數組shape[n_samples]。
property feature_importances_

基于雜質的功能的重要性。

越高,功能越重要。特征的重要性計算為該特征帶來的標準的(標準化)總縮減。這也被稱為基尼重要性。

警告:基于雜質的特征重要性可能會誤導高基數特征(許多唯一值)。另見 sklearn.inspection.permutation_importance

返回值 說明
feature_importances_ ndarray of shape (n_features,)
除非所有樹都是僅由根節點組成的單節點樹,否則此數組的值總計為1,在這種情況下,它將是零數組。
fit(X, y, sample_weight=None, monitor=None)

[源碼]

擬合gradient boosting模型。

參數 說明
X {array-like, sparse matrix} of shape (n_samples, n_features)
輸入樣本。在內部,它將被轉換為dtype=np.float32。以及是否將稀疏矩陣提供給稀疏csr_matrix
y array-like of shape (n_samples, )
目標值(分類中的字符串或整數,回歸中的實數)分類時,標簽必須與類對應。
sample_weight array-like of shape (n_samples,), default=None
樣本權重。如果沒有,那么樣本的權重相等。當在每個節點中搜索分割時,將忽略創建具有凈零權值或負權值的子節點的分割。在分類的情況下,如果分割會導致任何一個類在任一子節點中具有負權值,那么分割也將被忽略。
monitor callable, default=None
使用當前迭代器下的對估計器的引用和_fit_stages的局部變量作為可調用的關鍵字參數callable(i、self、locals()),在每次迭代之后調用監視器。如果callable返回True,擬合過程將停止。該監視器可用于各種各樣的情景,如計算拉出的估計、提前停止、模型內省和快照。
返回值 說明
self object
get_params(deep=True)

[源碼]

獲取此估計器的參數。

參數 說明
deep bool, default=True
如果為True,則將返回此估算器和作為估算器的所包含子對象的參數。
返回值 說明
params mapping of string to any
參數名稱與其值相對應。
predict(X)

[源碼]

預測X的分類。

對輸入樣本的預測類別進行計算,作為分類器在集成中的加權平均預測。

參數 說明
X {array-like, sparse matrix} of shape (n_samples, n_features)
輸入樣本。在內部,它將被轉換為dtype=np.float32。以及是否將稀疏矩陣提供給稀疏csr_matrix
返回值 說明
y ndarray of shape (n_sample, )
預測后的回歸值。
predict_log_proba(X)

[源碼]

預測X的類對數概率。

將輸入樣本的預測類對數概率計算為集合中基本估計器的平均預測類對數的對數。

參數 說明
X {array-like, sparse matrix} of shape (n_samples, n_features)
輸入樣本。在內部,它將被轉換為dtype=np.float32。以及是否將稀疏矩陣提供給稀疏csr_matrix
返回值 說明
p ndarray of shape (n_samples, n_classes)
輸入樣本的類對數概率。類的順序與屬性classes_中的順序相對應。
報錯信息 說明
Raises AttributeError
loss不支持概率。
predict_proba(X)

[源碼]

預測X的類概率。

參數 說明
X {array-like, sparse matrix} of shape (n_samples, n_features)
輸入樣本。在內部,它將被轉換為dtype=np.float32。以及是否將稀疏矩陣提供給稀疏csr_matrix
返回值 說明
p ndarray of shape (n_samples, n_classes)
輸入樣本的分類概率。類的順序與屬性classes_中的順序相對應。
報錯信息 說明
Raises AttributeError
loss不支持概率。
p ndarray of shape (n_samples, n_classes)
輸入樣本的分類概率。輸出的順序與其classes_屬性相一致。
score(self, X, y, sample_weight=None)

[源碼]

返回測試數據和標簽的平均準確率。

在多標簽分類中,這是子集精度,這是一個苛刻的指標,因為你需要對每個樣本正確預測每個標簽集。

參數 說明
X array-like of shape (n_sample, n_features)
測試樣本。
y array-like of shpe (n_sample, ) or (n_samples, n_outputs)
X的正確標簽。
sample_weight array-like of shape (n_samples, ), default = None
樣本權重。
返回值 說明
score float
self.predict(X) 關于y的平均準確率。
set_params(**params)

[源碼]

設置該估計器的參數。

該方法適用于簡單估計器和嵌套對象(如pipline)。后者具有形式為<component>_<parameter>的參數,這樣就可以更新嵌套對象的每個組件。

參數 說明
**params dict
估計器參數
返回值 說明
self object
估計實例。
staged_decision_function(X)

每次迭代計算X的決策函數。

該方法允許在每個階段后進行監控(即在測試集上確定錯誤)。

參數 說明
X {array-like, sparse matrix} of shape (n_samples, n_features)
輸入樣本。在內部,它將被轉換為dtype=np.float32。以及是否將稀疏矩陣提供給稀疏csr_matrix
返回值 說明
score generator of ndarray of shape (n_samples, k)
輸入樣本的決策函數,它對應于從集合樹中預測的原始值。這些類對應于屬性classes_中的類。回歸和二分類是k== 1的特例,否則k==n_classes
staged_predict(X)

[源碼]

返回X階段性的預測。

該方法允許在每個階段后進行監控(即在測試集上確定錯誤)。

參數 說明
X {array-like, sparse matrix} of shape (n_samples, n_features)
輸入樣本。在內部,它將被轉換為dtype=np.float32。以及是否將稀疏矩陣提供給稀疏csr_matrix
產出 說明
y generator of ndarray of shape (n_samples, )
預測后的回歸值。
staged_predict_proba(self, X)

[源碼]

預測X的類概率。

該方法允許在每個階段后進行監控(即在測試集上確定錯誤)。

參數 說明
X {array-like, sparse matrix} of shape (n_samples, n_features)
輸入樣本。在內部,它將被轉換為dtype=np.float32。以及是否將稀疏矩陣提供給稀疏csr_matrix
產出 說明
p generator of ndarray of shape (n_samples,)
輸入樣本的預測值。