|
| 1 | +""" |
| 2 | +Hands-On with Column Selection and Transformers |
| 3 | +=============================================== |
| 4 | +
|
| 5 | +In previous examples, we saw how skrub provides powerful abstractions like |
| 6 | +:class:`~skrub.TableVectorizer` and :func:`~skrub.tabular_learner` to create pipelines. |
| 7 | +
|
| 8 | +In this new example, we show how to create more flexible pipelines by selecting |
| 9 | +and transforming dataframe columns using arbitrary logic. |
| 10 | +""" |
| 11 | + |
| 12 | +# %% |
| 13 | +# We begin with loading a dataset with heterogeneous datatypes, and replacing Pandas's |
| 14 | +# display with the TableReport display via :func:`skrub.set_config`. |
| 15 | +import skrub |
| 16 | +from skrub.datasets import fetch_employee_salaries |
| 17 | + |
| 18 | +skrub.set_config(use_tablereport=True) |
| 19 | +data = fetch_employee_salaries() |
| 20 | +X, y = data.X, data.y |
| 21 | +X |
| 22 | + |
| 23 | +# %% |
| 24 | +# Our goal is now to apply a :class:`~skrub.StringEncoder` to two columns of our |
| 25 | +# choosing: ``division`` and ``employee_position_title``. |
| 26 | +# |
| 27 | +# We can achieve this using :class:`~skrub.ApplyToCols`, whose job is to apply a |
| 28 | +# transformer to multiple columns independently, and let unmatched columns through |
| 29 | +# without changes. |
| 30 | +# This can be seen as a handy drop-in replacement of the |
| 31 | +# :class:`~sklearn.compose.ColumnTransformer`. |
| 32 | +# |
| 33 | +# Since we selected two columns and set the number of components to ``30`` each, |
| 34 | +# :class:`~skrub.ApplyToCols` will create ``2*30`` embedding columns in the dataframe |
| 35 | +# ``Xt``, which we prefix with ``lsa_``. |
| 36 | +from skrub import ApplyToCols, StringEncoder |
| 37 | + |
| 38 | +apply_string_encoder = ApplyToCols( |
| 39 | + StringEncoder(n_components=30), |
| 40 | + cols=["division", "employee_position_title"], |
| 41 | + rename_columns="lsa_{}", |
| 42 | +) |
| 43 | +Xt = apply_string_encoder.fit_transform(X) |
| 44 | +Xt |
| 45 | + |
| 46 | +# %% |
| 47 | +# In addition to the :class:`~skrub.ApplyToCols` class, the |
| 48 | +# :class:`~skrub.ApplyToFrame` class is useful for transformers that work on multiple |
| 49 | +# columns at once, such as the :class:`~sklearn.decomposition.PCA` which reduces the |
| 50 | +# number of components. |
| 51 | +# |
| 52 | +# To select columns without hardcoding their names, we introduce |
| 53 | +# :ref:`selectors<selectors>`, which allow for flexible matching pattern and composable |
| 54 | +# logic. |
| 55 | +# |
| 56 | +# The regex selector below will match all columns prefixed with ``"lsa"``, and pass them |
| 57 | +# to :class:`~skrub.ApplyToFrame` which will assemble these columns into a dataframe and |
| 58 | +# finally pass it to the PCA. |
| 59 | +from sklearn.decomposition import PCA |
| 60 | + |
| 61 | +from skrub import ApplyToFrame |
| 62 | +from skrub import selectors as s |
| 63 | + |
| 64 | +apply_pca = ApplyToFrame(PCA(n_components=8), cols=s.regex("lsa")) |
| 65 | +Xt = apply_pca.fit_transform(Xt) |
| 66 | +Xt |
| 67 | + |
| 68 | +# %% |
| 69 | +# These two selectors are scikit-learn transformers and can be chained together within |
| 70 | +# a :class:`~sklearn.pipeline.Pipeline`. |
| 71 | +from sklearn.pipeline import make_pipeline |
| 72 | + |
| 73 | +make_pipeline( |
| 74 | + apply_string_encoder, |
| 75 | + apply_pca, |
| 76 | +).fit_transform(X) |
| 77 | + |
| 78 | +# %% |
| 79 | +# Note that selectors also come in handy in a pipeline to select or drop columns, using |
| 80 | +# :class:`~skrub.SelectCols` and :class:`~skrub.DropCols`! |
| 81 | +from sklearn.preprocessing import StandardScaler |
| 82 | + |
| 83 | +from skrub import SelectCols |
| 84 | + |
| 85 | +# Select only numerical columns |
| 86 | +pipeline = make_pipeline( |
| 87 | + SelectCols(cols=s.numeric()), |
| 88 | + StandardScaler(), |
| 89 | +).set_output(transform="pandas") |
| 90 | +pipeline.fit_transform(Xt) |
| 91 | + |
| 92 | +# %% |
| 93 | +# Let's run through one more example to showcase the expressiveness of the selectors. |
| 94 | +# Suppose we want to apply an :class:`~sklearn.preprocessing.OrdinalEncoder` on |
| 95 | +# categorical columns with low cardinality (e.g., fewer than ``40`` unique values). |
| 96 | +# |
| 97 | +# We define a column filter using skrub selectors with a lambda function. Note that |
| 98 | +# the same effect can be obtained directly by using |
| 99 | +# :func:`~srkub.selectors.cardinality_below`. |
| 100 | +from sklearn.preprocessing import OrdinalEncoder |
| 101 | + |
| 102 | +low_cardinality = s.filter(lambda col: col.nunique() < 40) |
| 103 | +ApplyToCols(OrdinalEncoder(), cols=s.string() & low_cardinality).fit_transform(X) |
| 104 | + |
| 105 | +# %% |
| 106 | +# Notice how we composed the selector with :func:`~skrub.selectors.string()` |
| 107 | +# using a logical operator. This resulting selector matches string |
| 108 | +# columns with cardinality below ``40``. |
| 109 | +# |
| 110 | +# We can also define the opposite selector ``high_cardinality`` using the negation |
| 111 | +# operator ``~`` and apply a :class:`skrub.StringEncoder` to vectorize those |
| 112 | +# columns. |
| 113 | +from sklearn.ensemble import HistGradientBoostingRegressor |
| 114 | + |
| 115 | +high_cardinality = ~low_cardinality |
| 116 | +pipeline = make_pipeline( |
| 117 | + ApplyToCols( |
| 118 | + OrdinalEncoder(), |
| 119 | + cols=s.string() & low_cardinality, |
| 120 | + ), |
| 121 | + ApplyToCols( |
| 122 | + StringEncoder(), |
| 123 | + cols=s.string() & high_cardinality, |
| 124 | + ), |
| 125 | + HistGradientBoostingRegressor(), |
| 126 | +).fit(X, y) |
| 127 | +pipeline |
| 128 | + |
| 129 | +# %% |
| 130 | +# Interestingly, the pipeline above is similar to the datatype dispatching performed by |
| 131 | +# :class:`~skrub.TableVectorizer`, also used in :func:`~skrub.tabular_learner`. |
| 132 | +# |
| 133 | +# Click on the dropdown arrows next to the datatype to see the columns are mapped to |
| 134 | +# the different transformers in :class:`~skrub.TableVectorizer`. |
| 135 | +from skrub import tabular_learner |
| 136 | + |
| 137 | +tabular_learner("regressor").fit(X, y) |
| 138 | +# %% |
0 commit comments