overlap¶
champalimaud.overlap
¶
Overlap between sets, as the Jaccard index.
The Jaccard index of two sets is the size of their intersection over the size of their union: 1 for the same set and 0 for disjoint sets.
jaccard(a, b)
¶
Jaccard index of two sets, 0 for two empty sets.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
set
|
The sets to compare. |
required |
b
|
set
|
The sets to compare. |
required |
Returns:
| Type | Description |
|---|---|
float
|
|
Examples:
Source code in champalimaud/overlap.py
pairwise_jaccard(sets)
¶
The Jaccard index of every pair of the sets.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sets
|
dict of str to set
|
The sets, by name. |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
One row per pair of names, with columns |
See Also
jaccard : The index of one pair. sets_by_type : Builds the sets from a table.
Examples:
>>> table = pairwise_jaccard({"P": {1, 2}, "Q": {2, 3}})
>>> table.select("a", "b", "shared", "union").rows()
[('P', 'Q', 1, 3)]
>>> round(table["jaccard"].item(), 3)
0.333
Source code in champalimaud/overlap.py
sets_by_type(strong, column, types)
¶
The values of a column among the rows of each type, as sets.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
strong
|
DataFrame
|
Has a column |
required |
column
|
str
|
Column whose values are collected. |
required |
types
|
sequence of str
|
The types to collect, in order. |
required |
Returns:
| Type | Description |
|---|---|
dict of str to set
|
One set per type; a type with no row has an empty set. |
See Also
pairwise_jaccard : The overlap of these sets.
Examples:
>>> import polars as pl
>>> strong = pl.DataFrame(
... {"type": ["P", "P", "Q"], "partner": [1, 2, 2]}
... )
>>> sets_by_type(strong, "partner", ["P", "Q", "R"])
{'P': {1, 2}, 'Q': {2}, 'R': set()}