在3D空间中绘制PCA组件:句子嵌入

问题描述

我正尝试使用spacy比较句子,如下所示:

import spacy

nlp = spacy.load('en_core_web_lg')

search_doc = nlp("Dalí's artistic repertoire included painting,graphic arts,film,sculpture,design and photography,at times in collaboration with other artists.")
search_doc2=nlp("Ferrari is an Italian luxury sports car manufacturer based in Maranello,Italy. ")
main_doc = nlp("Salvador dali,Spanish Surrealist painter and printmaker,influential for his explorations of subconscIoUs imagery.")

search_doc_no_stop_words = nlp(' '.join([str(t) for t in search_doc if not t.is_stop]))
search_doc_2_no_stop_words = nlp(' '.join([str(t) for t in search_doc2 if not t.is_stop]))
main_doc_no_stop_words = nlp(' '.join([str(t) for t in main_doc if not t.is_stop]))

print(search_doc_no_stop_words.similarity(main_doc_no_stop_words))
print(search_doc_no_stop_words.similarity(search_doc_2_no_stop_words))
print(search_doc_2_no_stop_words.similarity(main_doc_no_stop_words))

返回

0.6973421217068086
0.5081730270792771 # I would expect a lower value
0.4034629230737514 # I would expect a lower value

我正在尝试生成单词嵌入向量以将句子聚类到3D空间中:

vector_1 = np.array([token.vector for token in search_doc_no_stop_words])
vector_2 = np.array([token.vector for token in search_doc_2_no_stop_words])
vector_3 = np.array([token.vector for token in main_doc_no_stop_words])

然后使用PCA组件对每个向量进行变换:

pca_vec_1 = PCA(n_components=3).fit_transform(vector_1)
pca_vec_2 = PCA(n_components=3).fit_transform(vector_2)
pca_vec_3 = PCA(n_components=3).fit_transform(vector_3)

我想在3D空间中绘制句子(我想通过使用matplotlib)。但是,我不确定这些步骤对于3D空间中的句子表示是否有意义。 欢迎提示

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)