我正在学习第1 部分和第2 部分提供的教程.不幸的是,作者没有时间进行涉及使用余弦相似性的最后一节实际找到两个文档之间的距离.我在文章的示例中借助stackoverflow中的以下链接,包括上面链接中提到的代码(只是为了让生活更轻松)
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfTransformer
from nltk.corpus import stopwords
import numpy as np
import numpy.linalg as LA
train_set = ["The sky is blue.", "The sun is bright."] # Documents
test_set = ["The sun in the sky is bright."] # Query
stopWords = stopwords.words('english')
vectorizer = CountVectorizer(stop_words = stopWords)
#print vectorizer
transformer = TfidfTransformer()
#print transformer
trainVectorizerArray = vectorizer.fit_transform(train_set).toarray()
testVectorizerArray = vectorizer.transform(test_set).toarray()
print 'Fit Vectorizer to train set', trainVectorizerArray …Run Code Online (Sandbox Code Playgroud) 在scikit-learn和中有TF-IDF实现gensim.
有简单的实现在Python中简单实现N-Gram,tf-idf和余弦相似性
为了避免重新发明轮子,
在这篇博文中,它说NLTK没有它.真的吗? http://www.bogotobogo.com/python/NLTK/tf_idf_with_scikit-learn_NLTK.php
我正在使用gensim一些NLP任务.我创建从一个语料库dictionary.doc2bow,其中dictionary是一个对象corpora.Dictionary.现在我想在运行LDA模型之前过滤掉低tf-idf值的术语.我查看了语料库类的文档,但找不到访问这些术语的方法.有任何想法吗?谢谢.