|
2 | 2 | Getting Started |
3 | 3 | =============== |
4 | 4 |
|
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 |
10 | 7 | in pre-processing raw data into a format that shallow or classic machine-learning |
11 | 8 | models can understand, that is, numerical data. |
12 | 9 |
|
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` |
15 | 24 | """ |
16 | 25 |
|
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 | | - |
36 | 26 |
|
37 | 27 | # %% |
| 28 | +# Preliminary exploration with the |TableReport| |
| 29 | +# ---------------------------------------------- |
38 | 30 | from skrub.datasets import fetch_employee_salaries |
39 | 31 |
|
40 | 32 | dataset = fetch_employee_salaries() |
41 | 33 | employees_df, salaries = dataset.X, dataset.y |
42 | 34 |
|
43 | 35 | # %% |
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|. |
45 | 38 |
|
46 | 39 | # %% |
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 |
60 | 41 |
|
61 | 42 | TableReport(employees_df) |
62 | 43 |
|
63 | 44 | # %% |
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 | | -# |
72 | 45 | # You can use the interactive display above to explore the dataset visually. |
73 | 46 | # |
| 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 | +# |
74 | 51 | # .. note:: |
75 | 52 | # |
76 | 53 | # You can see a few more `example reports`_ online. We also |
|
80 | 57 | # |
81 | 58 | # .. _example reports: https://skrub-data.org/skrub-reports/examples/ |
82 | 59 | # .. _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. |
83 | 66 |
|
84 | 67 | # %% |
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 | +# |
90 | 74 |
|
91 | | -set_config(use_table_report=True) |
| 75 | +from skrub import Cleaner |
92 | 76 |
|
93 | | -employees_df |
| 77 | +employees_df = Cleaner().fit_transform(employees_df) |
| 78 | +TableReport(employees_df) |
94 | 79 |
|
95 | 80 | # %% |
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. |
101 | 83 |
|
102 | 84 | # %% |
103 | 85 | # Easily building a strong baseline for tabular machine learning |
104 | 86 | # -------------------------------------------------------------- |
105 | 87 | # |
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 |
108 | 90 | # but reliable machine learning model that works well on most tabular data. |
109 | 91 |
|
110 | 92 |
|
|
114 | 96 | from skrub import tabular_pipeline |
115 | 97 |
|
116 | 98 | model = tabular_pipeline("regressor") |
| 99 | +model |
| 100 | +# %% |
117 | 101 | results = cross_validate(model, employees_df, salaries) |
118 | 102 | results["test_score"] |
119 | 103 |
|
120 | 104 | # %% |
121 | 105 | # 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|. |
124 | 108 | # See its documentation or :ref:`sphx_glr_auto_examples_01_encodings.py` for |
125 | 109 | # more details. An overview of the chosen defaults is available in |
126 | 110 | # :ref:`user_guide_tabular_pipeline`. |
127 | 111 |
|
128 | 112 |
|
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 | | - |
177 | 113 | # %% |
178 | 114 | # Encoding any data as numerical features |
179 | 115 | # --------------------------------------- |
180 | 116 | # |
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. |
185 | 121 | # |
186 | | -# ``skrub`` helps with this by providing various transformers that automatically |
| 122 | +# Skrub helps with this by providing various transformers that automatically |
187 | 123 | # encode different datatypes into ``float32`` features. |
188 | 124 | # |
189 | | -# For **numerical features**, the :class:`~skrub.SquashingScaler` applies a robust |
| 125 | +# For **numerical features**, the |SquashingScaler| applies a robust |
190 | 126 | # 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>` |
192 | 128 | # for more information on the feature. |
193 | 129 | # |
194 | | -# For **datetime columns**, ``skrub`` provides the :class:`~skrub.DatetimeEncoder` |
| 130 | +# For **datetime columns**, skrub provides the |DatetimeEncoder| |
195 | 131 | # which can extract useful features such as year, month, day, as well as additional |
196 | 132 | # 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| |
198 | 134 | # documentation for more detail. |
199 | 135 | # |
200 | 136 |
|
|
211 | 147 | data = Cleaner().fit_transform(data) |
212 | 148 | TableReport(data) |
213 | 149 | # %% |
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 |
216 | 152 | # 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>`. |
218 | 154 |
|
219 | 155 | from skrub import ApplyToCols, DatetimeEncoder |
220 | 156 |
|
|
224 | 160 |
|
225 | 161 | # %% |
226 | 162 | # 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 |
229 | 165 | # `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 |
231 | 167 | # practice. |
232 | 168 |
|
233 | 169 | data = pd.DataFrame( |
|
243 | 179 |
|
244 | 180 | # %% |
245 | 181 | # If your data includes a lot of text, you may want to use the |
246 | | -# :class:`~skrub.TextEncoder`, |
| 182 | +# |TextEncoder|, |
247 | 183 | # which uses pre-trained language models retrieved from the HuggingFace hub to |
248 | 184 | # 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 |
251 | 187 | # 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`. |
252 | 206 |
|
253 | 207 | # %% |
254 | 208 | # Advanced use cases |
255 | 209 | # ---------------------- |
256 | 210 | # If your use case involves more complex data preparation, hyperparameter tuning, |
257 | 211 | # 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 |
262 | 216 | # :ref:`data_ops_examples_ref` |
263 | 217 | # examples for more details. |
264 | 218 |
|
|
267 | 221 | # ---------- |
268 | 222 | # |
269 | 223 | # 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! |
272 | 226 | # |
273 | 227 | # 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 |
275 | 229 | # `examples <https://skrub-data.org/stable/auto_examples>`_ for more |
276 | 230 | # illustrations of the tools that we provide! |
277 | 231 | # |
0 commit comments