def generateCosineNeighborGraphfromX(X, kNeighbors=10):
cosX = cosine_similarity(X)
# return sparse.csc_matrix(X.dot(X.transpose())),newIds
#print cosX.shape
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
#print 'graph generation done in %f seconds.' % toc
return sparse.csc_matrix(graph)
评论列表
文章目录