scipy 稀疏矩阵:索引超出范围
scipy sparse matrix: Index out of range
我想 运行 针对大型语料库进行文本排名(仅我的开发环境使用了 17K 个句子)
因此我使用了scipydok_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
简单:
similarity_matrix[i,j]
我会把它留在这里,以防其他人最终犯下和我一样的认知错误
我想 运行 针对大型语料库进行文本排名(仅我的开发环境使用了 17K 个句子)
因此我使用了scipydok_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
简单:
similarity_matrix[i,j]
我会把它留在这里,以防其他人最终犯下和我一样的认知错误