Skip to content

bogliosimone/similaripy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

75 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

similaripy

SimilariPy

PyPI version Docs Build and Test Publish to PyPI Docs Status Python versions License DOI

High-performance KNN similarity functions in Python, optimized for sparse matrices.

SimilariPy is primarily designed for Recommender Systems and Information Retrieval (IR) tasks, but can be applied to other domains as well.

The package also includes a set of normalization functions useful for pre-processing data before the similarity computation.

The official documentations is available at ๐Ÿ“˜ SimilariPy Guide

๐Ÿ” Similarity Functions

SimilariPy provides a range of high-performance similarity functions for sparse matrices.
All functions are multi-threaded and implemented in Cython + OpenMP for fast parallel computation on CSR matrixes.

Core

  • Dot Product โ€“ Simple raw inner product between vectors.
  • Cosine โ€“ Normalized dot product based on L2 norm.
  • Asymmetric Cosine โ€“ Skewed cosine similarity using an alpha parameter.
  • Jaccard, Dice, Tversky โ€“ Set-based generalized similarities.

Graph-Based

  • P3ฮฑ โ€“ Graph-based similarity computed through random walk propagation with exponentiation.
  • RP3ฮฒ โ€“ Similar to P3ฮฑ but includes popularity penalization using a beta parameter.

Advanced

  • S-Plus โ€“ A hybrid model combining Tversky and Cosine components, with full control over weights and smoothing.

For mathematical definitions and parameter details, see the ๐Ÿ“˜ SimilariPy Guide.

๐Ÿงฎ Normalization Functions

SimilariPy provides a suite of normalization functions for sparse matrix pre-processing.
All functions are implemented in Cython and can operate in-place on CSR matrixes for maximum performance and memory efficiency.

  • L1, L2 โ€“ Applies row- or column-wise normalization.
  • TF-IDF โ€“ Computes TF-IDF weighting with customizable term-frequency and IDF modes.
  • BM25 โ€“ Applies classic BM25 weighting used in information retrieval.
  • BM25+ โ€“ Variant of BM25 with additive smoothing for low-frequency terms.

For more details, check the ๐Ÿ“˜ SimilariPy Guide.

โšก Performance

SimilariPy is built for speed on large sparse matrices:

  • Cython + C++ + OpenMP โ€” multi-threaded, GIL-free computation compiled to native code
  • Cache-optimized โ€” column-blocked accumulation with popularity reordering minimizes L2 cache misses
  • Memory-efficient โ€” float32 precision, pre-allocated buffers, top-K heap filtering

๐Ÿš€ Getting Started

Hereโ€™s a minimal example to get you up and running with SimilariPy:

import similaripy as sim
import scipy.sparse as sps

# Create a random User-Rating Matrix (URM)
urm = sps.random_array((1000, 2000), density=0.025)

# Normalize the URM using BM25
urm = sim.normalization.bm25(urm)

# Train an item-item cosine similarity model
similarity_matrix = sim.cosine(urm.T, k=50)

# Compute recommendations for user 1, 14, 8 
# filtering out already-seen items
recommendations = sim.dot_product(
    urm,
    similarity_matrix.T,
    k=100,
    target_rows=[1, 14, 8],
    filter_cols=urm
)

For a more in-depth, end-to-end example with SimilariPy, see the notebook: MovieLens 32M Item-Item Recommender.

๐Ÿ“ฆ Installation

SimilariPy can be installed from PyPI with:

pip install similaripy

๐Ÿ”ง GCC Compiler - Required

To install the package and compile the Cython code, a GCC-compatible compiler with OpenMP is required.

Ubuntu / Debian

Install the official dev-tools:

sudo apt update && sudo apt install build-essential

MacOS (Intel & Apple Silicon)

Install GCC with homebrew:

brew install gcc

Windows

Install the official Visual C++ Build Tools.

โš ๏ธ On Windows, use the default format_output='coo' in all similarity functions, as 'csr' is currently not supported.

Optional Optimization: Intel MKL for Intel CPUs

For Intel CPUs, using SciPy/Numpy with MKL (Math Kernel Library) is highly recommended for best performance. The easiest way to achieve this is to install them via Anaconda.

๐Ÿ“ฆ Requirements

Package Version
numpy >= 1.22.4
scipy >= 1.12.0

๐Ÿ“œ History

This library originated during the Spotify Recsys Challenge 2018.

Our team, The Creamy Fireflies, faced major challenges computing large similarity models on a dataset with over 66 million interactions. Standard Python/Numpy solutions were too slow as a whole day was required to compute one single model.

To overcome this, I developed high-performance versions of the core similarity functions in Cython and OpenMP. Encouraged by my teammates, I open-sourced this work to help others solve similar challenges.

Thanks to my Creamy Fireflies friends for the support! ๐Ÿ™

๐Ÿ“„ License

This project is released under the MIT License.

๐Ÿ”– Citation

If you use SimilariPy in your research, please cite:

@misc{boglio_simone_similaripy,
  author       = {Boglio Simone},
  title        = {bogliosimone/similaripy},
  doi          = {10.5281/zenodo.2583851},
  url          = {https://doi.org/10.5281/zenodo.2583851}
}