A Python library of set similarity search algorithms
Project description
Set Similarity Search
What is set similarity search?
Let's say we have a database of users and books they have read. Assume that we want to recommend "friends" for each user, and the "friends" must have read very similar set of books as the user have. We can model this as a set similarity search problem, by representing each user's books as a set:
Alice: {"Anna Karenina", "War and Peace", "The Chameleon", ...}
Bob: {"Lolita", "The Metamorphosis", "The Judgement", ...}
Joey: {"Anna Karenina", "The Chameleon" ...}
A popular way to measure the similarity between two sets is Jaccard similarity, which gives a fractional score between 0 and 1.0.
The set similarity search problem is given a collection of sets, a similarity function and a threshold, find all pairs of sets that have similarities greater than (or equal to) the threshold. This can be very computationally expensive as 1) the number of sets is large and 2) the set sizes are large. The simple brute-force algorithm is O(n^2).
This package includes a Python implementation of the "All-Pair-Binary" algorithm in Scaling Up All Pairs Similarity Search paper, with additional position filter optimization. This algorithm still runs in O(n^2) in the worst case, however, by taking advantage of skewness in empirical distributions of frequency, it often runs much faster.
Install
pip install -U SetSimilaritySearch
Library usage
You can import this package in your own Python code:
from SetSimilaritySearch import all_pairs
# The input sets must be a Python list of iterables (i.e., lists or sets)
sets = [[1,2,3], [3,4,5], [2,3,4], [5,6,7]]
# all_pairs returns an iterable of tuples.
pairs = all_pairs(sets, similarity_func_name="jaccard",
similarity_threshold=0.1)
list(pairs)
# [(1, 0, 0.2), (2, 0, 0.5), (2, 1, 0.5), (3, 1, 0.2)]
# Each tuple is (<index of the first set>, <index of the second set>, <similarity>).
# The indexes are the list indexes of the input sets.
Supported similarity functions (more to come):
- Jaccard: intersection size divided by union size; set
similarity_func_name="jaccard"
. - Cosine: intersection size divided by square root of the product of sizes; set
similarity_func_name="cosine"
.
Command line usage
You can also use the command line program all_pairs.py
on a file.
The input must be a file with each line a unique SetID Token
tuple.
For example:
# Line starts with # will be ignored.
# Each line is <Set ID> <Token (i.e. Set Element)>, separate by a whitespace or tab.
# Every line must be unique.
1 a
1 b
1 c
1 d
2 a
2 b
2 c
3 d
3 e
Example usage:
all_pairs.py --input-sets testdata/example_input.txt \
--output-pairs testdata/example_output.txt \
--similarity-func jaccard \
--similarity-threshold 0.5
Benchmarks
Run on 3.5 GHz Intel Core i7, using similarity function jaccard
and
similarity threshold 0.5.
Dataset | Input Sets | Output Pairs | Runtime | Note |
---|---|---|---|---|
Pokec social network (relationships) | 1432693 | 355215 | 10m49s | Each from-node is a set; each to-node is a token |
LiveJournal | 4308452 | 5545706 | 28m51s | Each from node is a set; each to-node is a token |
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for SetSimilaritySearch-0.1.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8e9b8171b1ed4c7a54f83e99d6174e477fca78955aff1f90bf4d78684be5477a |
|
MD5 | 0642f776c65c8aed94897ad7e163685d |
|
BLAKE2b-256 | ebf300eba67c6201a6fcb24959b2978ee257ec7179d9d8ef0695081d56e08322 |
Hashes for SetSimilaritySearch-0.1.1-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9b6d7e7b03c791a471d917848350b615d318a7fe75dba3539949d540426f65cb |
|
MD5 | 383737ce1d2caf36472dda42f0b284c7 |
|
BLAKE2b-256 | 5905e18b4b64033b58759e878cda40b035f04fde7a9d578ba36b49d251098a7e |