Search code examples
pythonarraysnumpyfunctionlogarithm

TypeError: return arrays must be of ArrayType for a function that uses only floats


This one really stumps me. I have a function that calculates the weight of a word, I've confirmed that both a and b local variables are of type float:

def word_weight(term):
    a = term_freq(term)
    print a, type(a)
    b = idf(term)
    print b, type(b)
    return a*log(b,2)

running word_weight("the") logs:

0.0208837518791 <type 'float'>
6.04987801572 <type 'float'>
Traceback (most recent call last):
  File "summary.py", line 59, in <module>
    print word_weight("the")
  File "summary.py", line 43, in word_weight
    return a*log(b,2)
TypeError: return arrays must be of ArrayType

why?


Solution

  • You are using numpy.log function here, its second argument is not base but out array:

    >>> import numpy as np
    >>> np.log(1.1, 2)
    Traceback (most recent call last):
      File "<ipython-input-5-4d17df635b06>", line 1, in <module>
        np.log(1.1, 2)
    TypeError: return arrays must be of ArrayType
    

    You can now either use numpy.math.log or Python's math.log:

    >>> np.math.log(1.1, 2)
    0.13750352374993502
    >>> import math
    >>> math.log(1.1, 2) #This will return a float object not Numpy's scalar value
    0.13750352374993502
    

    Or if you're dealing only with base 2 then as @WarrenWeckesser suggested you can use numpy.log2: