1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
| import numpy as np import random import heapq import time
def Embedding(C,line): string = [] for element in line: for i in range(element): string.append("1") for j in range(C-element): string.append("0") return string
def set_table(H,C,B,data): table = {} f = {} for i, line in enumerate(data): pi = [] var = ''.join(Embedding(C,line)) for j in H: pi.append(var[j]) if (''.join(pi)) in f: if f[''.join(pi)]<B: f[''.join(pi)] += 1 table[i] = ''.join(pi) else: f[''.join(pi)]=1 table[i] = ''.join(pi) return table
def set_H(Cn,k): H = [] for i in range(k): H.append(random.randint(0,Cn-1)) H.sort() return H
def search(Hlist,tblist,var): keys = [] for i,h in enumerate(Hlist): p = [] for hi in h: p.append(var[hi]) for key, value in tblist[i].items(): if value== ''.join(p): keys.append(key) return keys
def LSH(line, Hlist, tblist): array = line var = ''.join(Embedding(C, array)) keys = search(Hlist, tblist, var) keys.sort() set1 = set(keys) dict01 = {item: keys.count(item) for item in set1} sorted_x = sorted(dict01.items(), key=lambda x: x[1], reverse=True) keys = [] num = 0 for i,n in sorted_x: keys.append(i) num += 1 return keys
def distance(line1,line2): dist = np.linalg.norm(line1-line2) return dist
def mindistancedata(linenum,data): dist = [] for i, line in enumerate(data): dist.append(distance(data[linenum],data[i])) min_index_list = map(dist.index, heapq.nsmallest(10, dist)) return list(min_index_list)
def mindistance(linenum,data,linelist): min_index_list = [] dist = {} for i in linelist: dist[i] = distance(data[linenum], data[i]) L = sorted(dist.items(), key=lambda item: item[1]) for i in range(10): min_index_list.append(L[i][0]) return min_index_list
bit = 2 odata = data = np.loadtxt('ColorHistogram.asc', usecols = range(1, 33), unpack= False) data = np.loadtxt('ColorHistogram.asc',usecols = range(1, 33), unpack= False) data = data*(10**bit) data = data.astype(np.int)
k = input("input K:") k = int(k)
L = input("input L:") L = int(L)
B = input("input B:") B = int(B)
C = int(np.max(data)+1) n = data.shape[1] hamming_code = [] Cn = C*n Hlist=[] tblist = []
for i in range(L): H = set_H(Cn,k) Hlist.append(H) tblist.append(set_table(H,C,B,data)) print("创建索引哈希表: "+str(i))
while(True): true = [] my = [] linenum = 1000 for i in range(linenum): trueindex = [] trueindex = mindistancedata(i, odata) true.append(trueindex) time_start = time.time() tinm = 0 mint = 0 for i in range(linenum): myindex = [] line = data[i] keys = LSH(line, Hlist, tblist) myindex = mindistance(i,odata,keys) for ni in range(10): if true[i][ni] in myindex: tinm += 1 time_end = time.time() print("K=" + str(k) +",L=" + str(L) + "B=" + str(B)) print("召回率"+str(tinm/linenum/10)) print("准确率"+str(1-(linenum*10-tinm)*2/linenum/68040)) print('time cost', time_end - time_start, 's')
|