首先,如果你想提取计数功能和应用TF-IDF正常化和逐行欧几里德正常化你可以用TfidfVectorizer
做在一个操作:
>>> from sklearn.feature_extraction.text import TfidfVectorizer
>>> from sklearn.datasets import fetch_20newsgroups
>>> twenty = fetch_20newsgroups()
>>> tfidf = TfidfVectorizer().fit_transform(twenty.data)
>>> tfidf
<11314x130088 sparse matrix of type '<type 'numpy.float64'>'
with 1787553 stored elements in Compressed Sparse Row format>
我们发现一个文档的余弦距离(例如,在数据集中的第一个)和所有其他的,你只需要计算f的点积由于tfidf向量已经行标准化,因此首先与其他所有向量一起构成向量。 scipy稀疏矩阵API有点奇怪(不像密集的N维numpy阵列那么灵活)。为了得到第一个矢量,你需要切片矩阵逐行获取与单行子矩阵:
>>> tfidf[0:1]
<1x130088 sparse matrix of type '<type 'numpy.float64'>'
with 89 stored elements in Compressed Sparse Row format>
scikit学习已经提供成对的指标(又名核机器学习的说法),其工作既密集和矢量集合的稀疏表示。在这种情况下,我们需要一个点的产品,也被称为线性内核:
>>> from sklearn.metrics.pairwise import linear_kernel
>>> cosine_similarities = linear_kernel(tfidf[0:1], tfidf).flatten()
>>> cosine_similarities
array([ 1. , 0.04405952, 0.11016969, ..., 0.04433602,
0.04457106, 0.03293218])
因此找到了前5名相关文件,我们可以使用argsort
和一些负面阵列切片(最相关的文档具有最高余弦相似值,因此,在排序索引数组末尾):
>>> related_docs_indices = cosine_similarities.argsort()[:-5:-1]
>>> related_docs_indices
array([ 0, 958, 10576, 3277])
>>> cosine_similarities[related_docs_indices]
array([ 1. , 0.54967926, 0.32902194, 0.2825788 ])
的第一个结果是健全性检查:我们发现查询文档与一个余弦相似性得分的1,其具有以下文本中的最类似的文件:
>>> print twenty.data[0]
From: [email protected] (where's my thing)
Subject: WHAT car is this!?
Nntp-Posting-Host: rac3.wam.umd.edu
Organization: University of Maryland, College Park
Lines: 15
I was wondering if anyone out there could enlighten me on this car I saw
the other day. It was a 2-door sports car, looked to be from the late 60s/
early 70s. It was called a Bricklin. The doors were really small. In addition,
the front bumper was separate from the rest of the body. This is
all I know. If anyone can tellme a model name, engine specs, years
of production, where this car is made, history, or whatever info you
have on this funky looking car, please e-mail.
Thanks,
- IL
---- brought to you by your neighborhood Lerxst ----
第二个最相似的文档是引用原邮件,因此有许多常用词的答复:
>>> print twenty.data[958]
From: [email protected] (Robert Seymour)
Subject: Re: WHAT car is this!?
Article-I.D.: reed.1993Apr21.032905.29286
Reply-To: [email protected]
Organization: Reed College, Portland, OR
Lines: 26
In article <[email protected]> [email protected] (where's my
thing) writes:
>
> I was wondering if anyone out there could enlighten me on this car I saw
> the other day. It was a 2-door sports car, looked to be from the late 60s/
> early 70s. It was called a Bricklin. The doors were really small. In
addition,
> the front bumper was separate from the rest of the body. This is
> all I know. If anyone can tellme a model name, engine specs, years
> of production, where this car is made, history, or whatever info you
> have on this funky looking car, please e-mail.
Bricklins were manufactured in the 70s with engines from Ford. They are rather
odd looking with the encased front bumper. There aren't a lot of them around,
but Hemmings (Motor News) ususally has ten or so listed. Basically, they are a
performance Ford with new styling slapped on top.
> ---- brought to you by your neighborhood Lerxst ----
Rush fan?
--
Robert Seymour [email protected]
Physics and Philosophy, Reed College (NeXTmail accepted)
Artificial Life Project Reed College
Reed Solar Energy Project (SolTrain) Portland, OR
对于trainVectorizerArray中的每个向量,您必须在testVectorizerArray中找到与向量的余弦相似度。 – excray
@excray非常感谢,我帮你解决了这个问题,我应该怎么做? –
@excray但我确实有一个小问题,执行tf * idf计算对此没有用处,因为我没有使用矩阵中显示的最终结果。 –