如果声明有numpy

时间:2014-06-13 22:33:02

标签: python numpy

这是我为一堂课写的一个函数:

def idf(myMatrix,N):
    idfList = []
    df = 0
    transposedMatrix = list(zip(*myMatrix))

    for row in transposedMatrix:
        for num in row:
            if num > 0:
                df = df +1
        if(df>0):
            df= math.log10(N/df)
            idfList.append(df)
        else:
            idfList.append(df)
        df = 0
    return np.matrix(idfList)

idfList = idf(rawFreqMatrix,N)

我需要做一些矩阵操作,所以我导入numpy然后我得到了这个奇怪的错误:

Traceback(most recent call last):
    File"blabbity/blah.py", line 65 in <module>
        idfList = idf(rawFreqMatrix,N)
    File "blabbity/blah.py", line 35, in idf
    if num > 0:
Value Error: The truth value of an array with more than one element is ambiguous. Use     a.any or a.all()

我查了a.any()和a.all(),但他们似乎并不是我想要的。

1 个答案:

答案 0 :(得分:1)

请勿使用matrix。始终坚持array。在其他挫折中,matrix在迭代方面表现不佳;它总能为您提供2D矩阵结果。这意味着list(zip(*myMatrix))执行以下操作:

>>> list(zip(*numpy.matrix([[1, 2], [3, 4]])))
[(matrix([[1, 2]]), matrix([[3, 4]]))]

并且接下来的循环变得更加糟糕:

for row in transposedMatrix:
    # row is a matrix, and 2D
    for num in row:
        # num is a matrix, and 2D
        # in fact, it's the entire row
        if num > 0:
            # Rather than comparing a number to 0, you're comparing a whole row.
            # That blows up when the if receives it.

真的,你不应该做list(zip(*some_numpy_object))之类的东西或者根本不迭代数组,但我们一次只能处理一个问题。