需要帮助。需要矢量化列表,
也就是说,将子列表的每个元素替换为特定的计算数字。子列表https://i.stack.imgur.com/73xj9.png的每个元素的公式
列表具有以下结构:list = [[document0],[document1],[document2],[document3].......]
每个文档都由术语组成,例如document1 = ['i','love','you']
我编写了用于计算的函数,但它不能正常工作:(
def tfc(slova):
import math as m
meshokslov1=[{}for i in range(len(slova))]
meshokslov2=[{}for i in range(len(slova))]
SummaKvadratov=0
for i in range(len(slova)):
for j in range(len(slova[i])):
n=0
for q in slova:
if q.count(slova[i][j])!=0:
n+=1
if slova[i][j] in meshokslov1:
continue
else:
meshokslov1[i][slova[i][j]]=slova[i].count(slova[i][j])*m.log10(len(slova)/n)
SummaKvadratov+=(slova[i].count(slova[i][j])*m.log10(len(slova)/n))**2
for i in range(len(slova)):
for j in range(len(slova[i])):
if slova[i][j] in meshokslov2:
continue
else:
meshokslov2[i][slova[i][j]]=meshokslov1[i][slova[i][j]]/(SummaKvadratov**0.5)
return meshokslov2
以下是针对您的问题的自上而下设计的解决方案:
输出:
相关问题 更多 >
编程相关推荐