Sklearn库计算TFIDF
from sklearn.feature_extraction.text import CountVectorizer,TfidfTransformer
def TF_IDF(corpus):
vectorizer=CountVectorizer()
transformer=TfidfTransformer()
x = vectorizer.fit_transform(corpus)
tfidf=transformer.fit_transform(vectorizer.fit_transform(corpus))
word=vectorizer.get_feature_names()
word_location = vectorizer.vocabulary_
weight=tfidf.toarray()
return weight,word_location,x.toarray()
corpus = [
'我 来到 北京 清华大学',
'他 来到 了 中国',
'小明 硕士 毕业 与 中国 科学院',
'我 爱 北京 天安门'
]
weight,word_location,tf = TF_IDF(corpus)
print(weight)
print(word_location)
print(tf)