Skip to content

Commit bd7c251

Browse files
rcap107glemaitre
andcommitted
DOC - Improving getting started example (#1627)
Co-authored-by: Guillaume Lemaitre <guillaume@probabl.ai>
1 parent d2c5146 commit bd7c251

3 files changed

Lines changed: 92 additions & 148 deletions

File tree

doc/modules/data_ops/validation/hyperparameter_tuning.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,6 @@ We can see the generated parameter grid with :func:`DataOps.skb.describe_param_g
200200
"- choose_from({'number': …, 'text': …}): ['number', 'text']\n"
201201

202202
A more advanced application of this technique is used in
203-
:ref:`this tutorial on forecasting timeseries <https://skrub-data.org/EuroSciPy2025/content/notebooks/single_horizon_prediction.html>_`,
203+
`this tutorial on forecasting timeseries <https://skrub-data.org/EuroSciPy2025/content/notebooks/single_horizon_prediction.html>`_,
204204
along with the feature engineering required to prepare the columns, and the
205205
analysis of the results.

doc/modules/tablereport/exploring_dataframes_interactively.rst

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,6 @@ be copied in a script.
4545
The TableReport can be used in a notebook cell, or it can be opened in a browser
4646
window using ``TableReport(df).open()``.
4747

48-
It is also possible to export the |TableReport| in HTML or JSON format: see
49-
:ref:`user_guide_table_report_sharing` for more detail.
50-
51-
Customizing and sharing the |TableReport|
52-
=========================================
53-
54-
.. |set_config| replace:: :func:`~skrub.set_config`
55-
5648
.. _user_guide_table_report_customize:
5749

5850
Altering the Appearance of the |TableReport|
@@ -73,8 +65,6 @@ Parameters can be made permanent in a script by altering the configuration with
7365
|set_config|, or by setting the respective environment variables. Refer to
7466
:ref:`user_guide_configuration_parameters` for more detail.
7567

76-
.. |TableReport| replace:: :class:`~skrub.TableReport`
77-
7868
.. _user_guide_table_report_sharing:
7969

8070
Exporting and Sharing the |TableReport|

examples/00_getting_started.py

Lines changed: 91 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -2,75 +2,52 @@
22
Getting Started
33
===============
44
5-
This guide showcases some of the features of ``skrub``, an open-source package
6-
that aims at bridging the gap between tabular data stored in Pandas or Polars
7-
dataframes, and machine-learning models.
8-
9-
Much of ``skrub`` revolves around simplifying many of the tasks that are involved
5+
This guide showcases some of the features of skrub.
6+
Much of skrub revolves around simplifying many of the tasks that are involved
107
in pre-processing raw data into a format that shallow or classic machine-learning
118
models can understand, that is, numerical data.
129
13-
``skrub`` does this by vectorizing, assembling, and encoding tabular data through
14-
a number of features that we present in this example and the following.
10+
Skrub achieves this by vectorizing, assembling, and encoding tabular data through
11+
the features we present in this example and the following ones.
12+
13+
.. |TableReport| replace:: :class:`~skrub.TableReport`
14+
.. |Cleaner| replace:: :class:`~skrub.Cleaner`
15+
.. |set_config| replace:: :func:`~skrub.set_config`
16+
.. |tabular_pipeline| replace:: :func:`~skrub.tabular_pipeline`
17+
.. |TableVectorizer| replace:: :class:`~skrub.TableVectorizer`
18+
.. |Joiner| replace:: :class:`~skrub.Joiner`
19+
.. |SquashingScaler| replace:: :class:`~skrub.SquashingScaler`
20+
.. |DatetimeEncoder| replace:: :class:`~skrub.DatetimeEncoder`
21+
.. |ApplyToCols| replace:: :class:`~skrub.ApplyToCols`
22+
.. |StringEncoder| replace:: :class:`~skrub.StringEncoder`
23+
.. |TextEncoder| replace:: :class:`~skrub.TextEncoder`
1524
"""
1625

17-
# %%
18-
# Downloading example datasets
19-
# ----------------------------
20-
#
21-
# The :obj:`~skrub.datasets` module allows us to download tabular datasets and
22-
# demonstrate ``skrub``'s features.
23-
#
24-
# .. note::
25-
#
26-
# You can control the directory where the datasets are stored by:
27-
#
28-
# - setting in your environment the ``SKRUB_DATA_DIRECTORY`` variable to an
29-
# absolute directory path,
30-
# - using the parameter ``data_directory`` in fetch functions, which takes
31-
# precedence over the envar.
32-
#
33-
# By default, the datasets are stored in a folder named "skrub_data" in the
34-
# user home folder.
35-
3626

3727
# %%
28+
# Preliminary exploration with the |TableReport|
29+
# ----------------------------------------------
3830
from skrub.datasets import fetch_employee_salaries
3931

4032
dataset = fetch_employee_salaries()
4133
employees_df, salaries = dataset.X, dataset.y
4234

4335
# %%
44-
# Explore all the available datasets in :ref:`datasets_ref`.
36+
# Typically, the first step with new data is exploration and parsing.
37+
# To quickly get an overview of a dataframe's contents, use the |TableReport|.
4538

4639
# %%
47-
# Preliminary exploration and parsing of data
48-
# -------------------------------------------------
49-
# Typically, the first operations that are done on new data involve data exploration
50-
# and parsing.
51-
# To quickly get an overview of a dataframe's contents, use the
52-
# :class:`~skrub.TableReport`.
53-
# Here, we also use the :class:`~skrub.Cleaner`, a transformer that cleans the
54-
# dataframe by parsing nulls and dates, and by dropping "uninformative" columns
55-
# (e.g., that contain too many nulls, or that are constant).
56-
#
57-
58-
# %%
59-
from skrub import Cleaner, TableReport
40+
from skrub import TableReport
6041

6142
TableReport(employees_df)
6243

6344
# %%
64-
# From the Report above, we can see that there are datetime columns, so we use the
65-
# :class:`~skrub.Cleaner` to parse them.
66-
67-
employees_df = Cleaner().fit_transform(employees_df)
68-
TableReport(employees_df)
69-
70-
# %%
71-
#
7245
# You can use the interactive display above to explore the dataset visually.
7346
#
47+
# It is also possible to tell skrub to replace the default pandas and polars
48+
# displays with |TableReport| by modifying the global config with
49+
# |set_config|.
50+
#
7451
# .. note::
7552
#
7653
# You can see a few more `example reports`_ online. We also
@@ -80,31 +57,36 @@
8057
#
8158
# .. _example reports: https://skrub-data.org/skrub-reports/examples/
8259
# .. _demo: https://skrub-data.org/skrub-reports/
60+
#
61+
# From the report above, we see that there are columns with date and time stored
62+
# as `object` dtype (cf. "Stats" tab of the report).
63+
# Datatypes not being parsed correctly is a scenario that occurs commonly after
64+
# reading a table. We can use the |Cleaner| to address this.
65+
# In the next section, we show that this transformer does additional cleaning.
8366

8467
# %%
85-
# It is also possible to tell ``skrub`` to replace the default pandas & polars
86-
# displays with ``TableReport`` by modifying the global config with
87-
# :func:`~skrub.set_config`.
88-
89-
from skrub import set_config
68+
# Sanitizing data with the |Cleaner|
69+
# ----------------------------------
70+
# Here, we use the |Cleaner|, a transformer that sanitizing the
71+
# dataframe by parsing nulls and dates, and by dropping "uninformative" columns
72+
# (e.g., columns with too many nulls or that are constant).
73+
#
9074

91-
set_config(use_table_report=True)
75+
from skrub import Cleaner
9276

93-
employees_df
77+
employees_df = Cleaner().fit_transform(employees_df)
78+
TableReport(employees_df)
9479

9580
# %%
96-
# This setting can easily be reverted:
97-
98-
set_config(use_table_report=False)
99-
100-
employees_df
81+
# We can see from the "Stats" tab that now the column `date_first_hired` has been
82+
# parsed correctly as a Datetime.
10183

10284
# %%
10385
# Easily building a strong baseline for tabular machine learning
10486
# --------------------------------------------------------------
10587
#
106-
# The goal of ``skrub`` is to ease tabular data preparation for machine learning.
107-
# The :func:`~skrub.tabular_pipeline` function provides an easy way to build a simple
88+
# The goal of skrub is to ease tabular data preparation for machine learning.
89+
# The |tabular_pipeline| function provides an easy way to build a simple
10890
# but reliable machine learning model that works well on most tabular data.
10991

11092

@@ -114,87 +96,41 @@
11496
from skrub import tabular_pipeline
11597

11698
model = tabular_pipeline("regressor")
99+
model
100+
# %%
117101
results = cross_validate(model, employees_df, salaries)
118102
results["test_score"]
119103

120104
# %%
121105
# To handle rich tabular data and feed it to a machine learning model, the
122-
# pipeline returned by :func:`~skrub.tabular_pipeline` preprocesses and encodes
123-
# strings, categories and dates using the :class:`~skrub.TableVectorizer`.
106+
# pipeline returned by |tabular_pipeline| preprocesses and encodes
107+
# strings, categories and dates using the |TableVectorizer|.
124108
# See its documentation or :ref:`sphx_glr_auto_examples_01_encodings.py` for
125109
# more details. An overview of the chosen defaults is available in
126110
# :ref:`user_guide_tabular_pipeline`.
127111

128112

129-
# %%
130-
# Assembling data
131-
# ---------------
132-
#
133-
# ``skrub`` allows imperfect assembly of data, such as joining dataframes
134-
# on columns that contain typos. ``skrub``'s joiners have ``fit`` and
135-
# ``transform`` methods, storing information about the data across calls.
136-
#
137-
# The :class:`~skrub.Joiner` allows fuzzy-joining multiple tables, each row of
138-
# a main table will be augmented with values from the best match in the auxiliary table.
139-
# You can control how distant fuzzy-matches are allowed to be with the
140-
# ``max_dist`` parameter.
141-
142-
# %%
143-
# In the following, we add information about countries to a table containing
144-
# airports and the cities they are in:
145-
146-
# %%
147-
import pandas as pd
148-
149-
from skrub import Joiner
150-
151-
airports = pd.DataFrame(
152-
{
153-
"airport_id": [1, 2],
154-
"airport_name": ["Charles de Gaulle", "Aeroporto Leonardo da Vinci"],
155-
"city": ["Paris", "Roma"],
156-
}
157-
)
158-
# Notice the "Rome" instead of "Roma"
159-
capitals = pd.DataFrame(
160-
{"capital": ["Berlin", "Paris", "Rome"], "country": ["Germany", "France", "Italy"]}
161-
)
162-
joiner = Joiner(
163-
capitals,
164-
main_key="city",
165-
aux_key="capital",
166-
max_dist=0.8,
167-
add_match_info=False,
168-
)
169-
joiner.fit_transform(airports)
170-
171-
# %%
172-
# Information about countries have been added, even if the rows aren't exactly matching.
173-
#
174-
# ``skrub`` allows to aggregate multiple tables according to various strategies: you
175-
# can see other ways to join multiple tables in :ref:`userguide_joining_tables`.
176-
177113
# %%
178114
# Encoding any data as numerical features
179115
# ---------------------------------------
180116
#
181-
# Tabular data can contain a variety of datatypes, ranging from numerical, to
182-
# datetimes, to categories, strings, and text. Encoding features in a meaningful
183-
# way requires a lot of effort and is a major part of the feature engineering
184-
# process that is required to properly train machine learning models.
117+
# Tabular data can contain a variety of datatypes, from numerical to
118+
# datetimes, categories, strings, and text. Encoding features in a meaningful
119+
# way requires significant effort and is a major part of the feature engineering
120+
# process required to properly train machine learning models.
185121
#
186-
# ``skrub`` helps with this by providing various transformers that automatically
122+
# Skrub helps with this by providing various transformers that automatically
187123
# encode different datatypes into ``float32`` features.
188124
#
189-
# For **numerical features**, the :class:`~skrub.SquashingScaler` applies a robust
125+
# For **numerical features**, the |SquashingScaler| applies a robust
190126
# scaling technique that is less sensitive to outliers. Check the
191-
# :ref:`relative example <sphx_glr_auto_examples_11_squashing_scaler.py>`
127+
# :ref:`relative example <sphx_glr_auto_examples_10_squashing_scaler.py>`
192128
# for more information on the feature.
193129
#
194-
# For **datetime columns**, ``skrub`` provides the :class:`~skrub.DatetimeEncoder`
130+
# For **datetime columns**, skrub provides the |DatetimeEncoder|
195131
# which can extract useful features such as year, month, day, as well as additional
196132
# features such as weekday or day of year. Periodic encoding with trigonometric
197-
# or spline features is also available. Refer to the :class:`~skrub.DatetimeEncoder`
133+
# or spline features is also available. Refer to the |DatetimeEncoder|
198134
# documentation for more detail.
199135
#
200136

@@ -211,10 +147,10 @@
211147
data = Cleaner().fit_transform(data)
212148
TableReport(data)
213149
# %%
214-
# ``skrub`` transformers are applied column-by-column, but it is possible to use
215-
# the :class:`~skrub.ApplyToCols` meta-transformer to apply a transformer to
150+
# Skrub transformers are applied column-by-column, but it's possible to use
151+
# the |ApplyToCols| meta-transformer to apply a transformer to
216152
# multiple columns at once. Complex column selection is possible using
217-
# :ref:`skrub's column selectors <userguide_selectors>`.
153+
# :ref:`skrub's column selectors <user_guide_selectors>`.
218154

219155
from skrub import ApplyToCols, DatetimeEncoder
220156

@@ -224,10 +160,10 @@
224160

225161
# %%
226162
# Finally, when a column contains **categorical or string data**, it can be
227-
# encoded using various encoders provided by ``skrub``. The default encoder is
228-
# the :class:`~skrub.StringEncoder`, which encodes categories using
163+
# encoded using various encoders provided by skrub. The default encoder is
164+
# the |StringEncoder|, which encodes categories using
229165
# `Latent Semantic Analysis (LSA) <https://scikit-learn.org/stable/modules/decomposition.html#about-truncated-svd-and-latent-semantic-analysis-(lsa)>`_.
230-
# It is a simple and efficient way to encode categories, and works well in
166+
# It is a simple and efficient way to encode categories and works well in
231167
# practice.
232168

233169
data = pd.DataFrame(
@@ -243,22 +179,40 @@
243179

244180
# %%
245181
# If your data includes a lot of text, you may want to use the
246-
# :class:`~skrub.TextEncoder`,
182+
# |TextEncoder|,
247183
# which uses pre-trained language models retrieved from the HuggingFace hub to
248184
# create meaningful text embeddings.
249-
# See :ref:`userguide_encoders` for more details on all the categorical encoders
250-
# provided by ``skrub``, and :ref:`sphx_glr_auto_examples_01_encodings.py` for a
185+
# See :ref:`user_guide_encoders_index` for more details on all the categorical encoders
186+
# provided by skrub, and :ref:`sphx_glr_auto_examples_01_encodings.py` for a
251187
# comparison between the different methods.
188+
#
189+
190+
# %%
191+
# Assembling data
192+
# ---------------
193+
#
194+
# Skrub allows imperfect assembly of data, such as joining dataframes
195+
# on columns that contain typos. Skrub's joiners have ``fit`` and
196+
# ``transform`` methods, storing information about the data across calls.
197+
#
198+
# The |Joiner| allows fuzzy-joining multiple tables, where each row of
199+
# a main table will be augmented with values from the best match in the auxiliary table.
200+
# You can control how distant fuzzy-matches are allowed to be with the
201+
# ``max_dist`` parameter.
202+
#
203+
# Skrub also allows you to aggregate multiple tables according to various strategies.
204+
# You can see other ways to join multiple tables in
205+
# :ref:`user_guide_joining_dataframes`.
252206

253207
# %%
254208
# Advanced use cases
255209
# ----------------------
256210
# If your use case involves more complex data preparation, hyperparameter tuning,
257211
# or model selection, if you want to build a multi-table pipeline that requires
258-
# assembling and preparing multiple tables, or if you want to make sure that the
259-
# data preparation can be reproduced exactly, you can use the ``skrub`` Data Ops,
260-
# a powerful framework which provides tools to build complex data processing pipelines.
261-
# See the relative :ref:`user guide <userguide_data_ops>` and the
212+
# assembling and preparing multiple tables, or if you want to ensure that the
213+
# data preparation can be reproduced exactly, you can use the skrub Data Ops,
214+
# a powerful framework that provides tools to build complex data processing pipelines.
215+
# See the related :ref:`user guide <user_guide_data_ops_index>` and the
262216
# :ref:`data_ops_examples_ref`
263217
# examples for more details.
264218

@@ -267,11 +221,11 @@
267221
# ----------
268222
#
269223
# We have briefly covered pipeline creation, vectorizing, assembling, and encoding
270-
# data. We presented the main functionalities of ``skrub``, but there is much
271-
# more to it!
224+
# data. We presented the main functionalities of skrub, but there is much
225+
# more to explore!
272226
#
273227
# Please refer to our :ref:`user_guide` for a more in-depth presentation of
274-
# ``skrub``'s concepts, or visit our
228+
# skrub's concepts, or visit our
275229
# `examples <https://skrub-data.org/stable/auto_examples>`_ for more
276230
# illustrations of the tools that we provide!
277231
#

0 commit comments

Comments
 (0)