This .zip file contains the Python scripts to find users with a > 0.5 Jaccard similarity. To do so, it employs a Locality Sensitive Hashing (LSH) algorithm.
Contains the main script to run the algorithm
Contains the LSH class and helper functions
Methods:
generate_hash_functions(): Generates N hash functions to compute MinHash signaturesminhashing(): Computes the MinHash signatures matrixband_signatures(): Computes bands for the signatures and saves the hash values of each band for later comparisonfind_candidate_pairs(): Creates buckets using the banded data and finds users with identical values in a bandcompute_pseudo_jaccard(): Computes the pseudo-Jaccard similarity scores using the MinHash signatures for the candidate pairscompute_real_jaccard(): Computes the true Jaccard similarity score for the pairs that exceed 0.5 in pseudo-Jaccard. Returns the true positive rate
Returns:
data_: The CSR matrix containing the ratings datausers_: The number of usersmovies_: The number of moviesthresh_: The threshold for writing pairs to the results textfilehash_functions_: The generated hash functionsnum_functions_: The number of hash functions generatedsignatures_: The MinHash signaturesnum_bands_: The number of bands for the bandingbanded_data_: The banded MinHash signaturescandidate_pairs_: The candidate pairs found after banding. Candidate pairs have at least one band value that is the samesimilarities_: The pseudo-Jaccard similarities that exceed 0.5pairs_: The pairs whose pseudo-Jaccard similarity exceeds 0.5true_similarities_(optional): The true Jaccard similarities for the pairs with pseudo-Jaccard above 0.5true_pairs_(optional): The pairs with a true Jaccard similarities above 0.5true_positive_rate_(optional): The true positive rate of having both pseudo- and true Jaccard above 0.5
-
find_next_prime(int): Takes a starting number and finds the next prime number -
write_pairs_to_file(np.ndarray): Takes a 2-dimensional array of user pairs and writes them to a textfile in format$u1,u2$
Sets the default values for LSH parameters and checks for exitence of path to data file.
Run the main.py script from the terminal. Default parameters are pre-set; however, it is possible to set custom parameters from the command line. Options are below.
| Argument | Description | Default value |
|---|---|---|
-s, --seed |
Sets the random seed for the generation of hash functions. | 32 |
-b, --bands |
Sets the number of bands for the banding of the data. The number of rows per band is inferred. | 24 |
-f, --functions |
Sets the number of hash function to generate and use during MinHashing | 150 |
-u, --users |
Sets the limit to the amount of users per bucket. | 150 |
-j, -jaccard |
Decides whether to compute true Jaccard similarity and return true positive rate of Jaccard similarities > 0.5. If True, it writes the pairs with both true Jaccard and pseudo-Jaccard similarity above 0.5 |
1 (True) |