问题描述
我从桌面上导入一个文本文件,以便在jupyter笔记本上使用gensim模型.但是,它返回:
I import a text file from the desktop to do with gensim model on jupyter notebook. However, it return that:
AttributeError:模块'gensim.models.word2vec'没有属性加载""
AttributeError: module 'gensim.models.word2vec' has no attribute 'load'"
如何解决此问题
import numpy as np
import pandas as pd
import gensim
from matplotlib import pyplot as plt
from gensim.models import word2vec
from collections import defaultdict
from sklearn.cluster import KMeans
model = word2vec.Text8Corpus(r'C:\Users\qlm\Desktop\globalwarming.txt')
model = word2vec.load(r'C:\Users\qlm\Desktop\globalwarming.txt')
推荐答案
有一个名为 word2vec
的模块,其内部有一个名为 Word2Vec
的类,因为Word2Vec
类在 gensim.models 的code> __ init __.py :
There is a module named word2vec
and inside it a class named Word2Vec
, since the Word2Vec
class is imported in __init__.py
of gensim.models
you can import it as you tried before:
from gensim.models import Word2Vec
然后,您将可以访问加载方法.
您也可以使用完整的名称空间.
You can also use the full namespace too.
所以:
# Will work as long as models.__init__ keep it available
from gensim.models import Word2Vec
但是:
# Will always work as long as the namespace is not altered
from gensim.models.word2vec import Word2Vec
我个人更喜欢第二选择.
I personally prefer the second choice.
这篇关于AttributeError:模块'gensim.models.word2vec'没有属性'load'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!