gpt4 book ai didi

Python - 数据编码向量到Word

转载 作者:行者123 更新时间:2023-11-30 09:32:19 25 4
gpt4 key购买 nike

我有一个将单词转换为向量的代码。下面是我的代码:

# word_to_vec_demo.py

from gensim.models import word2vec
import logging

logging.basicConfig(format='%(asctime)s : \
%(levelname)s : %(message)s', level=logging.INFO)

sentences = [['In', 'the', 'beginning', 'Abba','Yahweh', 'created', 'the',
'heaven', 'and', 'the', 'earth.', 'And', 'the', 'earth', 'was',
'without', 'form,', 'and', 'void;', 'and', 'darkness', 'was',
'upon', 'the', 'face', 'of', 'the', 'deep.', 'And', 'the',
'Spirit', 'of', 'Yahweh', 'moved', 'upon', 'the', 'face', 'of',
'the', 'waters.']]

model = word2vec.Word2Vec(sentences, size=10, min_count=1)

print("Vector for \'earth\' is: \n")
print(model.wv['earth'])

print("\nEnd demo")

输出为

Vector for 'earth' is: 

[-0.00402722 0.0034133 0.01583795 0.01997946 0.04112177 0.00291858
-0.03854967 0.01581967 -0.02399057 0.00539708]

是否可以将向量数组编码为单词?如果是,我将如何用Python实现它?

最佳答案

您可以使用similar_by_vector()方法从模型中通过向量查找前 N 个最相似的单词。希望这会有所帮助。

关于Python - 数据编码向量到Word,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53265028/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com