def generateCosineNeighborGraph(hin,kNeighbors=10,tf_param={'word':True, 'entity':False, 'we_weight':1}):
X, newIds, entIds = GraphGenerator.getTFVectorX(hin,param=tf_param)
cosX = cosine_similarity(X)
#return sparse.csc_matrix(X.dot(X.transpose())),newIds
n = cosX.shape[0]
graph = np.zeros((n,n))
tic = time.time()
for i in range(n):
for j in np.argpartition(-cosX[i],kNeighbors)[:kNeighbors]:
if j == i:
continue
#graph[i, j] += cosX[i, j]
#graph[j, i] += cosX[i, j]
graph[i, j] += 1
graph[j, i] += 1
toc = time.time() - tic
return sparse.csc_matrix(graph), newIds
评论列表
文章目录