栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

gensim报错 : TypeError: ‘Word2Vec‘ object is not subscriptable

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

gensim报错 : TypeError: ‘Word2Vec‘ object is not subscriptable

gensim报错 : TypeError: 'Word2Vec' object is not subscriptable
  • 报错原因
  • 解决方法
    • 降低版本(不推荐)
    • 按照gensim4的使用方法来用
  • 整体代码(用gensim模块训练得到词向量)
  • 参考文档

报错原因

gensim 4 版本与 gensim3使用方法不同。

解决方法 降低版本(不推荐)

安装gensim3版本

pip install gensim==3.2
按照gensim4的使用方法来用

目的是为了获得某个词的词向量

model = Word2Vec(sentences, min_count=1)

## 原来的代码:出现报错的地方
print(model['sentence'])

## 修改后的代码
print(model.wv['sentence'])
整体代码(用gensim模块训练得到词向量)
from gensim.models import Word2Vec
# define training data
sentences = [['this', 'is', 'the', 'first', 'sentence', 'for', 'word2vec'],
            ['this', 'is', 'the', 'second', 'sentence'],
            ['yet', 'another', 'sentence'],
            ['one', 'more', 'sentence'],
            ['and', 'the', 'final', 'sentence']]
# train model
model = Word2Vec(sentences, min_count=1)
# summarize the loaded model
print(model)
# summarize vocabulary
words = list(model.wv.key_to_index)
print(words)
# access vector for one word
print(model.wv['sentence'])   
# save model
model.save('model.bin')
# load model
new_model = Word2Vec.load('model.bin')
print(new_model)
参考文档

https://github.com/RaRe-Technologies/gensim/wiki/Migrating-from-Gensim-3.x-to-4

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/286621.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号