我在Lucene建了一个索引.我希望不指定查询,只是为了获得索引中两个文档之间的分数(余弦相似度或其他距离?).
例如,我从之前打开的IndexReader获取带有ID 2和4的文档.文档d1 = ir.document(2); 文件d2 = ir.document(4);
如何获得这两个文档之间的余弦相似度?
谢谢
我正在尝试使用tm包进行一些非常基本的文本分析并得到一些tf-idf分数; 我正在运行OS X(虽然我在Debian Squeeze上试过这个但结果相同); 我有一个目录(这是我的工作目录),里面有几个文本文件(第一集包含尤利西斯的前三集,第二集包含第二集,如果你必须知道的话).
R版本:2.15.1 SessionInfo()报告这个关于tm:[1] tm_0.5-8.3
相关的代码:
library('tm')
corpus <- Corpus(DirSource('.'))
dtm <- DocumentTermMatrix(corpus,control=list(weight=weightTfIdf))
str(dtm)
List of 6
$ i : int [1:12456] 1 1 1 1 1 1 1 1 1 1 ...
$ j : int [1:12456] 2 10 12 17 20 24 29 30 32 34 ...
$ v : num [1:12456] 1 1 1 1 1 1 1 1 1 1 ...
$ nrow : int 2
$ ncol : int 10646 …Run Code Online (Sandbox Code Playgroud) 我有一个TfidfVectorizer矢量化文章集合,然后是特征选择.
vectroizer = TfidfVectorizer()
X_train = vectroizer.fit_transform(corpus)
selector = SelectKBest(chi2, k = 5000 )
X_train_sel = selector.fit_transform(X_train, y_train)
Run Code Online (Sandbox Code Playgroud)
现在,我想存储它并在其他程序中使用它.我不想TfidfVectorizer()在训练数据集上重新运行和选择特征选择器.我怎么做?我知道如何使模型持久使用,joblib但我想知道这是否与使模型持久化相同.
我正在尝试使用TF-IDF将文档分类.我已经为某些文档计算了tf_idf,但是现在当我尝试计算其中两个文档之间的余弦相似度时,我得到一个追溯说:
#len(u)==201, len(v)==246
cosine_distance(u, v)
ValueError: objects are not aligned
#this works though:
cosine_distance(u[:200], v[:200])
>> 0.52230249969265641
Run Code Online (Sandbox Code Playgroud)
切片向量使len(u)== len(v)正确的方法?我认为余弦相似性适用于不同长度的矢量.
我正在使用这个功能:
def cosine_distance(u, v):
"""
Returns the cosine of the angle between vectors v and u. This is equal to
u.v / |u||v|.
"""
return numpy.dot(u, v) / (math.sqrt(numpy.dot(u, u)) * math.sqrt(numpy.dot(v, v)))
Run Code Online (Sandbox Code Playgroud)
另外 - 向量中tf_idf值的顺序是否重要?它们应该被分类 - 或者对于这个计算是否不重要?
我正在尝试使用Python的Tfidf来转换文本语料库.但是,当我尝试fit_transform它时,我得到一个值错误ValueError:空词汇; 也许这些文件只包含停用词.
In [69]: TfidfVectorizer().fit_transform(smallcorp)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-69-ac16344f3129> in <module>()
----> 1 TfidfVectorizer().fit_transform(smallcorp)
/Users/maxsong/anaconda/lib/python2.7/site-packages/sklearn/feature_extraction/text.pyc in fit_transform(self, raw_documents, y)
1217 vectors : array, [n_samples, n_features]
1218 """
-> 1219 X = super(TfidfVectorizer, self).fit_transform(raw_documents)
1220 self._tfidf.fit(X)
1221 # X is already a transformed view of raw_documents so
/Users/maxsong/anaconda/lib/python2.7/site-packages/sklearn/feature_extraction/text.pyc in fit_transform(self, raw_documents, y)
778 max_features = self.max_features
779
--> 780 vocabulary, X = self._count_vocab(raw_documents, self.fixed_vocabulary)
781 X = X.tocsc()
782
/Users/maxsong/anaconda/lib/python2.7/site-packages/sklearn/feature_extraction/text.pyc in _count_vocab(self, raw_documents, fixed_vocab)
725 vocabulary = …Run Code Online (Sandbox Code Playgroud) 我想知道是否有可能在整个索引或别名的Elasticsearch字段中获得前十个最常用的单词.
这是我正在尝试做的事情:
我正在索引从各种文档类型(Word,Powerpoint,PDF等)中提取的文本文档,这些文档被分析并存储在名为doc_content的字段中.我想知道是否有办法找到存储在doc_content字段中的特定索引中最常用的单词.
为了更清楚,我们假设我正在索引来自亚马逊和eBay的发票.现在让我们假设我有来自亚马逊的100张发票和来自易趣的20张发票.让我们假设每个亚马逊发票中出现两次"亚马逊"一词,每个易趣发票中出现"ebay"一词三次.
现在,有没有办法得到排序的汇总,告诉我"亚马逊"这个词在我的索引中出现200次(100张发票x 2次出现/发票),"ebay"出现60次(20张发票x 3)出现/发票).
我的另一个问题是,如果前者是可能的,那么有没有办法确定某个词之后最常出现的词是什么?
例如:假设我有100个文档.这些文件中的60个包含术语"老猫",40包含术语"老狗",并且为了参数,我们假设这些词仅在每个文档中出现一次.
现在,如果我们可以得到单词"old"的频率,在我们的例子中应该是100.然后我们可以确定与它之后的单词的关系来得到类似的东西:
__________ Cat (60)
|
Old (100)-----|
|__________ Dog (40)
Run Code Online (Sandbox Code Playgroud) 我正在开发一个简单的搜索引擎,我使用 TF-IDF 公式来评估搜索词的重要性。我看到人们使用不同的公式基础,但我没有看到什么时候使用哪个的解释。这很重要吗?您有什么建议吗?
我当前的实现使用 math.h 库的常规 log() 函数
我正在使用ES搜索使用模糊搜索技术的大量人名.
TF适用于评分,但在这种情况下我并不需要IDF.这真的是在稀释分数.我仍然希望将TF和Field Norm应用于分数.
如何为我的查询禁用/抑制IDF,但保留TF和Field Norm?
我遇到了禁用IDF计算线程,但它没有帮助我.在这种情况下,似乎常数分数查询也无法帮助我.
根据我对这个查询的搜索,我在这里发帖,我有很多提出解决方案的链接,但没有提到究竟是怎么做的.例如,我已经探索过以下链接:
等等
因此,我正在理解如何在这里使用带有tf-idf的朴素贝叶斯公式,它如下:
朴素贝叶斯公式:
P(word|class)=(word_count_in_class + 1)/(total_words_in_class+total_unique_words_in_all_classes(basically vocabulary of words in the entire training set))
Run Code Online (Sandbox Code Playgroud)
tf-idf加权可以在上面的公式中使用:
word_count_in_class : sum of(tf-idf_weights of the word for all the documents belonging to that class) //basically replacing the counts with the tfidf weights of the same word calculated for every document within that class.
total_words_in_class : sum of (tf-idf weights of all the words belonging to that class)
total_unique_words_in_all_classes : as is.
Run Code Online (Sandbox Code Playgroud)
这个问题已经在堆栈溢出上多次发布,但到目前为止还没有回答任何实质性问题.我想知道我正在考虑问题的方式是否正确,即我上面已经说明的实现.我需要知道这一点,因为我自己实现了朴素贝叶斯,而没有得到任何带有Naive Bayes和tf-idf的内置函数的Python库的帮助.我真正想要的是提高使用Naive Bayes训练分类器的模型的准确度(目前为30%).因此,如果有更好的方法来达到良好的准确性,欢迎提出建议.
请建议我.我是这个领域的新手.
我有一份文件清单和整个语料库中每个独特单词的tf-idf分数.我如何在二维图上形象化,以便计算出运行k-means需要多少个簇?
这是我的代码:
sentence_list=["Hi how are you", "Good morning" ...]
vectorizer=TfidfVectorizer(min_df=1, stop_words='english', decode_error='ignore')
vectorized=vectorizer.fit_transform(sentence_list)
num_samples, num_features=vectorized.shape
print "num_samples: %d, num_features: %d" %(num_samples,num_features)
num_clusters=10
Run Code Online (Sandbox Code Playgroud)
如您所见,我能够将我的句子转换为tf-idf文档矩阵.但我不确定如何绘制tf-idf分数的数据点.
我刚在想:
谢谢
tf-idf ×10
python ×4
scikit-learn ×3
similarity ×2
c ×1
frequency ×1
joblib ×1
k-means ×1
lucene ×1
naivebayes ×1
nlp ×1
nltk ×1
pandas ×1
python-2.7 ×1
python-3.x ×1
r ×1
scipy ×1
tm ×1
trigonometry ×1