Similarity Features
- class neer_match_utilities.similarity_features.SimilarityFeatures(similarity_map)[source]
- __init__(similarity_map)
- pairs(left, right, matches, left_id_col, right_id_col, match_col='match', matches_id_left='left', matches_id_right='right', matches_are_indices=True)[source]
The rows of
pairwise_similarity_dataframe(), built on demand.Takes the same arguments. Nothing is computed per pair until rows are requested from the returned object, in full, in blocks of left rows, or selected by position.
- Return type:
- pairwise_similarity_dataframe(left, right, matches, left_id_col, right_id_col, match_col='match', matches_id_left='left', matches_id_right='right', matches_are_indices=True)[source]
Build full cross-join of left × right, compute similarity features specified in SimilarityMap, and attach match indicator.
- Parameters:
left (
DataFrame) – Left- and right-hand side entity tables.right (
DataFrame) – Left- and right-hand side entity tables.matches (
DataFrame) – DataFrame describing which pairs are true matches. If matches_are_indices=True, matches[matches_id_left] and matches[matches_id_right] are interpreted as row positions in left and right (0..n-1), as neer_match interprets them, whatever the frames’ index labels are. A position outside 0..n-1 raises ValueError. If False, they are interpreted as IDs in the same space as left[left_id_col] / right[right_id_col]. A pair listed more than once is counted once.left_id_col (
str) – Column names in left / right that contain the entity IDs.right_id_col (
str) – Column names in left / right that contain the entity IDs.match_col (
str) – Name of the binary match indicator column in the output.matches_id_left (
str) – Column names in matches identifying the left/right side.matches_id_right (
str) – Column names in matches identifying the left/right side.matches_are_indices (
bool) – If True (default), treat matches_id_left / matches_id_right as row positions in left and right. If False, treat them as IDs.
- Return type:
DataFrame
Notes
The frame holds one row per pair, so its memory grows with
len(left) * len(right).pairs()builds the same rows in blocks, or only selected ones.
- class neer_match_utilities.similarity_features.SimilarityPairs(similarity_map, left, right, matches, left_id_col, right_id_col, match_col, matches_id_left, matches_id_right, matches_are_indices)[source]
The rows of a pairwise similarity frame, built on demand.
Row
kpairs left rowk // len(right)with right rowk % len(right), as in the full cross join. Everything that does not depend on the pair – the ID lookups, each compared column’s values, the match keys – is derived once here, so building the rows in several calls costs no more than building them in one, and gives the same values.- __init__(similarity_map, left, right, matches, left_id_col, right_id_col, match_col, matches_id_left, matches_id_right, matches_are_indices)[source]
- chunks(pairs_per_chunk=250000)[source]
All rows, in consecutive blocks of whole left rows.
Each block holds as many left rows as fit in
pairs_per_chunkpairs, and at least one. Its index continues the full frame’s, so concatenated, the blocks equalframe().- Return type:
Iterator[DataFrame]
- frame()[source]
All rows, as
SimilarityFeatures.pairwise_similarity_dataframe()returns them.- Return type:
DataFrame
- neer_match_utilities.similarity_features.subsample_non_match_positions(match, mismatch_share=1.0, random_state=None, shuffle=True)[source]
Positions of the rows
subsample_non_matches()keeps, in its order.subsample_non_matches(df, ...)equalsdf.iloc[positions]forpositions = subsample_non_match_positions(df[match_col].to_numpy(), ...)and the same remaining arguments. Only the match column is needed, so the similarity features can be computed afterwards for the kept rows alone.Both draws are made as pandas’
DataFrame.samplemakes them: an integer seed starts a freshnumpy.random.RandomStatefor each, None uses numpy’s global one, andround(mismatch_share * n)non-matches are kept. Calling numpy directly keeps the result independent of pandas’ implementation; numpy holds theRandomStatestream fixed across versions (NEP 19).- Parameters:
match (
ndarray) – Binary match indicator per row (1 = match).mismatch_share (
float) – As forsubsample_non_matches().random_state (
int|None) – As forsubsample_non_matches().shuffle (
bool) – As forsubsample_non_matches().
- Returns:
Row positions: all matches, then the sampled non-matches, shuffled together if
shuffleis True.- Return type:
ndarray
- neer_match_utilities.similarity_features.subsample_non_matches(df, match_col='match', mismatch_share=1.0, random_state=None, shuffle=True)[source]
Return a subsample of df where all matches are kept and a fraction of non-matches is sampled.
- Parameters:
df (
DataFrame) – DataFrame with a binary match column.match_col (
str) – Name of the binary target column (1 = match, 0 = non-match).mismatch_share (
float) – Share of non-matches to keep. Must satisfy 0 < mismatch_share <= 1.0. - 1.0 → keep all non-matches - 0.1 → keep 10% of non-matchesrandom_state (
int|None) – Random seed for reproducible sampling.shuffle (
bool) – If True, shuffle the resulting DataFrame.
- Returns:
Subsampled DataFrame with all matches and a subset of non-matches.
- Return type:
DataFrame
- neer_match_utilities.similarity_features.to_X_y(df, match_col='match')[source]
Extract (X, y) from a pairwise similarity DataFrame.
- Parameters:
df (
DataFrame) – DataFrame produced by SimilarityFeatures.pairwise_similarity_dataframe().match_col (
str) – Name of the binary match indicator column.
- Returns:
X (pd.DataFrame) – Feature matrix containing all similarity columns (col_*).
y (np.ndarray) – Target array (0/1).