Python Tf IDF算法

Python Tf IDF算法,第1张

Python Tf IDF算法

这是使用scikit-learn的Tf-
idf算法的实现。在应用之前,你可以

word_tokenize()
和干你的话。

import pandas as pdfrom sklearn.feature_extraction.text import TfidfVectorizerfrom nltk import word_tokenizefrom nltk.stem.porter import PorterStemmerdef tokenize(text):    tokens = word_tokenize(text)    stems = []    for item in tokens: stems.append(PorterStemmer().stem(item))    return stems# your corpustext = ["This is your first text book", "This is the third text for analysis", "This is another text"]# word tokenize and stemtext = [" ".join(tokenize(txt.lower())) for txt in text]vectorizer = TfidfVectorizer()matrix = vectorizer.fit_transform(text).todense()# transform the matrix to a pandas dfmatrix = pd.Dataframe(matrix, columns=vectorizer.get_feature_names())# sum over each document (axis=0)top_words = matrix.sum(axis=0).sort_values(ascending=False)


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5631369.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-16
下一篇2022-12-15

发表评论

登录后才能评论

评论列表(0条)

    保存