Applying Dedupers
In the First Steps you found out how to instantiate Liken, and how to profile near duplicate rates in your data. Now, you can apply duplicates for execution, by using the apply function:
There are several ways to apply dedupers in Liken. Each offers incremental value, at the cost of complexity. For a quick reference use the following table as a guide to this tutorial:
Different collections of dedupers, whether a single deduper, a dictionary or a pipeline, are best suited to different use cases:
| Quick tasks | Multiple columns | Logical combination semantics | Preprocessors | |
|---|---|---|---|---|
| Single Deduper | ||||
| Dict of dedupers | ||||
| Pipelines of dedupers |
Single Dedupers
If you only need a single deduper, use it straight in an apply function as seen above, i.e. apply(lk.fuzzy()), or any other deduper. The column or columns to deduplicate are passed in drop_duplicates:
For a deduper that accepts multiple columns, and keeping only the last instance (not the default first instance):
import liken as lk
df = (
lk.dedupe(df)
.apply(lk.cosine())
.drop_duplicates(("height", "width"), keep = "last")
)
keep argument
The keep argument accepts the literals "first" or "last" which picks the representative of each duplicate cluster.
Dictionaries of Dedupers
Dictionaries of dedupers have three properties:
- Keys are column sets. A key is a column label (
"email") or a tuple of column labels (("first_name", "last_name")). - Values are a deduper or a sequence of dedupers. A value is one deduper, or a tuple of dedupers chained on that key's columns: a record counts as a duplicate if it is linked by any deduper in the tuple.
- Keys combine as OR. Records linked by any key's deduper end up in the same cluster.
import liken as lk
collection = {
"email": lk.exact(),
"address": (
lk.fuzzy(threshold=0.98),
lk.tfidf(threshold=0.9, ngram=(1, 2), topn=1),
),
}
df = (
lk.dedupe(df)
.apply(collection)
.drop_duplicates(keep="first")
)
In the above: deduplicate records that share an exact email; and separately, deduplicate records whose addresses are fuzzy-similar at 0.98, then token-similar at 0.9. Records caught by either deduper end up in one cluster.
Keys run in insertion order, and the dedupers in a tuple value run in their listed order.
Pipelines of Dedupers
Liken exposes a pipeline builder function for you to build complex, composable pipelines.
At a minimum, pipelines can replicate a dictionary collection. Compare the following two which produce identical outputs:
A pipeline has the following features:
- Each
stepin a pipeline represents a deduplication step. - Column access is provided by
lk.colexpression. - Dedupers are provided as method calls to the
lk.colexpression.
AND semantics
Pipelines support combining the effects of multiple dedupers using implicit and statements.
AND semantics are supported in Liken when lists of dedupers are passed to a step in a pipeline. For a step of threshold dedupers, a pair of records is linked only when every deduper in the step matches it. For a step mixing predicate and threshold dedupers, the step runs as a chain: the predicates match first, and each later deduper sees only the records matched so far — so records are linked only when every condition holds on the way through:
import liken as lk
pipeline = (
lk.pipeline()
.step(
[
lk.col("address").fuzzy(threshold=0.8),
lk.col("address").str_startswith("10"),
] # AND: both conditions must hold
)
.step(lk.col("email").fuzzy(threshold=0.98))
)
df = (
lk.dedupe(df)
.apply(pipeline)
.drop_duplicates()
)
In the above case, for the first step, both conditions must hold — similar addresses will only be deduplicated if they also start with "10". See the difference on a small dataset:
| id | address |
|---|---|
| 0 | 10 high st |
| 1 | 10 high street |
| 2 | 99 high st |
| 3 | 99 high street |
Four addresses. The "10" pair and the "99" pair are both fuzzy-similar above 0.8.
With fuzzy(0.8) alone, all four records merge into one. With the AND step, the predicate rules out the "99" pair:
| id | address |
|---|---|
| 0 | 10 high st |
| 2 | 99 high st |
| 3 | 99 high street |
AND step output: only rows 0 and 1 were linked; the "99" records survive as two rows.
AND steps are most effective when one of the dedupers is a predicate deduper. Liken optimises such steps with predicate evaluation: the predicate dedupers run first, and the remaining dedupers only see the records the predicates matched. Liken reorders the step for you — the step is stably sorted so predicates run first — so you do not need to order dedupers yourself. A record counts as matched by a predicate only when the predicate actually links it to another record, and predicates see only the records the earlier predicates kept, so in a step with several predicates the order can matter. The optimisation matters because predicate dedupers run in about O(n) time while threshold dedupers compare every pair of records at about O(n²): restricting the threshold deduper to the predicate-matched subset skips the quadratic cost over records that could never match.
OR semantics
OR semantics are captured by distinct steps in a pipeline.
OR semantics are actually implicitly supported when using dictionaries and are best understood in comparison with AND semantics:
OR in dictionaries
OR semantics are achieved with dictionaries. If you are just using OR semantics in a pipeline, consider sticking to defining collections of dedupers as dictionaries, which are simpler to use.
NOT semantics
Predicate dedupers can be inverted to form a NOT semantic by using the ~ operator on the column accessor lk.col expression:
import liken as lk
pipeline = (
lk.pipeline()
.step(
[
lk.col("address").fuzzy(),
~lk.col("address").isna(), # NOT missing
]
)
)
A negated predicate links the records that do not satisfy it. Watch it isolate missing values on a small dataset:
| id | address |
|---|---|
| 0 | london |
| 1 | london |
| 2 | london |
| 3 | None |
| 4 | None |
| 5 | tokyo |
Six addresses: three identical, two missing, one distinct.
The step ~lk.col("address").isna() links every record whose address is not missing — rows 0, 1, 2 and 5 — so the three london records merge and tokyo stays apart:
| id | address |
|---|---|
| 0 | london |
| 3 | None |
| 4 | None |
~isna output: the london records merged; the missing records were never in the cluster.
The un-negated lk.col("address").isna() is the mirror image: the two missing records merge into one, and every other record survives untouched.
Preprocessors
Pipelines support the addition of a powerful feature: preprocessors. Liken's preprocessors transform data solely within the internals of the library for the purposes of deduplication whilst still returning data to you in the original format. Preprocessors never modify the data you get back — they run inside matching only, and your returned DataFrame keeps its original values.
Preprocessors can be used to refine deduplication pipelines, reduce boilerplate preprocessing code, reduce the number of "dummy" columns that you have to maintain, and reduces the risk of unacceptable false positive rates.
Preprocessors are available in the liken.preprocessors module and can be made available to the overall pipeline scope, a step in the pipeline, or on column only.
A single preprocessor can be passed, or multiple, if passed as a list:
import liken as lk
pipeline = (
lk.pipeline(
preprocessors=[
lk.preprocessors.lower(),
lk.preprocessors.ascii_fold(),
lk.preprocessors.remove_punctuation(),
]
)
.step(lk.col("address").tfidf())
)
Preprocessors are propagated in a top-down manner, but overridden bottom-up. So, a pipeline level preprocessor will propagate to each step and column accessor on, but will be respectively overridden if preprocessors are defined there:
pipeline = (
lk.pipeline(preprocessors=[lk.preprocessors.ascii_fold()])
.step(
[
lk.col("email").fuzzy(), # preprocessed by step's preprocessor, `alnum`.
~lk.col(
"address",
preprocessors=[lk.preprocessors.lower()],
).isna(), # uses its own preprocessor, `lower`.
],
preprocessors=[lk.preprocessors.alnum()], # defines the step's preprocessor
)
.step(
lk.col("address").tfidf()
) # defaults to the pipeline's preprocessor, `ascii_fold`.
)
Wait... What Did I Apply?
You can see what is applied using the explain function.
Single deduper application:
import liken as lk
import pandas as pd
df = pd.DataFrame([{"id": 1, "text": "apple", "cat_1": "sweden", "cat_2": "reddit"}]) # Dummy data
dedupers = lk.dedupe(df).apply(lk.fuzzy(threshold=0.6))
print("df will be deduped with...")
print(dedupers.explain())
Info
.explain() always shows any default arguments as well.
For dictionaries of dedupers:
import liken as lk
import pandas as pd
df = pd.DataFrame([{"id": 1, "text": "apple", "height": 0.1, "width": 0.2 }]) # Dummy data
dedupers = lk.dedupe(df).apply(
{
"text": lk.fuzzy(threshold=0.6),
("height", "width"): lk.cosine(),
}
)
print("df will be deduped with...")
print(dedupers.explain())
df will be deduped with...
{
'text': (
fuzzy(threshold=0.6, scorer='simple_ratio'),
),
'('height', 'width')': (
cosine(threshold=0.95),
),
}
For pipelines of dedupers:
import liken as lk
import pandas as pd
df = pd.DataFrame([{"id": 1, "email": "x@y.io", "address": "10th street"}]) # Dummy data
pipeline = (
lk.pipeline()
.step(
[
lk.col("address").fuzzy(threshold=0.8),
lk.col("address").str_startswith("10"),
]
)
.step(lk.col("email").fuzzy(threshold=0.98))
)
deduper = lk.dedupe(df).apply(pipeline)
print("df will be deduped with...")
print(dedupers.explain())
df will be deduped with...
{
'text': (
fuzzy(threshold=0.6, scorer='simple_ratio'),
),
'('height', 'width')': (
cosine(threshold=0.95),
),
}
Warning
Preprocessors are not currently shown in the explained output of a pipeline.