Skip to content

Commit efa4e2a

Browse files
committed
Pushing the docs to dev/ for branch: main, commit f982f322144a5f0de884e7aaf25af1483cafc322
1 parent 28bb947 commit efa4e2a

234 files changed

Lines changed: 255473 additions & 21239 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

dev/CHANGES.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,10 +599,13 @@ <h3>New features<a class="headerlink" href="#new-features" title="Link to this h
599599
The current implementation includes detection of columns that contain only a
600600
single value (constant columns), only missing values, or all unique values (such
601601
as IDs). <a class="reference external" href="https://github.com/skrub-data/skrub/pull/1313">#1313</a> by <a class="reference external" href="https://github.com/rcap107">Riccardo Cappuzzo</a>.</p></li>
602-
<li><p><code class="xref py py-func docutils literal notranslate"><span class="pre">get_config()</span></code>, <code class="xref py py-func docutils literal notranslate"><span class="pre">set_config()</span></code> and <code class="xref py py-func docutils literal notranslate"><span class="pre">config_context()</span></code> are now available
602+
<li><p><a class="reference internal" href="reference/generated/skrub.get_config.html#skrub.get_config" title="skrub.get_config"><code class="xref py py-func docutils literal notranslate"><span class="pre">get_config()</span></code></a>, <a class="reference internal" href="reference/generated/skrub.set_config.html#skrub.set_config" title="skrub.set_config"><code class="xref py py-func docutils literal notranslate"><span class="pre">set_config()</span></code></a> and <a class="reference internal" href="reference/generated/skrub.config_context.html#skrub.config_context" title="skrub.config_context"><code class="xref py py-func docutils literal notranslate"><span class="pre">config_context()</span></code></a> are now available
603603
to configure settings for dataframes display and expressions. <a class="reference internal" href="reference/generated/skrub.patch_display.html#skrub.patch_display" title="skrub.patch_display"><code class="xref py py-func docutils literal notranslate"><span class="pre">patch_display()</span></code></a>
604604
and <a class="reference internal" href="reference/generated/skrub.unpatch_display.html#skrub.unpatch_display" title="skrub.unpatch_display"><code class="xref py py-func docutils literal notranslate"><span class="pre">unpatch_display()</span></code></a> are deprecated and will be removed in the next release
605605
of skrub. <a class="reference external" href="https://github.com/skrub-data/skrub/pull/1427">#1427</a> by <a class="reference external" href="https://github.com/Vincent-Maladiere">Vincent Maladiere</a>.</p></li>
606+
<li><p><a class="reference internal" href="reference/generated/skrub.ApplyToCols.html#skrub.ApplyToCols" title="skrub.ApplyToCols"><code class="xref py py-class docutils literal notranslate"><span class="pre">ApplyToCols</span></code></a> and <a class="reference internal" href="reference/generated/skrub.ApplyToFrame.html#skrub.ApplyToFrame" title="skrub.ApplyToFrame"><code class="xref py py-class docutils literal notranslate"><span class="pre">ApplyToFrame</span></code></a> are now available to apply transformers
607+
on a set of columns independently and jointly respectively.
608+
<a class="reference external" href="https://github.com/skrub-data/skrub/pull/1478">#1478</a> by <a class="reference external" href="https://github.com/Vincent-Maladiere">Vincent Maladiere</a>.</p></li>
606609
</ul>
607610
</section>
608611
<section id="id1">
Binary file not shown.
Binary file not shown.
Binary file not shown.
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
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+
# %%
Binary file not shown.
Binary file not shown.
0 Bytes
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)