pandas - 通过排除某些词汇更快地加载 fasttext 模型

标签 pandas scikit-learn nlp stanford-nlp fasttext

在本地计算机上加载 Facebook Research 发布的预训练快速文本词向量需要很长时间,我喜欢这样:

model =  fs.load_word2vec_format('wiki.en.vec') 
print(model['test']) # get the vector of the word 'test'

我正在寻求通过删除数据集中未出现的单词的词向量来减少加载时间。 IE。我想将预训练向量模型简化为构成我需要分析的数据集的单词,这是预训练模型的子集。

我打算尝试通过提取所需的词向量并保存到新模型来构建新模型,但类型将从 FastTextKeyedVectors 更改为 FastText:

#model2 = gensim.models.Word2Vec(iter=1)
#model2 = FastText()
for word in words:
    if (word in model):
       model2[] = model[word]

如何减少加载时间?我的方法有意义还是我走错了路?

最佳答案

如果可以迭代 .vec 格式,这将是消除不需要的单词的最快方法。要了解这一点,您应该查看数据库的结构。如果它是接近 xml 格式的东西,那应该是可行的,并且如何在 python 中迭代 xml 文件已被广泛记录。

但是,关于您的方法,假设您刚刚以字典的形式将整个模型加载到RAM中,只是一个快速提示,请使用理解语法:

model={model[word] for word in model if word in mywords}

其中 mywords 是您要保留的单词列表。

关于pandas - 通过排除某些词汇更快地加载 fasttext 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44615340/

相关文章:

python - 使用 matplotlib 的 savefig 保存从 python pandas 生成的图(AxesSubPlot)

pandas - mplfinance 移动平均线和指数移动平均线

python - 将 python 模块添加到 AzureML 工作区

python-3.x - 从 SpaCy 中删除跨度中的单词?

java - 从 stanford core nlp 依赖表示中提取主要语义元素

machine-learning - 使用 Keras 进行文本分类 : How to add custom features?

python - 使用 pandas 重新分配项目会给出错误的结果

python - 使用 xlsxwriter 在条件格式(公式)后删除列

python - 如何在 Sklearn 中使用自定义距离度量

python - 学习 : Is there any way to debug Pipelines?