Python – 数据编码向量到词

我有一段将词转换为向量的代码。以下是我的代码:

# word_to_vec_demo.pyfrom gensim.models import word2vecimport logginglogging.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个词。希望这对你有帮助。

Related Posts

AttributeError: ‘LinearRegression’ 对象没有 ‘predict_proba’ 属性

我需要创建一个自定义转换器作为评分器的输入。 评分器将…

无法在Azure ML工作室中上传statsmodels 0.9rc1 Python包

我在Azure ML工作室中无法上传statsmode…

Python强化学习 – 元组观测空间

我创建了一个自定义的OpenAI Gym环境,具有离散…

### 在TensorFlow中训练芯片和目标图像的格式

已关闭。 这个问题与编程或软件开发无关。目前不接受回答…

有没有用Julia编写的原生机器学习库?

我开始使用Julia了。我听说它的速度比C还快。到目前…

如何使用Spark2和Scala获取数据框中某列的不同值及其计数,并将其作为(k,v)对存储在另一个数据框中

我想获取数据框中每列的不同值及其各自的计数,并将它们作…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注