"AttributeError: 'float' object has no attribute 'reshape'."

"AttributeError: 'float' object has no attribute 'reshape'."

# similarity matrix

sim_mat = np.zeros([len(cleaned_texts), len(cleaned_texts)])
for i in range(len(sentences)):
    for j in range(len(sentences)):
        if i != j:
            sim_mat[i][j] = cosine_similarity(sentence_vectors[i].reshape(1, dim),
sentence_vectors[j].reshape(1, dim))[0, 0]
sim_mat = np.round(sim_mat, 3)
# print(sim_mat)

这一行 sentence_vectors[j].reshape(1, dim))[0, 0] 显示错误:

AttributeError: 'float' object has no attribute 'reshape'

在不知道 'sentence_vectors'、'cleaned_text' 和 'sentences' 是什么的情况下几乎不可能明确回答。

不过我可以解释错误消息。似乎您期望 sentence_vectors 是一个 numpy 数组列表,但错误告诉您 sentence_vectors[j] 是一个浮点数,而不是一个 numpy 数组。您可能应该确保正确生成列表。 :)