gators.transformers.Transformer¶
-
class
gators.transformers.
Transformer
[source]¶ Abstract gators transformer class.
Examples
A Gators transformer.
>>> import pandas as pd >>> import databricks.koalas as ks >>> import numpy as np >>> from gators.transformers import Transformer >>> class GetFirstColumn(Transformer): ... def fit(self, X, y: Series = None): ... return self ... def transform(self, X: pd.DataFrame): ... return X[[X.columns[0]]] ... def transform_numpy(self, X: np.ndarray): ... return X[:, 0].reshape(-1, 1) >>> obj = GetFirstColumn()
The fit, transform, and fit_transform methods accept:
dask dataframes:
>>> import dask.dataframe as dd >>> import pandas as pd >>> import numpy as np >>> X = dd.from_pandas(pd.DataFrame({'A':[1, 2], 'B':[3, 4]}), npartitions=1)
koalas dataframes:
>>> import databricks.koalas as ks >>> import numpy as np >>> X = ks.DataFrame({'A':[1, 2], 'B':[3, 4]})
and pandas dataframes:
>>> import pandas as pd >>> import numpy as np >>> X = pd.DataFrame({'A':[1, 2], 'B':[3, 4]})
The result is a transformed dataframe belonging to the same dataframe library.
>>> obj.fit_transform(X) A 0 1 1 2
Independly of the dataframe library used to fit the transformer, the tranform_numpy method only accepts NumPy arrays and returns a transformed NumPy array. Note that this transformer should only be used when the number of rows is small e.g. in real-time environment.
>>> X = pd.DataFrame({'A':[1, 2], 'B':[3, 4]}) >>> obj.transform_numpy(X.to_numpy()) array([[1], [2]])
-
abstract
fit
(X: Union[pd.DataFrame, ks.DataFrame, dd.DataFrame], y: Union[pd.Series, ks.Series, dd.Series] = None) → gators.transformers.transformer.Transformer[source]¶ Fit the transformer on the dataframe X.
- Parameters
- XDataFrame.
Input dataframe.
- ySeries, default None.
Target values.
- Returns
- selfTransformer
Instance of itself.
-
abstract
transform
(X: Union[pd.DataFrame, ks.DataFrame, dd.DataFrame]) → Union[pd.DataFrame, ks.DataFrame, dd.DataFrame][source]¶ Transform the dataframe X.
- Parameters
- XDataFrame
Input dataframe.
- Returns
- XDataFrame
Transformed dataframe.
-
abstract
transform_numpy
(X: Union[pd.Series, ks.Series, dd.Series], y: Union[pd.Series, ks.Series, dd.Series] = None)[source]¶ Transform the array X.
- Parameters
- Xnp.ndarray
Array.
- Returns
- Xnp.ndarray
Transformed array.
-
fit_transform
(X: Union[pd.DataFrame, ks.DataFrame, dd.DataFrame], y: Union[pd.Series, ks.Series, dd.Series] = None) → Union[pd.DataFrame, ks.DataFrame, dd.DataFrame][source]¶ Fit and Transform the dataframe X.
- Parameters
- XDataFrame.
Input dataframe.
- ySeries, default None.
Input target.
- Returns
- XDataFrame
Transformed dataframe.
-
static
check_dataframe
(X: Union[pd.DataFrame, ks.DataFrame, dd.DataFrame])[source]¶ Validate dataframe.
- Parameters
- XDataFrame
Dataframe.
-
static
check_target
(X: Union[pd.DataFrame, ks.DataFrame, dd.DataFrame], y: Union[pd.Series, ks.Series, dd.Series])[source]¶ Validate target.
- Parameters
- XDataFrame
Dataframe.
- ySeries
Target values.
-
static
check_dataframe_is_numerics
(X: Union[pd.DataFrame, ks.DataFrame, dd.DataFrame])[source]¶ Check if dataframe is only numerics.
- Parameters
- XDataFrame
Dataframe.
-
check_datatype
(dtype, accepted_dtypes)[source]¶ Check if dataframe is only numerics.
- Parameters
- XDataFrame
Dataframe.
-
static
check_binary_target
(X: Union[pd.DataFrame, ks.DataFrame, dd.DataFrame], y: Union[pd.Series, ks.Series, dd.Series])[source]¶ Raise an error if the target is not binary.
- Parameters
- ySeries
Target values.
-
static
check_multiclass_target
(y: Union[pd.Series, ks.Series, dd.Series])[source]¶ Raise an error if the target is not discrete.
- Parameters
- ySeries
Target values.
-
static
check_regression_target
(y: Union[pd.Series, ks.Series, dd.Series])[source]¶ Raise an error if the target is not discrete.
- Parameters
- ySeries
Target values.
-
static
check_dataframe_contains_numerics
(X: Union[pd.DataFrame, ks.DataFrame, dd.DataFrame])[source]¶ Check if dataframe is only numerics.
- Parameters
- XDataFrame
Dataframe.
-
check_dataframe_with_objects
(X: Union[pd.DataFrame, ks.DataFrame, dd.DataFrame])[source]¶ Check if dataframe contains object columns.
- Parameters
- XDataFrame
Dataframe.
-
check_array_is_numerics
(X: numpy.ndarray)[source]¶ Check if array is only numerics.
- Parameters
- Xnp.ndarray
Array.
-
check_nans
(X: Union[pd.DataFrame, ks.DataFrame, dd.DataFrame], columns: List[str])[source]¶ Raise an error if X contains NaN values.
- Parameters
- XDataFrame
Dataframe.
- theta_vecList[float]
List of columns.
-
static
get_column_names
(inplace: bool, columns: List[str], suffix: str)[source]¶ 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_params
(deep=True)¶ Get parameters for this estimator.
- Parameters
- deepbool, default=True
If True, will return the parameters for this estimator and contained subobjects that are estimators.
- Returns
- paramsdict
Parameter names mapped to their values.
-
set_params
(**params)¶ Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as
Pipeline
). The latter have parameters of the form<component>__<parameter>
so that it’s possible to update each component of a nested object.- Parameters
- **paramsdict
Estimator parameters.
- Returns
- selfestimator instance
Estimator instance.