矢量化列表

午夜

需要帮助的家伙。需要对列表进行向量化,即用某个计算出的数字替换子列表的每个元素。子列表每个元素的公式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
瓦西里·G。

这是一个解决方案,遵循针对您的问题的自上而下的设计:

import math

def frequency(term, document):
    return document.count(term) / len(document)

def totalNumOfDocuments(inList):
    return len(inList)

def numOfDocumentsForTerm(inList, term):
    return len([doc for doc in inList if term in doc])

def TFCWeighting(inList):

    tfc = []

    N = totalNumOfDocuments(inList)

    for document in inList:

        temp = []
        for term in document:

            #numerator
            freq = frequency(term, document)
            n = numOfDocumentsForTerm(inList, term)
            logarithm = math.log(N/n)
            numerator = freq * logarithm

            #denominator
            summation = sum([(frequency(t, document) * math.log(N / numOfDocumentsForTerm(inList, term))) ** 2 for t in document])
            denominator = math.sqrt(summation)

            temp.append(round(numerator / denominator,3))

        tfc.append(temp)

    return tfc

l1=[['can','help','you'],['thank','you'],['help','help','help','help','help','help']]
l2=[['I','help'],['will','you']]

print(TFCWeighting(l1))
print(TFCWeighting(l2))

输出:

[[0.577, 0.577, 0.577], [0.707, 0.707], [0.408, 0.408, 0.408, 0.408, 0.408, 0.408]]
[[0.707, 0.707], [0.707, 0.707]]

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章