How to fix 'Float' object has no attribute

2020-04-17 18:56发布

问题:

I have the following Gaussian equation in Python:

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

Provided that x is an matrix.

However, the equation won't run, and I get the following error:

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

How can I solve this issue?

EDIT-1

Making the following edit:

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

Raised the error:

TypeError: 'float' object is not callable

EDIT-2

This is a sample of the value 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

Thanks.

回答1:

It maybe because you have your array's dtype as object so convert it to float i.e

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)) 

This will lead to :

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

Convert it to float i.e:

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]])

This also depends on the numpy version you are using. You should also try updating numpy, so many bugs have been fixed.



回答2:

Use python 3 instead of 2!

This must be a problem due to anaconda environment or something else the code is right , just use python3.x instead!



回答3:

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

map takes a function and an iterable. float is a function, float() is the result of applying that to nothing

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

Hence the complaint that a float is not an callable. It's not a function.

A proper use of map and float is:

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

The 2nd argument is the expression which you are already having problems with. If x does not work in the original, it won't work in this call.

np.exp applied to an object dtype array, tries to delegate the action to each object of the array.

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'

But most objects don't have an exp method.

The key to the original problem is understanding why your x is an object dtype array. Why isn't a float array? How was it constructed?

The solution in the other answer, which converts the object array to a float array works in this case, but might not with a more general object array.


The rest of the x expression does work with object dtype array. In general math on an object dtype array is iffy - working for something, not for others. It delegates the task to each element, so basic operators like - and ** work. But even when it works it is slower.

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'