【发布时间】:2020-04-13 13:02:08
【问题描述】:
我有文章的数据集以及这些文章中每个单词出现的次数: 如何计算TF-IDF?
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns; sns.set()
from sklearn.cluster import KMeans
import pandas as pd
import sklearn as sk
import math
data = pd.read_csv('D:\\Datasets\\NIPS_1987-2015.csv', index_col ="word")
# retrieving row by loc method
first = data["1987_1"]
second = data["1987_2"]
print(first, "\n\n\n", second)
我得到了这个数据库:
word
abalone 0
abbeel 0
abbott 0
abbreviate 0
abbreviated 0
..
zoo 0
zoom 0
zou 0
zoubin 0
zurich 0
Name: 1987_1, Length: 11463, dtype: int64
word
abalone 0
abbeel 0
abbott 0
abbreviate 0
abbreviated 0
..
zoo 0
zoom 0
zou 0
zoubin 0
zurich 0
Name: 1987_2, Length: 11463, dtype: int64
那么从这里如何计算 TF-IDF?有什么建议?我应该转换成字典还是有另一种可能性?
【问题讨论】:
-
你能提供一些数据吗?你期望出来的是什么?在我看来,这就像您正在导入一个单词数组,并且您正在使用单词作为索引......到目前为止,输出是否符合您的预期?还是有什么不符合您的预期?
-
每个单词出现的次数。是的,像索引这样的词。两篇文章都有很多零。例如鲍鱼不在这 2 篇文章中,但出现在另一篇文章中。目前,我只检查这两篇文章。但例如我有一个单词“written”,在 1987_1 中出现 1,在 1987_2 中出现 3
标签: python pandas numpy tf-idf tfidfvectorizer