sklearnFeatureTransformer¶
This class is a subclass of BaseFeatureTransformer and is used for transforming input data using scikit-learn transformers. It provides methods for fitting and transforming the data.
Configuration¶
Required Configuration¶
sklearnFeatureTransformer contains the following required configuration:
transformers: A list of transformers to run. Each transformer is a dictionary with the following entries:transformer: Thesklearn.preprocessingclass to use as the transformer. Required.transformer_args: A list of arguments to pass into the transformer. Optional.transformer_kwargs: A dictionary of keyword arguments to pass into the transformer. Optional.transformer_columns: A list of columns names to apply the transformer to.
Optional Configuration¶
sklearnFeatureTransformer contains no optional configuration.
Default Configuration¶
sklearnFeatureTransformer contains the following default configuration:
column_transformer_kwargs: A dictionary of keyword arguments to pass into theColumnTransformerclass. By default, this contains{'remainder': 'passthrough'}, which explicitly instructs theColumnTransformerto pass through any columns in the dataset untransformed that are not mentioned in thetransformersdictionary. If you override this, note that the default behavior forColumnTransformeris to drop these columns.
Attributes¶
transformer: An instance ofsklearn.compose.ColumnTransformer, which is used to run all the transformers provided in the configuraiton.
Methods¶
fit¶
This method fits the feature transformer to the input data.
def fit(self, data, *args, **kwargs)
Arguments:
data(array-like): The input data to fit the transformer to.**kwargs(optional): Keyword arguments to be passed to thefitmethod.
Returns:
self.transformer: The fittedColumnTransformerobject.
transform¶
This method applies the fitted feature transformer to transform the input data.
def transform(self, data, *args, **kwargs)
Arguments:
data(array-like): The input data to be transformed.**kwargs(optional): Keyword arguments to be passed to thetransformmethod.
Returns:
numpy.ndarrayorscipy.sparse matrix: The transformed data.
fit_transform¶
This method fits the feature transformer to the data and transforms it.
def fit_transform(self, data, *args, **kwargs)
Arguments:
data(array-like): The input data to fit the transformer to and transform.**kwargs(optional): Keyword arguments to be passed to thefit_transformmethod.
Returns:
numpy.ndarrayorscipy.sparse matrix: The transformed data.
_get_transformer¶
This method gets the scikit-learn transformer object based on the given class name.
def _get_transformer(self, transformer_class, *args, **kwargs)
Arguments:
transformer_class: The class name of the transformer.**kwargs(optional): Keyword arguments to be passed to the transformer constructor.
Returns:
object: The instantiated scikit-learn transformer object.
Usage:¶
from lolpop.component import sklearnFeatureTransformer
data = #generate data
conf = {
#insert component configuration here ...
"config" : {
"transformers" : [
{"transformer": "MinMaxScaler",
"transformer_columns": ["height", "weight"]},
{"transformer": "OneHotEncoder",
"transformer_columns": ["sex", "state"]},
]
}
}
transformer = sklearnFeatureTransformer(conf=conf)
transformed_data = transformer.fit_transform(data)
print(transformed_data)
Sample Configuration¶
conf = {
"config": {
"transformers":
[
{"transformer": "OneHotEncoder", "transformer_columns": ["Sex"]},
{"transformer": "MinMaxScaler", "transformer_columns": ["Height", "Weight", "Diameter"]},
]
}
}