Python实现word2Vec model过程解析

作者:Leslie_Chan 时间:2023-10-07 14:22:10 

这篇文章主要介绍了Python实现word2Vec model过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下


import gensim, logging, os
logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.INFO)
import nltk

corpus = nltk.corpus.brown.sents()

fname = 'brown_skipgram.model'
if os.path.exists(fname):
 # load the file if it has already been trained, to save repeating the slow training step below
 model = gensim.models.Word2Vec.load(fname)
else:
 # can take a few minutes, grab a cuppa
 model = gensim.models.Word2Vec(corpus, size=100, min_count=5, workers=2, iter=50)
 model.save(fname)

words = "woman women man girl boy green blue".split()
for w1 in words:
 for w2 in words:
   print(w1, w2, model.similarity(w1, w2))

print(model.most_similar(positive=['woman', ''], topn=1))
print(model.similarity('woman', 'girl'))girl

在gensim模块中已经封装了13年提出的model--word2vec,所以我们直接开始建立模型

Python实现word2Vec model过程解析

这是建立模型的过程,最后会出现saving Word2vec的语句,代表已经成功建立了模型

Python实现word2Vec model过程解析

这是输入了 gorvement和news关键词后 所反馈的词语 --- administration, 他们之间的相关性是0.508

当我在输入 women 和 man ,他们显示的相关性的0.638 ,已经是非常高的一个数字。

值得一提的是,我用的语料库是直接从nltk里的brown语料库。其中大概包括了一些新闻之类的数据。

大家如果感兴趣的话,可以自己建立该模型,通过传入不同的语料库,来calc 一些term的 相关性噢

来源:https://www.cnblogs.com/lesliechan/p/11966642.html

标签:Python,实现,word2Vec,model
0
投稿

猜你喜欢

  • 使用 Python 写一个简易的抽奖程序

    2023-07-28 00:46:54
  • Python3爬虫爬取百姓网列表并保存为json功能示例【基于request、lxml和json模块】

    2023-12-16 21:28:44
  • 配置 SQLServer2005 以允许远程连接

    2024-01-18 19:11:01
  • 浅析PHP的ASCII码转换类

    2023-09-08 02:54:28
  • python两种注释用法的示例

    2022-07-22 04:50:50
  • 终结IE6下背景图片闪烁问题

    2009-03-04 10:11:00
  • python数据可视化的那些操作你了解吗

    2023-11-04 15:18:55
  • asp实现非大小写的替换函数

    2010-05-19 21:23:00
  • 使用SQL Server分区表功能提高数据库的读写性能

    2024-01-17 18:32:20
  • Python高阶函数map() 简介和使用详解

    2021-04-03 04:34:11
  • 简单了解Python多态与属性运行原理

    2021-03-13 21:29:42
  • 用CSS实现柱状图(Bar Graph)的方法(二)—基于表格元素的柱状图

    2008-05-26 13:23:00
  • python安装cx_Oracle模块常见问题与解决方法

    2021-04-24 13:00:27
  • 对python实时得到鼠标位置的示例讲解

    2022-02-21 10:01:25
  • 如何测试字符串的长度?

    2009-11-11 20:02:00
  • element弹窗表格的字体模糊bug解决

    2024-04-18 10:53:25
  • Python实现K-means聚类算法并可视化生成动图步骤详解

    2021-06-20 23:10:40
  • Python二叉树的遍历操作示例【前序遍历,中序遍历,后序遍历,层序遍历】

    2022-07-06 16:40:59
  • 谈谈Javascript中的++和–操作符

    2009-05-08 11:43:00
  • Python+numpy实现一个蜘蛛纸牌游戏

    2022-05-02 03:29:08
  • asp之家 网络编程 m.aspxhome.com