使用TF-IDF 和餘弦相似度測量文本相似度
確定兩個文本文檔之間的相似度是文本挖掘和資訊中的一項關鍵任務檢索。一種流行的方法是利用 TF-IDF(術語頻率-逆文檔頻率) 和 餘弦相似度。
TF-IDF 分配權重根據文件中每個單字在該文件中的出現頻率及其在整個文件語料庫中的稀有性來對文件中的每個單字進行分類。具有相似單字模式的文件將共用更高的 TF-IDF 向量。
餘弦相似度 測量兩個向量之間的角度,提供 0(無相似性)和 1(完全相似)之間的值。在我們的例子中,兩個文件的 TF-IDF 向量形成了這些向量,餘弦相似度量化了它們的角度。
Python 實作
在Python 中,使用scikit-learn 和Gensim 包,計算成對相似度很包簡單:
<code class="python">from sklearn.feature_extraction.text import TfidfVectorizer documents = [open(f).read() for f in text_files] tfidf = TfidfVectorizer().fit_transform(documents) pairwise_similarity = tfidf * tfidf.T</code>
或者,如果文件已經是字串,請使用:
<code class="python">corpus = ["I'd like an apple", "An apple a day keeps the doctor away", "..."] vect = TfidfVectorizer(min_df=1, stop_words="english") tfidf = vect.fit_transform(corpus) pairwise_similarity = tfidf * tfidf.T</code>
解釋結果
pairwise_similarity 是稀疏性矩陣,表示每個文檔對之間的相似度。要查找與特定文檔最相似的文檔,請屏蔽文檔與其自身的相似性(將其設置為NaN),並使用np.nanargmax() 查找其行中的最大值:
<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]) similar_doc = corpus[result_idx]</code>
其他注意事項
對於大型語料庫和詞彙表,使用稀疏矩陣比轉換為NumPy 數組更有效率。
透過調整 TfidfVectorizer 中的參數,例如最小文件的 min_df頻率,可以自訂 TF-IDF 計算以滿足特定要求。
其他資源
- [資訊擷取簡介](http://infolab .stanford.edu/~backrub/classes/2002/cs276/handouts/stanford.edu/~backrub/classes/2002/cs276/handouts/ 04-tfidf.pdf)
- [使用Gensim 計算成對相似性](https://stackoverflow.com/questions/23752770/computing-與gensim 的成對相似性)
以上是如何使用 TF-IDF 和餘弦相似度測量文字相似度?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Python是解釋型語言,但也包含編譯過程。 1)Python代碼先編譯成字節碼。 2)字節碼由Python虛擬機解釋執行。 3)這種混合機制使Python既靈活又高效,但執行速度不如完全編譯型語言。

UseeAforloopWheniteratingOveraseQuenceOrforAspecificnumberoftimes; useAwhiLeLoopWhenconTinuingUntilAcIntiment.forloopsareIdealForkNownsences,而WhileLeleLeleLeleLeleLoopSituationSituationsItuationsItuationSuationSituationswithUndEtermentersitations。

pythonloopscanleadtoerrorslikeinfiniteloops,modifyingListsDuringteritation,逐個偏置,零indexingissues,andnestedloopineflinefficiencies

forloopsareadvantageousforknowniterations and sequests,供應模擬性和可讀性;而LileLoopSareIdealFordyNamicConcitionSandunknowniterations,提供ControloperRoverTermination.1)forloopsareperfectForeTectForeTerToratingOrtratingRiteratingOrtratingRitterlistlistslists,callings conspass,calplace,cal,ofstrings ofstrings,orstrings,orstrings,orstrings ofcces

pythonisehybridmodeLofCompilation和interpretation:1)thepythoninterpretercompilesourcecececodeintoplatform- interpententbybytecode.2)thepythonvirtualmachine(pvm)thenexecutecutestestestestestesthisbytecode,ballancingEaseofuseEfuseWithPerformance。

pythonisbothinterpretedAndCompiled.1)它的compiledTobyTecodeForportabilityAcrosplatforms.2)bytecodeisthenInterpreted,允許fordingfordforderynamictynamictymictymictymictyandrapiddefupment,儘管Ititmaybeslowerthananeflowerthanancompiledcompiledlanguages。

在您的知識之際,而foroopsareideal insinAdvance中,而WhileLoopSareBetterForsituations則youneedtoloopuntilaconditionismet

ForboopSareSusedwhenthentheneMberofiterationsiskNownInAdvance,而WhileLoopSareSareDestrationsDepportonAcondition.1)ForloopSareIdealForiteratingOverSequencesLikelistSorarrays.2)whileLeleLooleSuitableApeableableableableableableforscenarioscenarioswhereTheLeTheLeTheLeTeLoopContinusunuesuntilaspecificiccificcificCondond


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

Dreamweaver CS6
視覺化網頁開發工具

Atom編輯器mac版下載
最受歡迎的的開源編輯器

Dreamweaver Mac版
視覺化網頁開發工具