如何修复'Float'对象没有属性'exp'?

问题描述 投票:1回答:3

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

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

只要x是一个矩阵。

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

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

我该如何解决这个问题?

编辑-1

进行以下编辑:

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

提出错误:

TypeError: 'float' object is not callable

编辑-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

谢谢。

python numpy math gaussian algebra
3个回答
6
投票

这可能是因为您将数组的dtype作为对象,因此将其转换为float

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'

将其转换为float,即:

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,因此修复了很多错误。


0
投票

使用python 3而不是2!

这一定是一个问题,因为anaconda环境或其他代码是正确的,只需使用python3.x!


0
投票
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数组。为什么不是浮点数组?它是如何构建的?

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


x表达式的其余部分与对象dtype数组一起使用。一般来说,对象dtype数组的数学是iffy - 为某些东西而不是为其他人工作。它将任务委托给每个元素,因此像-**这样的基本操作符可以正常工作。但即使它工作也很慢。

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'
© www.soinside.com 2019 - 2024. All rights reserved.