【问题标题】:Error in python3 np.exp(matrix1 * matrix2) - "loop of ufunc does not support argument 0 of type float which has no callable exp method"python3 np.exp(matrix1 * matrix2)中的错误-“ufunc循环不支持float类型的参数0,它没有可调用的exp方法”
【发布时间】:2020-06-17 14:01:23
【问题描述】:

我有一个函数需要执行np.exp(matrix1 @ matrix2),但我收到错误消息:loop of ufunc does not support argument 0 of type float which has no callable exp method

  • matrix1 是 float 值的 210 x 4 矩阵
  • matrix2 是 float 值的 4 x 1
  • matrix1 @ matrix2 是 float 值的 210 乘 1
  • type(matrix1 @ matrix) 举报numpy.ndarray

numpy.exp() 需要 array_like 参数,所以我不明白为什么会这样。

错误详情:

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
​

【问题讨论】:

  • 似乎您使用了变量np 在程序中的某处存储浮点值,并且将numpy 的别名覆盖为np。
  • 什么是(matrix1 @ matrix).dtype?
  • 可能相关:stackoverflow.com/a/47208873/642070。可能是当 np.exp 遍历该系列时,它假定包含的对象将具有 exp 方法。
  • tdelaney,dtype 是“dtype('O')”
  • amzon-ex,我将 numpy 导入为 np。 np 没有被用作变量。

标签: python numpy matrix


【解决方案1】:

exp 作用于浮点数组:

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

但如果数组 dtype 是 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

您的数组可能包含混合对象 - 浮点数、整数、列表,谁知道呢。

===

为什么是matrix1 @ matrix2 object dtype? matrix1 和 matrix2 是什么? @ 通常是一个数字运算,尽管它最近已扩展为与对象 dtypes 一起使用。但这是一个较慢的计算。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-29
    • 1970-01-01
    • 2021-02-14
    • 1970-01-01
    • 2021-12-13
    • 1970-01-01
    • 2020-12-09
    相关资源
    最近更新 更多