使用 KeyedVectors 对象,我可以得到 W2V 向量,给定一个单词,就像这样。
from gensim.models import KeyedVectors
model = KeyedVectors.load('vectors.kv')
model.get_vector('example') # output => [0.12, 0.41, ..., 0.92]
如何对模型中包含的每个项(键)执行相同的操作?
请注意,这不是 have 是 KeyedVectors 对象,它也可以是 Word2Vec 对象。
编辑 - 感谢 gojomo:
vector_dct = {}
for word in kv_model.index2word:
vector_dct[word] = kv_model.get_vector(word)
df = pd.DataFrame(vector_dct).T
for word in kv_model.index_to_key: # was kv_model.index2word pre-gensim-4.0.0, when Q 1st asked
kv_model.get_vector(word)