Jak*_*ke 2 python nlp scipy sparse-matrix
我想针对大型语料库运行 textrank(仅我的开发环境使用 17K 句子)
因此我使用了 scipy dok_matrix。然而,当将第一个值分配给我的稀疏矩阵(即similarity_matrix[1][0])时,尽管在 pycharm 调试中看到我的 dok_matrix 的大小为 17K x 17k,但我收到以下错误。
IndexError: row index (1) out of range
我做错了什么?
def _score_generator(self, sentences, sentence_vectors):
sentence_count = len(sentences)
similarity_matrix = dok_matrix((sentence_count, sentence_count), dtype=np.float32)
for i in range(len(sentences)):
for j in range(len(sentences)):
if i != j:
similarity_matrix[i][j] = cosine_similarity(sentence_vectors[i].reshape(1, 100), sentence_vectors[j].reshape(1, 100))[0, 0]
nx_graph = nx.from_scipy_sparse_matrix(similarity_matrix)
scores = nx.pagerank(nx_graph)
return scores
Run Code Online (Sandbox Code Playgroud)