-
Notifications
You must be signed in to change notification settings - Fork 109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DOC Rework GapEncoder
example
#686
Merged
Vincent-Maladiere
merged 19 commits into
skrub-data:main
from
LilianBoulard:rework-gap-example
Nov 21, 2023
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
42b5d5b
Merge branch 'main' of https://github.com/skrub-data/skrub
LilianBoulard c306c09
Merge branch 'main' of https://github.com/skrub-data/skrub
LilianBoulard ac04460
Merge branch 'main' of https://github.com/skrub-data/skrub
LilianBoulard 4fcca5c
Rework gap example
LilianBoulard ef3ab1b
Apply suggestions from code review
LilianBoulard 3f49233
Merge branch 'rework-gap-example' of https://github.com/LilianBoulard…
LilianBoulard 6417dcd
Improvements
LilianBoulard 0e9c97e
Rename file
LilianBoulard f4db3ea
Rename file again
LilianBoulard d14aff7
Apply suggestions from code review
LilianBoulard 06ada4d
Merge branch 'rework-gap-example' of https://github.com/LilianBoulard…
LilianBoulard acbfc3c
Merge branch 'main' of https://github.com/skrub-data/skrub into rewor…
LilianBoulard 1bdb681
Remove preprocessing
LilianBoulard b003648
Merge commit 'refs/pull/686/head' of github.com:skrub-data/skrub into…
jovan-stojanovic 6834873
fix error
jovan-stojanovic 9e33b1e
add point
jovan-stojanovic 8ae1b8c
merge with main
jovan-stojanovic deb47fa
add note on dirty data
jovan-stojanovic a0a5984
Merge branch 'main' into rework-gap-example
Vincent-Maladiere File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
""" | ||
.. _example_interpreting_gap_encoder: | ||
|
||
========================================== | ||
Feature interpretation with the GapEncoder | ||
========================================== | ||
|
||
In this notebook, we will explore the output and inner workings of the | ||
|GapEncoder|, one of the `high cardinality categorical encoders <https://inria.hal.science/hal-02171256v4>`_ | ||
provided by skrub. | ||
|
||
.. |GapEncoder| replace:: | ||
:class:`~skrub.GapEncoder` | ||
|
||
.. |SimilarityEncoder| replace:: | ||
:class:`~skrub.SimilarityEncoder` | ||
""" | ||
|
||
############################################################################### | ||
# The |GapEncoder| is scalable and interpretable in terms of | ||
# finding latent categories, as we will show. | ||
# | ||
# First, let's retrieve the | ||
# `employee salaries dataset <https://www.openml.org/d/42125>`_: | ||
|
||
from skrub.datasets import fetch_employee_salaries | ||
|
||
dataset = fetch_employee_salaries() | ||
|
||
# Alias X and y | ||
X, y = dataset.X, dataset.y | ||
|
||
X | ||
|
||
############################################################################### | ||
# Encoding job titles | ||
# ------------------- | ||
# | ||
# Let's look at the job titles, the column containing dirty data we want to encode: | ||
# | ||
# .. topic:: Note: | ||
# | ||
# Dirty data, as opposed to clean, are all non-curated categorical | ||
# columns with variations such as typos, abbreviations, duplications, | ||
# alternate naming conventions etc. | ||
# | ||
|
||
X_dirty = X[["employee_position_title"]] | ||
|
||
############################################################################### | ||
# Let's have a look at a sample of the job titles: | ||
|
||
X_dirty.sort_values(by="employee_position_title").tail(15) | ||
|
||
############################################################################### | ||
# Then, we create an instance of the |GapEncoder| with 10 components. | ||
# This means that the encoder will attempt to extract 10 latent topics | ||
# from the input data: | ||
|
||
from skrub import GapEncoder | ||
|
||
enc = GapEncoder(n_components=10, random_state=1) | ||
|
||
############################################################################### | ||
# Finally, we fit the model on the dirty categorical data and transform it | ||
# in order to obtain encoded vectors of size 10: | ||
|
||
X_enc = enc.fit_transform(X_dirty) | ||
X_enc.shape | ||
|
||
############################################################################### | ||
# Interpreting encoded vectors | ||
# ---------------------------- | ||
# | ||
# The |GapEncoder| can be understood as a continuous encoding | ||
# on a set of latent topics estimated from the data. The latent topics | ||
# are built by capturing combinations of substrings that frequently | ||
# co-occur, and encoded vectors correspond to their activations. | ||
# To interpret these latent topics, we select for each of them a few labels | ||
# from the input data with the highest activations. | ||
# In the example below we select 3 labels to summarize each topic. | ||
|
||
topic_labels = enc.get_feature_names_out(n_labels=3) | ||
for k, labels in enumerate(topic_labels): | ||
print(f"Topic n°{k}: {labels}") | ||
|
||
############################################################################### | ||
# As expected, topics capture labels that frequently co-occur. For instance, | ||
# the labels "firefighter", "rescuer", "rescue" appear together in | ||
# "Firefighter/Rescuer III", or "Fire/Rescue Lieutenant". | ||
# | ||
# We can now understand the encoding of different samples. | ||
|
||
import matplotlib.pyplot as plt | ||
|
||
encoded_labels = enc.transform(X_dirty[:20]) | ||
plt.figure(figsize=(8, 10)) | ||
plt.imshow(encoded_labels) | ||
plt.xlabel("Latent topics", size=12) | ||
plt.xticks(range(0, 10), labels=topic_labels, rotation=50, ha="right") | ||
plt.ylabel("Data entries", size=12) | ||
plt.yticks(range(0, 20), labels=X_dirty[:20].to_numpy().flatten()) | ||
plt.colorbar().set_label(label="Topic activations", size=12) | ||
plt.tight_layout() | ||
plt.show() | ||
|
||
############################################################################### | ||
# As we can see, each dirty category encodes on a small number of topics, | ||
# These can thus be reliably used to summarize each topic, which are in | ||
# effect latent categories captured from the data. | ||
# | ||
# Conclusion | ||
# ---------- | ||
# | ||
# In this notebook, we have seen how to interpret the output of the | ||
# |GapEncoder|, and how it can be used to summarize categorical variables | ||
# as a set of latent topics. |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This part (and the subsequent visualization) is great! A real wow effect 👍