每个py文件称之为模块,每个具有init.py文件的目录被称为包。只要模块或者包所在的目录在sys.path中,就可以使用import 模块或import 包来使用。
- 如果要使用的模块和当前文件在同一目录,只要import相应的文件名就可以。
- 如果使用的模块不在同一目录下,使用sys.path.append方法将模块所在目录加入到搜素目录中。然后进行import即可。这种方法是暂时的。
- 使用PYTHONPATH变量,不同的路径之间用分号隔开。设置的路径会自动加入到sys.path中,加入模块时使用import即可。
查看python已经安装的模块
help(‘modules’)
安装模块
1.直接添加到路径下。
import sys
sys.path.append(“path”)
2.单文件模块
直接把文件拷贝到$python_dir/lib。
gensim库
主题模型是对文字隐含主题进行建模的方法。主题表现为词语的条件概率分布,与主题关系越密切的词语,条件概率就越大。
对于每一篇文档,左边的矩阵C是已知的,右边两个矩阵未知,主题模型通过大量已知的”词语-文档”矩阵C,通过训练,推导出右边的两个矩阵。主题模型训练的方法有两种,pLSA和LDA。
使用模型将文档vector转换成另一个vector,这样可以修正vector的含义,减少噪音,期望达到更好的效果。
from gensim import corpora, models, similarities
//从texts中得到distinct
dictionary=corpora.Dictionary(texts)
dictionary.save(‘data.dict’)
//将得到的主题word赋予相应的数字id
dictionary.token2id
//加载dict数据文件
dictionary=corpora.Dictionary.load(‘data.dict’)
//将一个文档转换为vector
new_doc=”Human computer interaction”
//计算文档中主题词的个数,(distinctid,appears)
new_vec=dictionary.doc2bow(new_doc.lower().split())
corpus=[dictionary.doc2bow(text) for text in texts]
corpora.MmC