テキスト ドキュメントの類似性の計算方法
ペアごとの類似性の計算
2 つのテキスト ドキュメント間の類似性を判断する最も一般的な方法は、テキスト ドキュメントを次のように変換することです。 TF-IDF (Term Frequency-Inverse Document Frequency) ベクトルを計算し、コサイン類似度を使用してそれらを比較します。このアプローチは、情報検索に関する教科書で説明されており、「情報検索の概要」で詳しく説明されています。
Gensim や scikit-learn などの Python ライブラリは、TF-IDF 変換とコサイン類似度計算の実装を提供します。 scikit-learn を使用すると、次のコード スニペットでコサイン類似度の計算が実行されます。
<code class="python">from sklearn.feature_extraction.text import TfidfVectorizer # Extract documents from text files documents = [open(f).read() for f in text_files] # Create a TF-IDF vectorizer tfidf = TfidfVectorizer().fit_transform(documents) # Calculate pairwise cosine similarity pairwise_similarity = tfidf * tfidf.T</code>
プレーン テキスト ドキュメントの場合:
<code class="python">corpus = ["I'd like an apple", "An apple a day keeps the doctor away", "Never compare an apple to an orange", "I prefer scikit-learn to Orange", "The scikit-learn docs are Orange and Blue"] # Create a TF-IDF vectorizer with minimum frequency and exclusion of stop words vect = TfidfVectorizer(min_df=1, stop_words="english") # Apply TF-IDF transformation tfidf = vect.fit_transform(corpus) # Calculate pairwise cosine similarity pairwise_similarity = tfidf * tfidf.T </code>
結果の解釈
pairwise_similarity は各行と列がコーパス内のドキュメントを表すスパース行列。スパース行列を NumPy 配列に変換すると、各セルが 2 つの対応するドキュメント間の類似性を表していることがわかります。
たとえば、「scikit-learn ドキュメントはオレンジとブルーです」に最も類似したドキュメントを特定するには、次の場所を見つけます。コーパス内のインデックスを取得し、np.fill_diagonal():
<code class="python">import numpy as np arr = pairwise_similarity.toarray() np.fill_diagonal(arr, np.nan) input_doc = "The scikit-learn docs are Orange and Blue" input_idx = corpus.index(input_doc) result_idx = np.nanargmax(arr[input_idx]) print(corpus[result_idx])</code>
で対角線 (自己相似性を表す) をマスクした後、np.nanargmax を対応する行に適用します。大規模なデータセットの場合は、スパース行列はメモリを節約します。あるいは、pairwise_similarity.shape を使用して自己類似性と argmax() を直接マスクすることを検討してください。
<code class="python">n, _ = pairwise_similarity.shape pairwise_similarity[np.arange(n), np.arange(n)] = -1.0 pairwise_similarity[input_idx].argmax() </code>
以上がTF-IDF とコサイン類似度を使用してテキスト ドキュメント間の類似性を計算するにはどうすればよいですか?の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

AlaySaregenerallymorememory-effictient forstring forstring inumericaldataduetotheirfixed-sizenature anddirectmoryaccess.1)AraysstoreElementsinaCourowlock、Reducingoverheadfrompointertersormetadata.2)リスト

ToconvertaPythonlisttoanarray,usethearraymodule:1)Importthearraymodule,2)Createalist,3)Usearray(typecode,list)toconvertit,specifyingthetypecodelike'i'forintegers.Thisconversionoptimizesmemoryusageforhomogeneousdata,enhancingperformanceinnumericalcomp

Pythonリストは、さまざまな種類のデータを保存できます。サンプルリストには、整数、文字列、フローティングポイント番号、ブール膜、ネストされたリスト、辞書が含まれています。リストの柔軟性は、データ処理とプロトタイピングにおいて価値がありますが、コードの読みやすさと保守性を確保するためには注意して使用する必要があります。

Pythondoesnothavebuiltinarays; usethearmoduleformemory-efficienthogeneousdatastorage、while-lelistSareversatileformixeddatypes.Arraysareeffientive for forlardatedateSetsetype、wheneasofferistofibuliestibuliestuseduseerieartusedoersorerdatatess。

sostCommonlylysedModule forcreatinginpythonisnumpy.1)numProvidesefficientToolsForArrayoperations、理想的なfornumericaldata.2)arrayscanbecreatedusingnp.array()for1dand2dstructures.3)

toAppendElementStoapyThonList、usetheappend()methodforsingleelements、extend()formultipleElements、andinsert()forspecificopsitions.1)useappend()foraddingoneElementatheend.2)useextend()toaddmultipleelementseffictience.3)

To CreateapythonList、usesquareBrackets []およびSeparateItemswithcommas.1)listsaredynamicandcanholdmixdatatypes.2)useappend()、remaid()、andslicingformanipulation.3)listcompreheNsionsionsionsionsionsionsionsionsionsionsionsionsionsionsionsionsionsientionforcreating.4)

金融、科学研究、医療、およびAIの分野では、数値データを効率的に保存および処理することが重要です。 1)財務では、メモリマッピングされたファイルとnumpyライブラリを使用すると、データ処理速度が大幅に向上する可能性があります。 2)科学研究の分野では、HDF5ファイルはデータストレージと取得用に最適化されています。 3)医療では、インデックス作成やパーティション化などのデータベース最適化テクノロジーがデータのパフォーマンスを向上させます。 4)AIでは、データシャーディングと分散トレーニングがモデルトレーニングを加速します。システムのパフォーマンスとスケーラビリティは、適切なツールとテクノロジーを選択し、ストレージと処理速度の間のトレードオフを検討することにより、大幅に改善できます。


ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

SublimeText3 中国語版
中国語版、とても使いやすい

メモ帳++7.3.1
使いやすく無料のコードエディター

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

PhpStorm Mac バージョン
最新(2018.2.1)のプロフェッショナル向けPHP統合開発ツール

SecLists
SecLists は、セキュリティ テスターの究極の相棒です。これは、セキュリティ評価中に頻繁に使用されるさまざまな種類のリストを 1 か所にまとめたものです。 SecLists は、セキュリティ テスターが必要とする可能性のあるすべてのリストを便利に提供することで、セキュリティ テストをより効率的かつ生産的にするのに役立ちます。リストの種類には、ユーザー名、パスワード、URL、ファジング ペイロード、機密データ パターン、Web シェルなどが含まれます。テスターはこのリポジトリを新しいテスト マシンにプルするだけで、必要なあらゆる種類のリストにアクセスできるようになります。
