<s> import numpy <EOL> import scipy <EOL> import unittest <EOL> from nearpy import Engine <EOL> from nearpy . distances import CosineDistance <EOL> from nearpy . hashes import RandomBinaryProjections , RandomBinaryProjectionTree , HashPermutations , HashPermutationMapper <EOL> def example1 ( ) : <EOL> DIM = <NUM_LIT:100> <EOL> POINTS = <NUM_LIT> <EOL> print '<STR_LIT>' <EOL> rbpt = RandomBinaryProjectionTree ( '<STR_LIT>' , <NUM_LIT:20> , <NUM_LIT:20> ) <EOL> engine_rbpt = Engine ( DIM , lshashes = [ rbpt ] , distance = CosineDistance ( ) ) <EOL> rbp = RandomBinaryProjections ( '<STR_LIT>' , <NUM_LIT:20> ) <EOL> engine = Engine ( DIM , lshashes = [ rbp ] , distance = CosineDistance ( ) ) <EOL> permutations = HashPermutations ( '<STR_LIT>' ) <EOL> rbp_perm = RandomBinaryProjections ( '<STR_LIT>' , <NUM_LIT:20> ) <EOL> rbp_conf = { '<STR_LIT>' : <NUM_LIT:50> , '<STR_LIT>' : <NUM_LIT:10> , '<STR_LIT>' : <NUM_LIT:100> } <EOL> permutations . add_child_hash ( rbp_perm , rbp_conf ) <EOL> engine_perm = Engine ( DIM , lshashes = [ permutations ] , distance = CosineDistance ( ) ) <EOL> permutations2 = HashPermutationMapper ( '<STR_LIT>' ) <EOL> rbp_perm2 = RandomBinaryProjections ( '<STR_LIT>' , <NUM_LIT:12> ) <EOL> permutations2 . add_child_hash ( rbp_perm2 ) <EOL> engine_perm2 = Engine ( DIM , lshashes = [ permutations2 ] , distance = CosineDistance ( ) ) <EOL> print '<STR_LIT>' % ( POINTS , DIM ) <EOL> matrix = numpy . zeros ( ( POINTS , DIM ) ) <EOL> for i in xrange ( POINTS ) : <EOL> v = numpy . random . randn ( DIM ) <EOL> matrix [ i ] = v <EOL> engine . store_vector ( v ) <EOL> engine_rbpt . store_vector ( v ) <EOL> engine_perm . store_vector ( v ) <EOL> engine_perm2 . store_vector ( v ) <EOL> print '<STR_LIT>' % len ( engine . storage . buckets [ '<STR_LIT>' ] . keys ( ) ) <EOL> print '<STR_LIT>' % len ( engine_rbpt . storage . buckets [ '<STR_LIT>' ] . keys ( ) ) <EOL> print '<STR_LIT>' <EOL> permutations . build_permuted_index ( ) <EOL> print '<STR_LIT>' <EOL> query = numpy . random . randn ( DIM ) <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' % engine_rbpt . candidate_count ( query ) <EOL> results = engine_rbpt . neighbours ( query ) <EOL> dists = [ x [ <NUM_LIT:2> ] for x in results ] <EOL> print dists <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' % engine . candidate_count ( query ) <EOL> results = engine . neighbours ( query ) <EOL> dists = [ x [ <NUM_LIT:2> ] for x in results ] <EOL> print dists <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' % engine_perm . candidate_count ( query ) <EOL> results = engine_perm . neighbours ( query ) <EOL> dists = [ x [ <NUM_LIT:2> ] for x in results ] <EOL> print dists <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' % engine_perm2 . candidate_count ( query ) <EOL> results = engine_perm2 . neighbours ( query ) <EOL> dists = [ x [ <NUM_LIT:2> ] for x in results ] <EOL> print dists <EOL> print '<STR_LIT>' <EOL> query = query . reshape ( ( <NUM_LIT:1> , DIM ) ) <EOL> dists = CosineDistance ( ) . distance ( matrix , query ) <EOL> dists = dists . reshape ( ( - <NUM_LIT:1> , ) ) <EOL> dists = sorted ( dists ) <EOL> print dists [ : <NUM_LIT:10> ] </s>
