lace.Engine.pairwise_fn

Engine.pairwise_fn(fn_name, indices: list | None = None, **kwargs)

Compute a function for a set of pairs of rows or columns.

Parameters:
  • fn_name (str) – The name of the function: ‘rowsim’, ‘mi’, or ‘depprob’

  • indices (List[index], optional) – An optional list of indices from which to generate pairs. The output will be the function computed over the Cartesian product of indices. If None (default), all indices will be considered.

  • function (All other keyword arguments will be passed to the target) –

Examples

Column weighted row similarity with indices defined

>>> from lace.examples import Animals
>>> engine = Animals()
>>> engine.pairwise_fn(
...     "rowsim",
...     indices=["wolf", "rat", "otter"],
... )
shape: (9, 3)
┌───────┬───────┬──────────┐
│ A     ┆ B     ┆ rowsim   │
│ ---   ┆ ---   ┆ ---      │
│ str   ┆ str   ┆ f64      │
╞═══════╪═══════╪══════════╡
│ wolf  ┆ wolf  ┆ 1.0      │
│ wolf  ┆ rat   ┆ 0.71689  │
│ wolf  ┆ otter ┆ 0.492262 │
│ rat   ┆ wolf  ┆ 0.71689  │
│ rat   ┆ rat   ┆ 1.0      │
│ rat   ┆ otter ┆ 0.613095 │
│ otter ┆ wolf  ┆ 0.492262 │
│ otter ┆ rat   ┆ 0.613095 │
│ otter ┆ otter ┆ 1.0      │
└───────┴───────┴──────────┘

Extra keyword arguments are passed to the parent function.

>>> engine.pairwise_fn(
...     "rowsim",
...     indices=["wolf", "rat", "otter"],
...     col_weighted=True,
... )
shape: (9, 3)
┌───────┬───────┬──────────┐
│ A     ┆ B     ┆ rowsim   │
│ ---   ┆ ---   ┆ ---      │
│ str   ┆ str   ┆ f64      │
╞═══════╪═══════╪══════════╡
│ wolf  ┆ wolf  ┆ 1.0      │
│ wolf  ┆ rat   ┆ 0.642647 │
│ wolf  ┆ otter ┆ 0.302206 │
│ rat   ┆ wolf  ┆ 0.642647 │
│ rat   ┆ rat   ┆ 1.0      │
│ rat   ┆ otter ┆ 0.491176 │
│ otter ┆ wolf  ┆ 0.302206 │
│ otter ┆ rat   ┆ 0.491176 │
│ otter ┆ otter ┆ 1.0      │
└───────┴───────┴──────────┘

If you do not provide indices, the function is computed for the product of all indices.

>>> engine.pairwise_fn("rowsim")
shape: (2_500, 3)
┌──────────┬──────────────┬──────────┐
│ A        ┆ B            ┆ rowsim   │
│ ---      ┆ ---          ┆ ---      │
│ str      ┆ str          ┆ f64      │
╞══════════╪══════════════╪══════════╡
│ antelope ┆ antelope     ┆ 1.0      │
│ antelope ┆ grizzly+bear ┆ 0.464137 │
│ antelope ┆ killer+whale ┆ 0.479613 │
│ antelope ┆ beaver       ┆ 0.438467 │
│ …        ┆ …            ┆ …        │
│ dolphin  ┆ walrus       ┆ 0.724702 │
│ dolphin  ┆ raccoon      ┆ 0.340923 │
│ dolphin  ┆ cow          ┆ 0.482887 │
│ dolphin  ┆ dolphin      ┆ 1.0      │
└──────────┴──────────────┴──────────┘