まず第一に、あなたはカウント特徴を抽出し、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ベクトルとして他のすべてのものを含む第1のベクトルは既に行正規化されている。 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計算にはこれが使用されていません。 –