我有一个频率值表,比如-
a b
1 3 0
2 0 3
3 4 5
我想计算tfèidf。你知道吗
我的代码-
l=len(data)
for doc in data:
m=data.groupby(doc).apply(lambda column: column.sum()/(column != 0).sum())
for i in range(l):
tf=print(data.loc[i,doc])
idf=log(l/m)
weight=tf*idf
data.loc[i,doc]=weight
解释- 首先,我遍历每一列,在var m中找到该列中的非零行,并将该行在列中的特定值存储为tf,然后计算tf_idf并用tf_idf权重替换表中的值。你知道吗
预期输出-
对于g列第一行,我们有tf=3 idf=log(5/4),因此tf_idf=idf*tf
a b
1 0.4 0
2 0 0.4
3 0.17 .22
输入数据帧:
首先,找出所有单词的
idf
现在,找到
tf-idf
并更新数据帧输出:
相关问题 更多 >
编程相关推荐