如何修复"Float"对象没有属性"exp"? [英] How to fix 'Float' object has no attribute 'exp'?
本文介绍了如何修复"Float"对象没有属性"exp"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在Python中具有以下高斯方程:
I have the following Gaussian equation in Python:
numpy.exp((-(x-m)**2)/(2*sigma))
提供x
是矩阵.
但是,等式无法运行,并且出现以下错误:
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
进行以下
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
谢谢.
推荐答案
可能是因为您将数组的dtype作为对象,所以将其转换为float
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))
这将导致:
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,因此已修复了许多错误.
This also depends on the numpy version you are using. You should also try updating numpy, so many bugs have been fixed.
这篇关于如何修复"Float"对象没有属性"exp"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文