sklearn.pipeline.Pipeline?

class sklearn.pipeline.Pipeline(steps, *, memory=None, verbose=False)

[源碼]

帶有最終估計器的轉換管道。

依次應用轉換列表和最終估計器。管道中間的steps必須是“transforms”(轉換器),也就是說,它們必須實現擬合和轉換方法。最終估計器只需實現擬合。使用memory參數可以緩存管道中的轉換器。

管道的目的是組裝幾個可以一起交叉驗證的steps,同時設置不同的參數。為此,它可以使用他們的名稱和以“__”分隔的參數名稱來設置各個steps的參數,如下所示。可以通過將參數的名稱設置為另一個估計器來完全替換一個step的估計器,也可以通過將其設置為“ passthrough”或None刪除轉換器。

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

0.5版的新功能。

參數 說明
steps list
(命名,轉換)元組(實現擬合/轉換)的列表,按照它們被鏈接的順序,最后一個對象是估計器。
memory str or object with the joblib.Memory interface, default=None
用于緩存擬合轉換的管道。默認情況下,不執行緩存。如果給定一個字符串,則它是緩存目錄的路徑。啟用緩存會觸發轉換器在擬合之前的克隆。因此,不能直接檢查提供給管道的轉換器實例。可以使用屬性named_stepssteps檢查管道中的估計器。當擬合耗時時,緩存轉換器是有利的。
verbose bool, default=False
如果為True,則在擬合每個step時將打印經過的時間。
屬性 說明
named_steps Bunch
類字典對象,具有以下屬性。只讀屬性,用于通過用戶給定名稱訪問任意step的參數。鍵是step的名稱,值是step的參數。

另見:

sklearn.pipeline.make_pipeline 便利函數,簡化了管道建設。

示例

>>> from sklearn.svm import SVC
>>> from sklearn.preprocessing import StandardScaler
>>> from sklearn.datasets import make_classification
>>> from sklearn.model_selection import train_test_split
>>> from sklearn.pipeline import Pipeline
>>> X, y = make_classification(random_state=0)
>>> X_train, X_test, y_train, y_test = train_test_split(X, y,
...                                                     random_state=0)
>>> pipe = Pipeline([('scaler', StandardScaler()), ('svc', SVC())])
>>> # The pipeline can be used as any other estimator
>>> # and avoids leaking the test set into the train set
>>> pipe.fit(X_train, y_train)
Pipeline(steps=[('scaler', StandardScaler()), ('svc', SVC())])
>>> pipe.score(X_test, y_test)
0.88

方法

方法 說明
decision_function(X) 應用transforms,和最后一個估計器的decision_function
fit(X[, y]) 擬合模型
fit_predict(X[, y]) 在transforms后應用管道中最后一個step的fit_predict
fit_transform(X[, y]) 擬合模型并用最后一個估計器進行轉換
get_params([deep]) 獲取這個估計器的參數
predict(X, **predict_params) 將transforms應用于數據,并使用最后一個估計器進行預測
predict_log_proba(X) 應用transforms,和最后一個估計器的predict_log_proba
predict_proba(X) 應用transforms,和最后一個估計器的predict_proba
score(X[, y, sample_weight]) 應用transforms,和最后一個估計器的score
score_samples(X) 應用transforms,和最后一個估計器的score_samples
set_params(**kwargs) 為這個估計器設置參數
__init__(steps, *, memory=None, verbose=False)

[源碼]

初始化self。詳情可參閱 type(self)的幫助。

decision_function(X)

[源碼]

應用transforms,和最后一個估計器的decision_function

參數 說明
X iterable
用于預測的數據。必須滿足管道第一個step的輸入要求。
返回值 說明
y_score array-like of shape (n_samples, n_classes)
fit(X, y=None, **fit_params)

[源碼]

擬合模型

依次擬合所有轉換器變換并轉換數據,然后使用最后一個估計器擬合轉換后的數據。

參數 說明
X iterable
用于訓練的數據。必須滿足管道第一個step的輸入要求。
y iterable, default=None
訓練集標簽。必須滿足管道所有steps的標簽要求。
**fit_params dict of string -> object
每個step傳遞給fit方法的參數,其中每個參數名稱都帶有前綴,比如用于步驟s的參數p有鍵s__p
返回值 說明
self Pipeline
這個估計器
fit_predict(X, y=None, **fit_params)

[源碼]

在transforms后應用管道中最后一個step的fit_predict。

將管道的fit_transforms應用于數據,然后將管道中的最后一個估計器的fit_predict方法應用于數據。僅在最后一個估計器實現fit_predict時有效。

參數 說明
X iterable
用于訓練的數據。必須滿足管道第一個step的輸入要求。
y iterable, default=None
訓練集標簽。必須滿足管道所有steps的標簽要求。
**fit_params dict of string -> object
每個step傳遞給fit方法的參數,其中每個參數名稱都帶有前綴,比如用于步驟s的參數p有鍵s__p
返回值 說明
y_pred array-like
fit_transform(X, y=None, **fit_params)

[源碼]

擬合模型并用最后一個估計器進行轉換

依次擬合所有轉換器并轉換數據,然后對帶有最后一個估計器的轉換后數據使用fit_transform。

參數 說明
X iterable
用于訓練的數據。必須滿足管道第一個step的輸入要求。
y iterable, default=None
訓練集標簽。必須滿足管道所有steps的標簽要求。
**fit_params dict of string -> object
每個step傳遞給fit方法的參數,其中每個參數名稱都帶有前綴,比如用于步驟s的參數p有鍵s__p
返回值 說明
Xt array-like of shape (n_samples, n_transformed_features)
轉換后的樣本
get_params(deep=True)

[源碼]

獲取這個估計器的參數

參數 說明
deep bool, default=True
如果為True,則將返回這個估計器的參數和所包含的估計器子對象。
返回值 說明
params mapping of string to any
參數名映射到其值
property inverse_transform

以相反順序應用逆變換。

管道中的所有估計器都必須支持inverse_transform

參數 說明
Xt array-like of shape (n_samples, n_transformed_features)
數據樣本,其中n_samples是樣本數量, n_features是特征數量。必須滿足管道最后一個step的inverse_transform方法的輸入要求 。
返回值 說明
Xt array-like of shape (n_samples, n_features)
predict(X, **predict_params)

[源碼]

將transforms應用于數據,并使用最后一個估計器進行預測

參數 說明
X iterable
用于預測的數據。必須滿足管道第一個step的輸入要求。
**predict_params dict of string -> object
在管道中的所有轉換結束時調用predict參數。請注意,雖然這可以用于從帶有return_std或return_cov的某些模型返回不確定性,但是由管道中的轉換生成的不確定性不會傳播到最后一個估計器。

0.20版中的新功能。
返回值 說明
y_pred array-like
predict_log_proba(X)

[源碼]

應用transforms,和最后一個估計器的predict_log_proba

參數 說明
X iterable
用于預測的數據。必須滿足管道第一個step的輸入要求。
返回值 說明
y_score array-like of shape (n_samples, n_classes)
predict_proba(X)

[源碼]

應用transforms,和最后一個估計器的predict_proba

參數 說明
X iterable
用于預測的數據。必須滿足管道第一個step的輸入要求。
返回值 說明
y_proba array-like of shape (n_samples, n_classes)
score(X, y=None, sample_weight=None)

[源碼]

應用transforms,和最后一個估計器的score

參數 說明
X iterable
用于預測的數據。必須滿足管道第一個step的輸入要求。
y iterable, default=None
用于計算準確率的標簽值。必須滿足管道所有steps的標簽要求。
sample_weight array-like, default=None
如果不是None,則將此參數作為sample_weight關鍵字參數傳遞給最后一個估計器的score方法。
返回值 說明
score float
score_samples(X)

[源碼]

應用transforms,和最后一個估計器的score_samples

參數 說明
X iterable
用于預測的數據。必須滿足管道第一個step的輸入要求。
返回值 說明
y_score ndarray of shape (n_samples,)
set_params(**kwargs)

[源碼]

為這個估計器設置參數

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

返回值
self
property transform

應用transforms,并使用最后一個估計器的transform

這在最后一個估計器為None的情況下也適用:應用之前所有的轉換器。

參數 說明
X iterable
用于轉換的數據。必須滿足管道第一個step的輸入要求。
返回值 說明
Xt array-like of shape (n_samples, n_transformed_features)