我正在嘗試計(jì)算大約 7000 個文檔的語料庫的 tfidf 值。在互聯(lián)網(wǎng)上搜索,我找到了很多示例(當(dāng)我嘗試為每個文檔創(chuàng)建唯一詞矩陣時,其中許多示例被鎖定)。唯一有效的似乎是下面的代碼from sklearn.feature_extraction.text import TfidfVectorizerimport pandas as pdtfidf = TfidfVectorizer()x = tfidf.fit_transform(corpus)df_tfidf = pd.DataFrame(x.toarray(), columns=tfidf.get_feature_names())print(df_tfidf)假設(shè)以下語料庫corpus = [ 'This is the first document.', 'This document is the second document.', 'And this is the third one.', 'Is this the first document?']它產(chǎn)生了這樣的輸出:這段代碼也適用于我的情況,事實(shí)上它生成了一個包含 7180 行和 10390 列的矩陣。但我不確定這是否正確。您認(rèn)為,這是計(jì)算一組文檔的 tfidf 的有效解決方案嗎?ps:我可以插入我關(guān)注的指南鏈接嗎?
1 回答

慕田峪7331174
TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超13個贊
是的,這是計(jì)算 tf-idf 矩陣的正確方法。
您正在使用
x = tfidf.fit_transform(corpus)
它首先適合您的TfidfVectorizer
語料庫,然后相應(yīng)地轉(zhuǎn)換語料庫,這樣您就可以得到 tf-idf 矩陣作為x
添加回答
舉報
0/150
提交
取消