Install sklearn-pandas
masterYou can install sklearn-pandas using either pip or conda via the conda-forge channel.
# pip install sklearn-pandas
# conda install -c conda-forge sklearn-pandasrepository·master·Indexed 25 days ago
https://github.com/scikit-learn-contrib/sklearn-pandasA bridge between Scikit-Learn machine learning methods and pandas DataFrames. It provides the DataFrameMapper class to map specific DataFrame columns to various transformations, allowing users to recombine them into a feature set. Key features include support for dynamic column selection via callables, custom feature naming with aliases, prefixes, and suffixes, and the ability to return results as pandas DataFrames or scipy sparse matrices.
You can install sklearn-pandas using either pip or conda via the conda-forge channel.
# pip install sklearn-pandas
# conda install -c conda-forge sklearn-pandasTo have fit_transform return a pandas DataFrame instead of a NumPy array, set df_out=True when initializing the DataFrameMapper. The resulting DataFrame will use the names found in transformed_names_ as column headers.
Note: df_out=True is incompatible with default=True or sparse=True.
mapper_df = DataFrameMapper([
('pet', sklearn.preprocessing.LabelBinarizer()),
(['children'], sklearn.preprocessing.StandardScaler())
], df_out=True)By default, transformers receive NumPy arrays. To pass pandas DataFrames or Series directly to the transformers (useful for methods like .dt accessors), set input_df=True in the DataFrameMapper constructor or within a specific column's attribute dictionary.
# Set globally for the whole mapper
mapper_dates = DataFrameMapper([
('dates', DateEncoder())
], input_df=True)
# Set per group of columns
mapper_dates = DataFrameMapper([
('dates', DateEncoder(), {'input_df': True})
])DataFrameMapper supports transformers that require both X and y (target) arguments, such as scikit-learn's feature selection tools. When calling fit_transform, pass the target variable as the second argument.
from sklearn.feature_selection import SelectKBest, chi2
mapper_fs = DataFrameMapper([(['children','salary'], SelectKBest(chi2, k=1))])
# Pass target 'pet' as the second argument
mapper_fs.fit_transform(data[['children','salary']], data['pet'])To include a column in the output without applying any transformation, use None as the transformer in the mapping tuple.
mapper3 = DataFrameMapper([
('pet', sklearn.preprocessing.LabelBinarizer()),
('children', None)
])You can control the names of the output features by providing a dictionary as the third element in the transformation tuple. Supported keys include:
alias: Replaces the generated name with a specific string.prefix: Prepends a string to the generated name.suffix: Appends a string to the generated name.You can specify a default transformer to be applied to all columns not explicitly listed in the mapping.
default=Transformer(): Applies the transformer to unselected columns.default=None: Passes unselected columns through unchanged.default=False (default behavior): Drops all unselected columns.mapper4 = DataFrameMapper([
('pet', sklearn.preprocessing.LabelBinarizer()),
('children', None)
], default=sklearn.preprocessing.StandardScaler())To apply a single transformation to a group of columns (e.g., PCA on multiple features), pass a list of column names as the first element of the tuple.
mapper2 = DataFrameMapper([
(['children', 'salary'], sklearn.decomposition.PCA(1))
])fit or fit_transform, you can inspect the transformed_names_ attribute to see the names of the generated features. This is useful for mapping original features to their transformed counterparts, especially when using encoders that expand a single column into multiple columns.If column names are unknown beforehand, you can use a custom callable or scikit-learn's make_column_selector to select columns during the fit operation.
# Using make_column_selector
mapper = DataFrameMapper([
(sklearn.compose.make_column_selector(dtype_include=float), sklearn.preprocessing.StandardScaler())
])
# Using a custom callable
class GetColumnsStartingWith:
def __init__(self, start_str):
self.pattern = start_str
def __call__(self, X:pd.DataFrame=None):
return [c for c in X.columns if c.startswith(self.pattern)]
mapper = DataFrameMapper([
(GetColumnsStartingWith('petal'), None)
])The DataFrameMapper takes a list of tuples to map columns to transformations. Each tuple follows this structure:
make_column_selector).alias, prefix, or suffix.Note on shapes:
'column' passes a 1D array to the transformer.['column'] passes a 2D array (column vector) to the transformer. This is important for transformers like OneHotEncoder or Imputer that expect 2D input.mapper = DataFrameMapper([
('pet', sklearn.preprocessing.LabelBinarizer()),
(['children'], sklearn.preprocessing.StandardScaler())
])To map pandas DataFrame columns to different scikit-learn transformations, import the DataFrameMapper class from the sklearn_pandas package.
from sklearn_pandas import DataFrameMapper