Python 中的自然语言处理(NLP)
Fouad Trad
Machine Learning Engineer




reviews = [ "I loved the movie. It was amazing!", "The movie was okay.", "I hated the movie. It was boring." ]cleaned_reviews = [preprocess(review) for review in reviews] print(cleaned_reviews)
['i loved the movie it was amazing',
'the movie was okay',
'i hated the movie it was boring']
from sklearn.feature_extraction.text import TfidfVectorizervectorizer = TfidfVectorizer()tfidf_matrix = vectorizer.fit_transform(cleaned_reviews)print(tfidf_matrix)
<Compressed Sparse Row 稀疏矩阵,dtype 为 'float64'
含 16 个存储元素,形状为 (3, 9)>
print(tfidf_matrix.toarray())
[[0.52523431 0. 0. 0.39945423 0.52523431 0.31021184 0. 0.31021184 0.31021184]
[0. 0. 0. 0. 0. 0.41285857 0.69903033 0.41285857 0.41285857]
[0. 0.52523431 0.52523431 0.39945423 0. 0.31021184 0. 0.31021184 0.31021184]]
vectorizer.get_feature_names_out()
['amazing' 'boring' 'hated' 'it' 'loved' 'movie' 'okay' 'the' 'was']
import pandas as pd df_tfidf = pd.DataFrame(tfidf_matrix.toarray(),columns=vectorizer.get_feature_names_out() )
import seaborn as sns import matplotlib.pyplot as pltsns.heatmap(df_tfidf, annot=True)plt.title("不同评论的 TF-IDF 分数") plt.xlabel("术语") plt.ylabel("文档") plt.show()



Python 中的自然语言处理(NLP)