gators.pipeline.Pipeline¶
-
class
gators.pipeline.
Pipeline
(steps: List[Tuple[gators.transformers.transformer.Transformer]], memory=None, verbose=False)[source]¶ SciKitPileline Chain the gators transformers together.
- Parameters
- stepsList[Tuple[Transformer]]
List of transformers ending, or not, by an estimator.
- memorystr, default None.
Scilit-learn pipeline memory parameter.
- verbosebool, default False.
Verbosity.
Examples
Imports and initialization:
>>> from gators.imputers import NumericsImputer >>> from gators.imputers import ObjectImputer >>> from gators.pipeline import Pipeline >>> steps = [ ... ('ObjectImputer', ObjectImputer(strategy='constant', value='MISSING')), ... ('NumericsImputerMedian', NumericsImputer(strategy='median', columns=['A'])), ... ('NumericsImputerFrequent', NumericsImputer(strategy='most_frequent', columns=['B']))] >>> obj = Pipeline(steps=steps)
The fit, transform, and fit_transform methods accept:
dask dataframes:
>>> import dask.dataframe as dd >>> import pandas as pd >>> X = dd.from_pandas(pd.DataFrame({ ... 'A': [0.1, 0.2, 0.3, np.nan], ... 'B': [1, 2, 2, np.nan], ... 'C': ['a', 'b', 'c', None]}), npartitions=1)
koalas dataframes:
>>> import databricks.koalas as ks >>> X = ks.DataFrame({ ... 'A': [0.1, 0.2, 0.3, np.nan], ... 'B': [1, 2, 2, np.nan], ... 'C': ['a', 'b', 'c', None]})
and pandas dataframes:
>>> import pandas as pd >>> X = pd.DataFrame({ ... 'A': [0.1, 0.2, 0.3, np.nan], ... 'B': [1, 2, 2, np.nan], ... 'C': ['a', 'b', 'c', None]})
The result is a transformed dataframe belonging to the same dataframe library.
>>> obj.fit_transform(X) A B C 0 0.1 1.0 a 1 0.2 2.0 b 2 0.3 2.0 c 3 0.2 2.0 MISSING
>>> X = pd.DataFrame({ ... 'A': [0.1, 0.2, 0.3, np.nan], ... 'B': [1, 2, 2, np.nan], ... 'C': ['a', 'b', 'c', None]}) >>> _ = obj.fit(X) >>> obj.transform_numpy(X.to_numpy()) array([[0.1, 1.0, 'a'], [0.2, 2.0, 'b'], [0.3, 2.0, 'c'], [0.2, 2.0, 'MISSING']], dtype=object)
-
transform_numpy
(X: numpy.ndarray) → numpy.ndarray[source]¶ Transform the array X.
- Parameters
- Xnp.ndarray
Input array.
- Returns
- Xnp.ndarray
Transformed array.
-
static
check_array
(X: numpy.ndarray)¶ Validate array.
- Parameters
- Xnp.ndarray
Array.
-
check_array_is_numerics
(X: numpy.ndarray)¶ Check if array is only numerics.
- Parameters
- Xnp.ndarray
Array.
-
static
check_binary_target
(X: Union[pd.DataFrame, ks.DataFrame, dd.DataFrame], y: Union[pd.Series, ks.Series, dd.Series])¶ Raise an error if the target is not binary.
- Parameters
- ySeries
Target values.
-
static
check_dataframe
(X: Union[pd.DataFrame, ks.DataFrame, dd.DataFrame])¶ Validate dataframe.
- Parameters
- XDataFrame
Dataframe.
-
static
check_dataframe_contains_numerics
(X: Union[pd.DataFrame, ks.DataFrame, dd.DataFrame])¶ Check if dataframe is only numerics.
- Parameters
- XDataFrame
Dataframe.
-
static
check_dataframe_is_numerics
(X: Union[pd.DataFrame, ks.DataFrame, dd.DataFrame])¶ Check if dataframe is only numerics.
- Parameters
- XDataFrame
Dataframe.
-
check_dataframe_with_objects
(X: Union[pd.DataFrame, ks.DataFrame, dd.DataFrame])¶ Check if dataframe contains object columns.
- Parameters
- XDataFrame
Dataframe.
-
check_datatype
(dtype, accepted_dtypes)¶ Check if dataframe is only numerics.
- Parameters
- XDataFrame
Dataframe.
-
static
check_multiclass_target
(y: Union[pd.Series, ks.Series, dd.Series])¶ Raise an error if the target is not discrete.
- Parameters
- ySeries
Target values.
-
check_nans
(X: Union[pd.DataFrame, ks.DataFrame, dd.DataFrame], columns: List[str])¶ Raise an error if X contains NaN values.
- Parameters
- XDataFrame
Dataframe.
- theta_vecList[float]
List of columns.
-
static
check_regression_target
(y: Union[pd.Series, ks.Series, dd.Series])¶ Raise an error if the target is not discrete.
- Parameters
- ySeries
Target values.
-
static
check_target
(X: Union[pd.DataFrame, ks.DataFrame, dd.DataFrame], y: Union[pd.Series, ks.Series, dd.Series])¶ Validate target.
- Parameters
- XDataFrame
Dataframe.
- ySeries
Target values.
-
property
classes_
¶ The classes labels. Only exist if the last step is a classifier.
-
decision_function
(X)[source]¶ Transform the data, and apply decision_function with the final estimator.
Call transform of each transformer in the pipeline. The transformed data are finally passed to the final estimator that calls decision_function method. Only valid if the final estimator implements decision_function.
- Parameters
- Xiterable
Data to predict on. Must fulfill input requirements of first step of the pipeline.
- Returns
- y_scorendarray of shape (n_samples, n_classes)
Result of calling decision_function on the final estimator.
-
property
feature_names_in_
¶ Names of features seen during first step fit method.
-
fit
(X, y=None, **fit_params)[source]¶ Fit the model.
Fit all the transformers one after the other and transform the data. Finally, fit the transformed data using the final estimator.
- Parameters
- Xiterable
Training data. Must fulfill input requirements of first step of the pipeline.
- yiterable, default=None
Training targets. Must fulfill label requirements for all steps of the pipeline.
- **fit_paramsdict of string -> object
Parameters passed to the
fit
method of each step, where each parameter name is prefixed such that parameterp
for steps
has keys__p
.
- Returns
- selfobject
Pipeline with fitted steps.
-
fit_predict
(X, y=None, **fit_params)[source]¶ Transform the data, and apply fit_predict with the final estimator.
Call fit_transform of each transformer in the pipeline. The transformed data are finally passed to the final estimator that calls fit_predict method. Only valid if the final estimator implements fit_predict.
- Parameters
- Xiterable
Training data. Must fulfill input requirements of first step of the pipeline.
- yiterable, default=None
Training targets. Must fulfill label requirements for all steps of the pipeline.
- **fit_paramsdict of string -> object
Parameters passed to the
fit
method of each step, where each parameter name is prefixed such that parameterp
for steps
has keys__p
.
- Returns
- y_predndarray
Result of calling fit_predict on the final estimator.
-
fit_transform
(X, y=None, **fit_params)[source]¶ Fit the model and transform with the final estimator.
Fits all the transformers one after the other and transform the data. Then uses fit_transform on transformed data with the final estimator.
- Parameters
- Xiterable
Training data. Must fulfill input requirements of first step of the pipeline.
- yiterable, default=None
Training targets. Must fulfill label requirements for all steps of the pipeline.
- **fit_paramsdict of string -> object
Parameters passed to the
fit
method of each step, where each parameter name is prefixed such that parameterp
for steps
has keys__p
.
- Returns
- Xtndarray of shape (n_samples, n_transformed_features)
Transformed samples.
-
static
get_column_names
(inplace: bool, columns: List[str], suffix: str)¶ Return the names of the modified columns.
- Parameters
- inplacebool
If True return columns. If False return columns__suffix.
- columnsList[str]
List of columns.
- suffixstr
Suffix used if inplace is False.
- Returns
- List[str]
List of column names.
-
get_feature_names_out
(input_features=None)[source]¶ Get output feature names for transformation.
Transform input features using the pipeline.
- Parameters
- input_featuresarray-like of str or None, default=None
Input features.
- Returns
- feature_names_outndarray of str objects
Transformed feature names.
-
get_params
(deep=True)[source]¶ Get parameters for this estimator.
Returns the parameters given in the constructor as well as the estimators contained within the steps of the Pipeline.
- Parameters
- deepbool, default=True
If True, will return the parameters for this estimator and contained subobjects that are estimators.
- Returns
- paramsmapping of string to any
Parameter names mapped to their values.
-
inverse_transform
(Xt)[source]¶ Apply inverse_transform for each step in a reverse order.
All estimators in the pipeline must support inverse_transform.
- Parameters
- Xtarray-like of shape (n_samples, n_transformed_features)
Data samples, where
n_samples
is the number of samples andn_features
is the number of features. Must fulfill input requirements of last step of pipeline’sinverse_transform
method.
- Returns
- Xtndarray of shape (n_samples, n_features)
Inverse transformed data, that is, data in the original feature space.
-
property
n_features_in_
¶ Number of features seen during first step fit method.
-
property
named_steps
¶ Access the steps by name.
Read-only attribute to access any step by given name. Keys are steps names and values are the steps objects.
-
predict
(X, **predict_params)[source]¶ Transform the data, and apply predict with the final estimator.
Call transform of each transformer in the pipeline. The transformed data are finally passed to the final estimator that calls predict method. Only valid if the final estimator implements predict.
- Parameters
- Xiterable
Data to predict on. Must fulfill input requirements of first step of the pipeline.
- **predict_paramsdict of string -> object
Parameters to the
predict
called at the end of all transformations in the pipeline. Note that while this may be used to return uncertainties from some models with return_std or return_cov, uncertainties that are generated by the transformations in the pipeline are not propagated to the final estimator.New in version 0.20.
- Returns
- y_predndarray
Result of calling predict on the final estimator.
-
predict_log_proba
(X, **predict_log_proba_params)[source]¶ Transform the data, and apply predict_log_proba with the final estimator.
Call transform of each transformer in the pipeline. The transformed data are finally passed to the final estimator that calls predict_log_proba method. Only valid if the final estimator implements predict_log_proba.
- Parameters
- Xiterable
Data to predict on. Must fulfill input requirements of first step of the pipeline.
- **predict_log_proba_paramsdict of string -> object
Parameters to the
predict_log_proba
called at the end of all transformations in the pipeline.
- Returns
- y_log_probandarray of shape (n_samples, n_classes)
Result of calling predict_log_proba on the final estimator.
-
predict_proba
(X, **predict_proba_params)[source]¶ Transform the data, and apply predict_proba with the final estimator.
Call transform of each transformer in the pipeline. The transformed data are finally passed to the final estimator that calls predict_proba method. Only valid if the final estimator implements predict_proba.
- Parameters
- Xiterable
Data to predict on. Must fulfill input requirements of first step of the pipeline.
- **predict_proba_paramsdict of string -> object
Parameters to the predict_proba called at the end of all transformations in the pipeline.
- Returns
- y_probandarray of shape (n_samples, n_classes)
Result of calling predict_proba on the final estimator.
-
score
(X, y=None, sample_weight=None)[source]¶ Transform the data, and apply score with the final estimator.
Call transform of each transformer in the pipeline. The transformed data are finally passed to the final estimator that calls score method. Only valid if the final estimator implements score.
- Parameters
- Xiterable
Data to predict on. Must fulfill input requirements of first step of the pipeline.
- yiterable, default=None
Targets used for scoring. Must fulfill label requirements for all steps of the pipeline.
- sample_weightarray-like, default=None
If not None, this argument is passed as
sample_weight
keyword argument to thescore
method of the final estimator.
- Returns
- scorefloat
Result of calling score on the final estimator.
-
score_samples
(X)[source]¶ Transform the data, and apply score_samples with the final estimator.
Call transform of each transformer in the pipeline. The transformed data are finally passed to the final estimator that calls score_samples method. Only valid if the final estimator implements score_samples.
- Parameters
- Xiterable
Data to predict on. Must fulfill input requirements of first step of the pipeline.
- Returns
- y_scorendarray of shape (n_samples,)
Result of calling score_samples on the final estimator.
-
set_params
(**kwargs)[source]¶ Set the parameters of this estimator.
Valid parameter keys can be listed with
get_params()
. Note that you can directly set the parameters of the estimators contained in steps.- Parameters
- **kwargsdict
Parameters of this estimator or parameters of estimators contained in steps. Parameters of the steps may be set using its name and the parameter name separated by a ‘__’.
- Returns
- selfobject
Pipeline class instance.
-
transform
(X)[source]¶ Transform the data, and apply transform with the final estimator.
Call transform of each transformer in the pipeline. The transformed data are finally passed to the final estimator that calls transform method. Only valid if the final estimator implements transform.
This also works where final estimator is None in which case all prior transformations are applied.
- Parameters
- Xiterable
Data to transform. Must fulfill input requirements of first step of the pipeline.
- Returns
- Xtndarray of shape (n_samples, n_transformed_features)
Transformed data.