矢量化列表

时间:2018-04-05 19:14:40

标签: python text replace

需要帮助的人。需要矢量化列表, 也就是说,用一定的计算数字替换子列表的每个元素。子列表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

1 个答案:

答案 0 :(得分:0)

以下是针对您的问题采用自上而下设计的解决方案:

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]]