【问题标题】:How to fix 'Float' object has no attribute 'exp'?如何修复“浮动”对象没有属性“exp”?
【发布时间】:2017-12-25 07:46:11
【问题描述】:

我在 Python 中有以下高斯方程:

numpy.exp((-(x-m)**2)/(2*sigma))

假设x是一个矩阵。

但是,方程式不会运行,并且我收到以下错误:

AttributeError: 'Float' object has no attribute 'exp'

我该如何解决这个问题?

EDIT-1

进行以下编辑:

map(float(),np.exp((-(x-m)**2)/(2*sigma)))

引发错误:

TypeError: 'float' object is not callable

EDIT-2

这是值x的样本:

[[-0.20646505  0.07763347 -0.16161097  0.370439  ]
 [-0.91295327 -0.73768934 -0.78909055  0.06156045]
 [-0.37242104  0.51828245 -1.16138222 -0.02489585]
 [-1.07890926 -0.29704036 -1.7888618  -0.3337744 ]]

m = 5
sigma = 1

谢谢。

【问题讨论】:

  • 你做过类似numpy = float() 的事情吗??
  • 如果我尝试执行“np.exp(float((-(x-m)**2)/(2*sigma)))”或“float(np.exp((-(x-m )**2)/(2*sigma)))”,我得到错误:“TypeError:只有长度为1的数组可以转换为Python标量”
  • 确实很清楚!只有 [1] 可以转换为 1 ,而不是 [1,2,3,4] ,因为 python 无法将列表转换为浮点数。您可以使用map(float(),multi_dimensional_array) 将数组中的每个值转换为浮点数!
  • 非常感谢。即使我这样做,我也会得到“TypeError:'float' object is not callable”。请查看我的问题中的修改。
  • 你为什么使用 python 2.7 ?您应该尝试使用 python3.x

标签: python numpy math gaussian algebra


【解决方案1】:

这可能是因为您将数组的 dtype 作为对象,所以将其转换为浮点数,即

x = np.array([[-0.20646505,  0.07763347, -0.16161097,  0.370439  ],
              [-0.91295327,-0.73768934, -0.78909055,  0.06156045],
              [-0.37242104,  0.51828245, -1.16138222, -0.02489585],
              [-1.07890926, -0.29704036, -1.7888618,  -0.3337744 ]],dtype=object)

m = 5
sigma = 1
np.exp((-(x-m)**2)/(2*sigma)) 

这将导致:

 AttributeError: 'float' object has no attribute 'exp'

将其转换为浮动,即:

np.exp((-(x.astype(float)-m)**2)/(2*sigma))

array([[  1.29935943e-06,   5.47758566e-06,   1.63950875e-06,
      2.21778276e-05],
   [  2.55786406e-08,   7.10033001e-08,   5.27984133e-08,
      5.06026050e-06],
   [  5.40131118e-07,   4.34936286e-05,   5.70846640e-09,
      3.28945338e-06],
   [  9.45644899e-09,   8.07503629e-07,   9.81698210e-11,
      6.64271640e-07]])

这也取决于您使用的 numpy 版本。您还应该尝试更新 numpy,已经修复了很多错误。

【讨论】:

  • @UbdusSamad 你这是什么意思?您在尝试我的代码时遇到任何错误吗?
  • 对不起,实际上我无法复制他遇到的错误,所以实际上我是在问您是否遇到与 OP 相同的错误?
  • 是的,如果 dtype 是对象。这可能是因为它是 OP 使用的旧 numpy 版本。
  • 也许这就是我无法复制错误的原因!
  • 我想我可能有最新的 numpy!
【解决方案2】:

使用 python 3 而不是 2!

这肯定是anaconda环境的问题或者其他什么代码是对的,改用python3.x就好了!

【讨论】:

    【解决方案3】:
    map(float(),np.exp((-(x-m)**2)/(2*sigma)))
    

    map 接受一个函数和一个可迭代对象。 float 是一个函数,float() 是将其应用于空的结果

    In [173]: float()
    Out[173]: 0.0
    

    因此抱怨浮动不是可调用的。这不是一个函数。

    mapfloat 的正确用法是:

    In [175]: list(map(float, [1,2,3]))
    Out[175]: [1.0, 2.0, 3.0]
    

    第二个参数是您已经遇到问题的表达式。如果x 在原版中不起作用,则在本次调用中也不起作用。

    np.exp 应用于对象 dtype 数组,尝试将操作委托给数组的每个对象。

    In [174]: np.exp(np.array([[1,2],3,4]))
    ---------------------------------------------------------------------------
    AttributeError                            Traceback (most recent call last)
    <ipython-input-174-82d2b5b40b53> in <module>()
    ----> 1 np.exp(np.array([[1,2],3,4]))
    
    AttributeError: 'list' object has no attribute 'exp'
    

    但大多数对象没有exp 方法。

    原始问题的关键是理解为什么您的 x 是一个对象 dtype 数组。为什么不是浮点数组?它是如何建造的?

    另一个答案中的解决方案将对象数组转换为浮点数组在这种情况下有效,但可能不适用于更通用的对象数组。


    x 表达式的其余部分确实适用于对象 dtype 数组。一般来说,对象 dtype 数组上的数学是不确定的 - 为某事工作,而不是为其他人工作。它将任务委托给每个元素,因此-** 等基本运算符起作用。但即使它起作用,它也会变慢。

    In [177]: x =np.random.random((4,2))
    In [178]: (-(x-m)**2)/(2*sigma)
    Out[178]: 
    array([[ -8.07952334,  -8.15537868],
           [-10.37197226, -10.27704074],
           [-11.57978784, -11.18915471],
           [-10.39579226, -10.8018881 ]])
    In [179]: (-(x.astype(object)-m)**2)/(2*sigma)
    Out[179]: 
    array([[-8.079523343837689, -8.155378680249662],
           [-10.371972261358971, -10.277040739722857],
           [-11.579787836524062, -11.189154714963014],
           [-10.395792264129886, -10.80188810039029]], dtype=object)
    In [180]: np.exp((-(x.astype(object)-m)**2)/(2*sigma))
    ---------------------------------------------------------------------------
    AttributeError                            Traceback (most recent call last)
    <ipython-input-180-e040a450f8e2> in <module>()
    ----> 1 np.exp((-(x.astype(object)-m)**2)/(2*sigma))
    
    AttributeError: 'float' object has no attribute 'exp'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-12-08
      • 2018-07-02
      • 2013-09-04
      • 1970-01-01
      • 2018-03-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多