sklearn: TfidfVectorizer 中文處理及一些使用參數


TfidfVectorizer可以把原始文本轉化為tf-idf的特征矩陣,從而為后續的文本相似度計算,主題模型,文本搜索排序等一系列應用奠定基礎。基本應用如:

#coding=utf-8
from sklearn.feature_extraction.text import TfidfVectorizer
document = ["I have a pen.",
            "I have an apple."]
tfidf_model = TfidfVectorizer().fit(document)
sparse_result = tfidf_model.transform(document)     # 得到tf-idf矩陣,稀疏矩陣表示法
print(sparse_result)
# (0, 3)    0.814802474667
# (0, 2)    0.579738671538
# (1, 2)    0.449436416524
# (1, 1)    0.631667201738
# (1, 0)    0.631667201738
print(sparse_result.todense())                     # 轉化為更直觀的一般矩陣
# [[ 0.          0.          0.57973867  0.81480247]
#  [ 0.6316672   0.6316672   0.44943642  0.        ]]
print(tfidf_model.vocabulary_)                      # 詞語與列的對應關系
# {'have': 2, 'pen': 3, 'an': 0, 'apple': 1}

https://blog.csdn.net/blmoistawinde/article/details/80816179


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM