sklearn.ensemble.StackingClassifier?

class sklearn.ensemble.StackingClassifier(estimators, final_estimator=None, *, cv=None, stack_method='auto', n_jobs=None, passthrough=False, verbose=0)

[源碼]

帶有最終分類器的估計器堆棧。

堆疊泛化包括堆疊個別估計器的輸出,并使用分類器來計算最終的預測。疊加允許使用每個估計器的強度,使用它們的輸出作為最終估計器的輸入。

注意,estimators_是在完整的X上擬合而來的,而final_estimator_是通過使用cross_val_predict對基礎估計器進行交叉驗證的預測來進行訓練的。

0.22版本新功能。

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

參數 說明
estimators list of (str, estimator)
將被堆疊在一起的基礎估計器。列表中的每個元素都被定義為一個字符串元組(即名稱)和一個estimator實例。可以使用set_params將評估器設置為“drop”。
final_estimator estimator, default=None
一個分類器,它將被用來組合基礎估計器。默認的分類器是LogisticRegression
cv int, cross-validation generator or an iterable, default=None
確定cross_val_predict中用于訓練final_estimator的交叉驗證拆分策略。cv可能的輸入有:
- None,使用默認的5折交叉驗證。
- Integer, 用于指定(分層的)K-Fold中的折疊數。
- 用作交叉驗證生成器的對象。
- 一個可迭代的產生的訓練、測試分割。
對于integer/None,如果估計器是一個分類器,并且y是二進制的或多類的,則使用StratifiedKFold。所有其他情況下,使用K-Fold
參考用戶指南,了解這里可以使用的各種交叉驗證策略。
注意:如果訓練樣本的數量足夠大,那么分割的數量再大也沒有什么好處。事實上,訓練時間會增加。cv不是用于模型評估,而是用于預測。
stack_method {‘auto’, ‘predict_proba’, ‘decision_function’, ‘predict’}, default=’auto’
為每個基本估計器調用的方法。它可以是:
- 如果是“auto”,它將對每個估計器按相應順序調用“predict_proba”、“decision_function”或“predict”。
- 否則,'predict_proba'、'decision_function'或'predict'中的一個。如果估計器沒有實現該方法,它將產生一個錯誤。
n_jobs int, default=None
所有并行estimators fit作業數量。除非在joblib.parallel_backend中,否則None表示是1。-1表示使用所有處理器。參見Glossary了解更多細節。
passthrough bool, default=False
當為False時,只使用估計器的預測作為final_estimator的訓練數據。當為真時,final_estimator將在預測和原始訓練數據上進行訓練。
verbose int, default=0
冗余水平。
屬性 說明
classes_ ndarray of shape (n_classes,)
類標簽。
estimators_ list of estimators
估計器參數的元素,已在訓練數據上擬合。如果一個估計器被設置為“drop”,那么它將不會出現在estimators_中。
named_estimators_ Bunch
屬性來按名稱訪問任何適合的子估計器。
final_estimator_ estimator
給出估計器的輸出來預測的estimators_
stack_method_ list of str
每個基估計器使用的方法。

注意

當每個估計器使用predict_proba時(即大多數時候stack_method='auto',或者stack_method='predict_proba'),在出現二進制分類問題時,每個估計器預測的第一列將被刪除。事實上,這兩個特征將是完全共線的。

參考文獻

Wolpert, David H. “Stacked generalization.” Neural networks 5.2 (1992): 241-259.

實例

>>> from sklearn.datasets import load_iris
>>> from sklearn.ensemble import RandomForestClassifier
>>> from sklearn.svm import LinearSVC
>>> from sklearn.linear_model import LogisticRegression
>>> from sklearn.preprocessing import StandardScaler
>>> from sklearn.pipeline import make_pipeline
>>> from sklearn.ensemble import StackingClassifier
>>> X, y = load_iris(return_X_y=True)
>>> estimators = [
...     ('rf', RandomForestClassifier(n_estimators=10, random_state=42)),
...     ('svr', make_pipeline(StandardScaler(),
...                           LinearSVC(random_state=42)))
... ]
>>> clf = StackingClassifier(
...     estimators=estimators, final_estimator=LogisticRegression()
... )
>>> from sklearn.model_selection import train_test_split
>>> X_train, X_test, y_train, y_test = train_test_split(
...     X, y, stratify=y, random_state=42
... )
>>> clf.fit(X_train, y_train).score(X_test, y_test)
0.9...

方法

方法 說明
decision_function(X) 使用final_estimator_.decision_functionX中的樣本預測決策函數
fit(X[, y, sample_weight]) 擬合估計器。
fit_transform(X[, y, sample_weight]) 擬合估計器和變換數據集。
get_params([deep]) 從集成中得到估計器的參數。
predict(X, **predict_params) 預測X的目標值。
predict_proba(X) 使用final_estimator_.predict_proba預測X的類概率。
score(X, y[, sample_weight]) 返回給定測試數據和標簽的平均精度。
set_params(**params) 從集成中設置估計器的參數。
transform(X) 返回每個估計器X的類標簽或概率。
__init__(estimators, final_estimator=None, *, cv=None, stack_method='auto', n_jobs=None, passthrough=False, verbose=0)

[源碼]

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

decision_function(X)

[源碼]

使用final_estimator_.decision_functionX中的樣本預測決策函數。

參數 說明
X {array-like, sparse matrix} of shape (n_samples, n_features)
訓練向量,其中n_samples為樣本數量,n_features為特征數量。
返回值 說明
decisions ndarray of shape (n_samples,), (n_samples, n_classes), or (n_samples, n_classes * (n_classes-1) / 2)
決策函數計算最終的估計器。
fit(X, y, sample_weight = None)

[源碼]

擬合估計器。

參數 說明
X {array-like, sparse matrix} of shape (n_samples, n_features)
訓練向量,其中n_samples為樣本數量,n_features為特征數量。
y array-like of shape (n_samples,)
目標值。
sample_weight array-like of shape (n_samples,), default=None
樣本權重。如果沒有,那么樣本的權重相等。當在每個節點中搜索分割時,將忽略創建具有凈零權值或負權值的子節點的分割。在分類的情況下,如果分割會導致任何一個類在任一子節點中具有負權值,那么分割也將被忽略。
返回值 說明
self object
fit_transform(X, y=None, sample_weight=None)

[源碼]

擬合數據,然后轉換它。

使用可選參數fit_params將transformer與X和y匹配,并返回X的轉換版本。

參數 說明
X {array-like, sparse matrix} of shape (n_samples, n_features)
訓練向量,其中n_samples為樣本數量,n_features為特征數量。
y ndarray of shape (n_samples,), default=None
目標值。
**fit_params dict
其他擬合參數。
返回值 說明
X_new ndarray array of shape (n_samples, n_features_new)
轉化后的數組。
get_params(deep=True)

[源碼]

從集成中得到估計器的參數。

參數 說明
deep deep : bool, default = True
將其設置為True將獲得各種分類器以及分類器的參數。
property n_features_in_

fit 過程中可見的特征數量。

predict(X, **predict_params)

[源碼]

預測X的目標值。

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

參數 說明
X {array-like, sparse matrix} of shape (n_samples, n_features)
估計器參數訓練向量,其中n_samples為樣本數量,n_features為特征數量。
**predict_params dict of str -> obj
final_estimator調用的predict的參數。請注意,這可能用于從一些使用return_stdreturn_cov的估計器返回不確定性。請注意,它只會在最終的估計器中考慮不確定性。
返回值 說明
y_pred ndarray of shape (n_samples,) or (n_samples, n_output)
預測后的目標值。
predict_proba(X)

[源碼]

使用final_estimator_.predict_proba預測X的類概率

參數 說明
X {array-like, sparse matrix} of shape (n_samples, n_features)
訓練向量,其中n_samples為樣本數量,n_features為特征數量。
返回值 說明
probabilities ndarray of shape (n_samples, n_classes) or list of ndarray of shape (n_output,)
輸入樣本的類概率。
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)
X的正確標簽
sample_weight array-like of shape (n_samples,), default=None
樣本權重。
返回值 說明
score float
self.predict(X) 關于y的平均準確率。
set_params(**params)

[源碼]

從集成中設置估計器的參數。

有效的參數鍵可以用get_params()列出。

參數 說明
**params keyword arguments
使用例如set_params(parameter_name=new_value)的特定參數。此外,為了設置堆料估算器的參數,還可以設置疊加估算器的單個估算器,或者通過將它們設置為“drop”來刪除它們。
transform(X)

[源碼]

返回每個估計量X的類標簽或概率。

參數 說明
X {array-like, sparse matrix} of shape (n_samples, n_features)
訓練向量,其中n_samples為樣本數量,n_features為特征數量。
返回值 說明
y_preds ndarray of shape (n_samples, n_estimators) or (n_samples, n_classes * n_estimators)
每個估計器的預測輸出。

sklearn.ensemble.StackingClassifier使用示例?