1

I have a function where I need to perform np.exp(matrix1 @ matrix2), but I receive the error message: loop of ufunc does not support argument 0 of type float which has no callable exp method

  • matrix1 is a 210 by 4 matrix of float values
  • matrix2 is a 4 by 1 of float values
  • matrix1 @ matrix2 is a 210 by 1 of float values
  • type(matrix1 @ matrix) reports numpy.ndarray

numpy.exp() expects an array_like argument so I don't understand why this gags.

Error details:

newval = np.exp(matrix1 @ matrix2)

AttributeError                            Traceback (most recent call last)
AttributeError: 'float' object has no attribute 'exp'

The above exception was the direct cause of the following exception:

TypeError                                 Traceback (most recent call last)
<ipython-input-563-7924faaa390b> in <module>
----> 1 np.exp(matrix1 @ matrix2)

TypeError: loop of ufunc does not support argument 0 of type float which has no callable exp method
​
alan.elkin
  • 954
  • 1
  • 10
  • 19

1 Answers1

0

exp works on an array of floats:

In [186]: arr = np.array([1.,2.,3.])                                                                            
In [187]: np.exp(arr)                                                                                           
Out[187]: array([ 2.71828183,  7.3890561 , 20.08553692])

but not if the array dtype is object:

In [188]: np.exp(arr.astype(object))                                                                            
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
AttributeError: 'float' object has no attribute 'exp'

The above exception was the direct cause of the following exception:

TypeError                                 Traceback (most recent call last)
<ipython-input-188-b80d2a0372d5> in <module>
----> 1 np.exp(arr.astype(object))

TypeError: loop of ufunc does not support argument 0 of type float which has no callable exp method

Your array may have mixed objects - floats, ints, lists, who knows what.

===

Why is matrix1 @ matrix2 object dtype? What is matrix1 and matrix2? @ is normally a numeric operation, though it has been recently extended to work with object dtypes. But that is a slower calculation.

hpaulj
  • 221,503
  • 14
  • 230
  • 353