1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- import sys
- import pandas as pd
- import numpy as np
- import faiss
- def gen_i2i(index_item, embeddings,i2i):
- fw=open(i2i,"w")
-
- embed_matrix=np.array(embeddings).astype('float32')
-
- index=faiss.IndexFlatL2(100)
- index.add(embed_matrix)
-
-
-
- distence_matrix,recall_list=index.search(embed_matrix, 20)
- for idx,rec_arr in enumerate(recall_list):
-
- orgin_item=str(index_item[idx])
- recall_str=""
-
- for re_id in rec_arr[1:]:
- recall_idstr=str(index_item[re_id])
-
- recall_str=recall_str+","+recall_idstr
- fw.write(orgin_item+"\t"+recall_str[1:]+"\n")
- if __name__ == '__main__':
- f = open(sys.argv[1])
- index = 0
- index_dict = {}
- index_arr = []
- while True:
- line = f.readline()
- if not line:
- break
- line = line.strip()
-
- items = line.split(" ")
-
- try:
- vid = int(items[0])
- print(line)
-
-
- except:
-
- continue
- f.close()
-
-
|