Skip to content
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

[MRG] Fix the match score scaling #802

Merged
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ Major changes
Minor changes
-------------

* Scaling of ``matching_score`` in :func:`fuzzy_join` is now between 0 and 1; it used to be between 0.5 and 1. Moreover, the division by 0 error that occurred when all rows had a perfect match has been fixed. :pr:`802` by :user:`Jérôme Dockès <jeromedockes>`.

* :class:`TableVectorizer` is now able to apply parallelism at the column level rather than the transformer level. This is the default for univariate transformers, like :class:`MinHashEncoder`, and :class:`GapEncoder`.
:pr:`592` by :user:`Leo Grinsztajn <LeoGrin>`

Expand Down
14 changes: 7 additions & 7 deletions examples/04_fuzzy_joining.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@
gdppc,
left_on="Country",
right_on="Country Name",
match_score=0.35,
match_score=0.1,
return_score=True,
)
df1.sort_values("matching_score").head(4)
Expand All @@ -194,7 +194,7 @@
gdppc,
left_on="Country",
right_on="Country Name",
match_score=0.35,
match_score=0.1,
drop_unmatched=True,
)

Expand Down Expand Up @@ -237,7 +237,7 @@
life_exp,
left_on="Country",
right_on="Country Name",
match_score=0.45,
match_score=0.1,
)

df2.drop(columns=["Country Name"], inplace=True)
Expand Down Expand Up @@ -273,7 +273,7 @@
legal_rights,
left_on="Country",
right_on="Country Name",
match_score=0.45,
match_score=0.1,
)

df3.drop(columns=["Country Name"], inplace=True)
Expand Down Expand Up @@ -397,9 +397,9 @@

# We will test four possible values of match_score:
params = {
"joiner-1__match_score": [0.2, 0.9],
"joiner-2__match_score": [0.2, 0.9],
"joiner-3__match_score": [0.2, 0.9],
"joiner-1__match_score": [0.1, 0.9],
"joiner-2__match_score": [0.1, 0.9],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't comment on it but you need to update the description regarding the best match_score parameter here and in other places of the example.
"The grid searching gave us the best value of 0.5 for the parameter"

"joiner-3__match_score": [0.1, 0.9],
}

grid = GridSearchCV(pipeline, param_grid=params)
Expand Down
9 changes: 5 additions & 4 deletions skrub/_fuzzy_join.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,10 @@ def _nearest_matches(
neigh.fit(aux_array)
distance, neighbors = neigh.kneighbors(main_array, return_distance=True)
idx_closest = np.ravel(neighbors)
distance = distance / np.max(distance)
# Normalizing distance between 0 and 1:
matching_score = 1 - (distance / 2)
max_dist = distance.max()
if max_dist != 0:
jeromedockes marked this conversation as resolved.
Show resolved Hide resolved
distance /= max_dist
matching_score = 1 - distance
return idx_closest, matching_score


Expand Down Expand Up @@ -349,7 +350,7 @@ def fuzzy_join(
a_x b a_y c matching_score
0 ana 1 ana 7 1.0
1 lala 2 lala 6 1.0
2 nana 3 <NA> <NA> 0.5
2 nana 3 <NA> <NA> 0.0

As expected, the category "nana" has no exact match (`match_score=1`).
"""
Expand Down
38 changes: 30 additions & 8 deletions skrub/tests/test_fuzzy_join.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import warnings
from typing import Literal

import numpy as np
import pandas as pd
import pytest
from numpy.testing import assert_array_equal
from pandas.testing import assert_frame_equal
from sklearn.feature_extraction.text import HashingVectorizer

Expand All @@ -26,7 +28,7 @@ def test_fuzzy_join(analyzer: Literal["char", "char_wb", "word"]) -> None:
right=df2,
left_on="a1",
right_on="a2",
match_score=0.45,
match_score=0.0,
return_score=True,
analyzer=analyzer,
)
Expand All @@ -41,7 +43,7 @@ def test_fuzzy_join(analyzer: Literal["char", "char_wb", "word"]) -> None:
how="left",
left_on="a2",
right_on="a1",
match_score=0.35,
match_score=0.0,
return_score=True,
analyzer=analyzer,
)
Expand All @@ -54,7 +56,7 @@ def test_fuzzy_join(analyzer: Literal["char", "char_wb", "word"]) -> None:
how="right",
right_on=["a2"],
left_on=["a1"],
match_score=0.35,
match_score=0.0,
return_score=True,
analyzer=analyzer,
)
Expand All @@ -80,6 +82,26 @@ def test_fuzzy_join(analyzer: Literal["char", "char_wb", "word"]) -> None:
assert ("a1l" and "a1r") in df.columns


def test_match_score():
left = pd.DataFrame({"A": ["aa", "bb"]})
right = pd.DataFrame({"A": ["aa", "ba"], "B": [1, 2]})
join = fuzzy_join(left, right, on="A", suffixes=("l", "r"))
assert join["B"].to_list() == [1, 2]
join = fuzzy_join(left, right, on="A", suffixes=("l", "r"), match_score=0.5)
assert join["B"].fillna(-1).to_list() == [1, -1]


def test_perfect_matches():
# non-regression test for https://github.com/skrub-data/skrub/issues/764
# fuzzy_join when all rows had a perfect match used to trigger a division by 0
df = pd.DataFrame({"A": [0, 1]})
with warnings.catch_warnings():
warnings.simplefilter("error")
warnings.filterwarnings("ignore", message="This feature is still experimental")
join = fuzzy_join(df, df, on="A", return_score=True)
assert_array_equal(join["matching_score"].to_numpy(), [1.0, 1.0])


def test_fuzzy_join_dtypes() -> None:
"""
Test that the dtypes of dataframes are maintained after join
Expand Down Expand Up @@ -144,16 +166,16 @@ def test_drop_unmatched() -> None:
a = pd.DataFrame({"col1": ["aaaa", "bbb", "ddd dd"], "col2": [1, 2, 3]})
b = pd.DataFrame({"col1": ["aaa_", "bbb_", "cc ccc"], "col3": [1, 2, 3]})

c1 = fuzzy_join(a, b, on="col1", match_score=0.6, drop_unmatched=True)
c1 = fuzzy_join(a, b, on="col1", match_score=0.1, drop_unmatched=True)
assert c1.shape == (2, 4)

c2 = fuzzy_join(a, b, on="col1", match_score=0.6)
c2 = fuzzy_join(a, b, on="col1", match_score=0.1)
assert sum(c2["col3"].isna()) > 0

c3 = fuzzy_join(a, b, on="col1", how="right", match_score=0.6)
c3 = fuzzy_join(a, b, on="col1", how="right", match_score=0.1)
assert sum(c3["col3"].isna()) > 0

c4 = fuzzy_join(a, b, on="col1", how="right", match_score=0.6, drop_unmatched=True)
c4 = fuzzy_join(a, b, on="col1", how="right", match_score=0.1, drop_unmatched=True)
assert c4.shape == (2, 4)


Expand Down Expand Up @@ -301,7 +323,7 @@ def test_numerical_column() -> None:
left,
right,
on="int",
match_score=0.8,
match_score=0.4,
drop_unmatched=True,
)
assert fj_num3.shape == (2, n_cols)
Expand Down