コーパス内の各ドキュメントとクラスターの間の距離を計算することにより、類似性ランキングの仕事をしています。クラスターは、ドキュメントのリストとしても提供されます。私が困っているのは、類似性を計算できるように、クラスターの重心を計算する適切な方法を思い付くことができないということです。クラスターのtfidf行列の平均値を使用しようとしましたが、結果は良くありませんでした。
例:私のクラスターは次のとおりです。
['Line a baking pan with a sheet of parchment paper.',
'Line the cake pan with parchment paper.',
'Line the bottom with parchment paper.',
'Line a baking pan with parchment paper.'
]
そして私のコーパスには次の3つのドキュメントが含まれています。
['Add vinegar and sugar.',
'Remove pan from heat and let stand 5 minutes.',
'Line the pan with parchment paper.'
]
すべてのドキュメントとクラスター間の類似性を計算したいのですが、次のような結果が得られる可能性があります。
[0.1, 0.1, 0.8]
何か提案はありますか?クラスターとコーパスの両方のドキュメントをtfidf行列として表現しようとしましたが、2つの行列間の類似性を計算して目的の結果を出すのは難しいようです。LSIを試しましたが、クラスターを表す重心を見つける必要があるのは、クラスタードキュメントではなくランク付けしたいコーパスです。
from sklearn.feature_extraction.text import TfidfVectorizer
import numpy as np
cluster = ['Line a baking pan with a sheet of parchment paper.',
'Line the cake pan with parchment paper.',
'Line the bottom with parchment paper.',
'Line a baking pan with parchment paper.']
corpus = ['Add vinegar and sugar.',
'Remove pan from heat and let stand 5 minutes.',
'Line the pan with parchment paper.']
# Train tfidf on cluster
tfidf = TfidfVectorizer()
tfidf_cluster = tfidf.fit_transform(cluster)
# Tranform the corpus using the trained tfidf
tfidf_corpus = tfidf.transform(corpus)
# Cosine similarity
cos_similarity = np.dot(tfidf_corpus, tfidf_cluster.T).A
avg_similarity = np.mean(cos_similarity, axis=1)
cos_similarity
Out[271]:
array([[0. , 0. , 0. , 0. ],
[0.31452723, 0.36145869, 0. , 0.43855558],
[0.50673521, 0.8242027 , 0.7139548 , 0.70655744]])
avg_similarity
Out[272]: array([0. , 0.27863537, 0.68786254])
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加