sklearn.ensemble.BaggingClassifier?

class sklearn.ensemble.BaggingClassifier(base_estimator=None, n_estimators=10, *, max_samples=1.0, max_features=1.0, bootstrap=True, bootstrap_features=False, oob_score=False, warm_start=False, n_jobs=None, random_state=None, verbose=0)

[源碼]

Bagging分類器。

Bagging分類器是一個集合元估計器,它使每個基本分類器擬合原始數據集的隨機子集,然后將其單個預測(通過投票或平均)進行匯總以形成最終預測。這類元估計器通過將隨機化引入其構造過程中,并對其進行整體化,來減少黑盒估計器(例如決策樹)方差。

該算法涵蓋了文獻中的幾篇著作。當將數據集的隨機子集繪制為樣本的隨機子集時,該算法稱為Pasting[1]。如果抽取樣本進行替換,則該方法稱為Bagging [2]。當將數據集的隨機子集繪制為要素的隨機子集時,該方法稱為Random Subspaces[3]。最后,當基于樣本和特征的子集建立基本估計器時,該方法稱為Random Patches[4]。

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

0.15版的新功能。

參數 說明
base_estimator object, default=None
基本估計量適合數據集的隨機子集。如果為None,則基本估計量為決策樹。
n_estimators int,default = 10
集合中基本估計量的數量。
max_samples int or float, default=1.0
從X抽取以訓練每個基本估計量的樣本數量(默認情況下bootstrap為替換,請參見有關更多詳細信息)。
- 如果為int,則抽取max_samples樣本。
- 如果為float,則抽取樣品。max_samples * X.shape[0]
max_features int or float, default=1.0
從X繪制以訓練每個基本估計量的要素數量(默認情況下不進行替換,bootstrap_features有關更多詳細信息,請參見)。
- 如果為int,則繪制max_features特征。
- 如果為float,則繪制特征。max_features * X.shape[1]
bootstrap bool, default=True
是否抽取樣本進行替換。如果為False,則執行不替換的采樣。
bootstrap_features bool, default=False
是否用替換繪制特征。
oob_score bool,defalut = False
是否使用現成的樣本來估計泛化誤差。
warm_start bool,defalut = False
設置為True時,請重用上一個調用的解決方案以適合并在集合中添加更多估計量,否則,僅適合一個全新的集合。請參閱Glossary
0.17版中的新功能:warm_start構造函數參數。
n_jobs int, default=None
fit和 并行運行的作業數predict。除非用于joblib.parallel_backend上下文中,否則None表示1 。-1表示使用所有處理器。有關更多詳細信息,請參見Glossary
random_state int or RandomState, default=None
控制原始數據集的隨機重采樣(sample wise 和 feature wise)。如果基本估算器接受random_state屬性,則會為集合中的每個實例生成一個不同的種子。為多個函數調用傳遞可重復輸出的int值。請參閱Glossary
verbose int, default=0
在擬合和預測時控制冗余程度。
屬性 說明
base_estimator_ estimator
通過集成成長而來的基本估計器。
n_features_ int
fit執行時的功能數量。
estimators_ list of estimators
擬合基礎估計器的集合。
estimators_samples_ list of arrays
每個基本估計器的抽取樣本的子集。
estimators_features_ list of arrays
每個基本估計器的繪制要素子集。
classes_ ndarray of shape (n_classes,)
類標簽。
n_classes_ int or list
類數。
oob_score_ float
使用"袋外"估計獲得的訓練數據集的分數。該屬性僅在oob_score為True 時存在。
oob_decision_function_ ndarray of shape (n_samples, n_classes)
用訓練集上的實際估計值計算的決策函數。如果n_estimators較小,則有可能在bootstrap不會遺漏任何數據點。在這種情況下, oob_decision_function_可能包含NaN。該屬性僅在oob_score為True 時存在。

參考文獻

[1] L. Breiman, “Pasting small votes for classification in large databases and on-line”, Machine Learning, 36(1), 85-103, 1999.

[2] L. Breiman, “Bagging predictors”, Machine Learning, 24(2), 123-140, 1996.

[3] T. Ho, “The random subspace method for constructing decision forests”, Pattern Analysis and Machine Intelligence, 20(8), 832-844, 1998.

[4] G. Louppe and P. Geurts, “Ensembles on Random Patches”, Machine Learning and Knowledge Discovery in Databases, 346-361, 2012.

實例:

>>> from sklearn.svm import SVC
>>> from sklearn.ensemble import BaggingClassifier
>>> from sklearn.datasets import make_classification
>>> X, y = make_classification(n_samples=100, n_features=4,
...                            n_informative=2, n_redundant=0,
...                            random_state=0, shuffle=False)
>>> clf = BaggingClassifier(base_estimator=SVC(),
...                         n_estimators=10, random_state=0).fit(X, y)
>>> clf.predict([[0000]])
array([1])

方法

方法 說明
decision_function(X) 基本分類器決策函數的平均值。
fit(X, y[, sample_weight]) 從訓練集中構建一個bagging評估器的集合。
get_params([deep]) 獲取此估計器的參數。
predict(X) 預測X的類。
predict_log_proba(X) 預測X的類對數概率。
predict_proba(X) 預測X的類概率。
score(X, y[, sample_weight]) 返回給定測試數據和標簽上的平均準確度。
set_params(**params) 設置此估算器的參數。
__init__(base_estimator = None,n_estimators = 10,*,max_samples = 1.0,max_features = 1.0,bootstrap = True,bootstrap_features = False,oob_score = False,warm_start = False,n_jobs = None,random_state = None,verbose = 0 )

[源碼]

初始化self。有關準確的簽名,請參見help(type(self))

decision_function(X)

[源碼]

基本分類器決策函數的平均值。

參數 說明
X {array-like, sparse matrix} of shape (n_samples, n_features)
訓練輸入樣本。僅當基本估計器支持稀疏矩陣時才會被接受。
返回值 說明
score ndarray of shape (n_samples, k)
輸入樣本的決策函數。列按屬性順序顯示在類別中,與它們對應 classes_。回歸和二進制分類是的特例,即k == 1,其他情況下,k==n_classes
property estimators_samples_

每個基本估計器的抽取樣本的子集。

返回一個動態生成的索引列表,這些索引標識用于擬合集合的每個成員的樣本,即袋裝樣本。

注意:在每次調用屬性時都會重新創建列表,以通過不存儲采樣數據來減少對象內存占用。因此,獲取屬性可能比預期要慢。

fit(X, y, sample_weight=None)

[源碼]

從訓練集中構建一個bagging評估器的集合。

set(X, y)

參數 說明
X {array-like, sparse matrix} of shape (n_samples, n_features)
訓練輸入樣本。僅當基本估計器支持稀疏矩陣時,才接受。
y array-like of shape (n_samples,)
目標值(分類中的類標簽,回歸中的實數)。
sample_weight array-like of shape (n_samples,), default=None
樣品重量。如果為None,則對樣本進行平均加權。請注意,僅當基本估算器支持樣本加權時才支持此功能。
返回值 說明
self object
get_params(deep=True)

[源碼]

獲取此估計器的參數。

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

[源碼]

預測X的類。

輸入樣本的預測類別被計算為具有最高平均預測概率的類別。如果基本估計器未實現predict_proba方法,則它會采取投票的手段。

參數 說明
X {array-like, sparse matrix} of shape (n_samples, n_features)
訓練輸入樣本。僅當基本估計器支持稀疏矩陣時,才接受。
返回值 說明
y ndarray of shape (n_samples,)
被預測的類。
predict_log_proba(X)

[源碼]

預測X的類對數概率。

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

參數 說明
X {array-like, sparse matrix} of shape (n_samples, n_features)
訓練輸入樣本。僅當基本估計量支持稀疏矩陣時,才接受。
返回值 說明
p ndarray of shape (n_samples, n_classes)
輸入樣本的類對數概率。類的順序與屬性classes_中的順序相對應。
predict_proba(X)

[源碼]

預測X的類概率。

將輸入樣本的預測類別概率計算為集合中基本估計量的平均預測類別概率。如果基本估計量未實現predict_proba 方法,則采用投票的方式,并且輸入樣本的預測類概率表示預測每個類估計器的比例。

參數 說明
X {array-like, sparse matrix} of shape (n_samples, n_features)
訓練輸入樣本。僅當基本估計量支持稀疏矩陣時才會被接受。
返回值 說明
p ndarray of shape (n_samples, n_classes)
輸入樣本的分類概率。類的順序與屬性classes_中的順序相對應。
score(X, y, sample_weight=None)

[源碼]

返回給定測試數據和標簽上的平均準確度。

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

參數 說明
X array-like of shape (n_samples, n_features)
測試樣本。
y array-like of shape (n_samples,) or (n_samples, n_outputs)
樣本集的標簽。
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
估計實例。