[英]loop of ufunc does not support argument 0 of type float which has no callable exp method
I am having trouble with an optimization that used to work with a previous version of python...我在使用以前版本的 python 的优化时遇到问题...
I have a function fp = lambda c, x: (c[0])+ (c[1]*((1- np.exp(-x/c[4]))/(x/c[4])))+ (c[2]*((((1-np.exp(-x/c[4]))/(x/c[4])))- (np.exp(-x/c[4]))))+ (c[3]*((((1-np.exp(-x/c[5]))/(x/c[5])))- (np.exp(-x/c[5]))))
我有一个 function
fp = lambda c, x: (c[0])+ (c[1]*((1- np.exp(-x/c[4]))/(x/c[4])))+ (c[2]*((((1-np.exp(-x/c[4]))/(x/c[4])))- (np.exp(-x/c[4]))))+ (c[3]*((((1-np.exp(-x/c[5]))/(x/c[5])))- (np.exp(-x/c[5]))))
and I need to minimize the error e = lambda c, x, y: (((fp(c,x)-y)**2).sum())
我需要最小化错误
e = lambda c, x, y: (((fp(c,x)-y)**2).sum())
using the initial parameter values p0 = np.array([0.01,0.01,0.01,0.01,0.01,1.00,1.00])
使用初始参数值
p0 = np.array([0.01,0.01,0.01,0.01,0.01,1.00,1.00])
basically p = optimize.fmin(e, p0, args=(x,y))
基本上
p = optimize.fmin(e, p0, args=(x,y))
were x and y are np.arrays (14,) each. x 和 y 分别是 np.arrays (14,)。
So, this used to work but now it throws this error TypeError: loop of ufunc does not support argument 0 of type float which has no callable exp method
所以,这曾经可以工作,但现在它抛出了这个错误
TypeError: loop of ufunc does not support argument 0 of type float which has no callable exp method
I've done some research and it seems that there is a problem related to np.exp()
and some versions of Numpy... Actually this problem appeared when I updated Python and Numpy as well to 3.7 and 1.18.1 repectively.我做了一些研究,似乎存在与
np.exp()
和 Numpy 的某些版本相关的问题......实际上,当我更新 Python 和 Z55F00E1DAA52B7CB5C5C5BC865E79DBC1Z1 时出现了这个问题。1 和 3 代表。
Any thoughts?有什么想法吗?
The issue may be caused by the fact the elements of your numpy array are not of type int.问题可能是由于您的 numpy 数组的元素不是 int 类型。
This can be fixed by turning your float array into an int one, at the small cost of losing a bit of accuracy.这可以通过将浮点数组转换为 int 数组来解决,但代价是损失了一点准确性。 This fixed the issue for me.
这为我解决了这个问题。
import numpy as np
y_int = np.array(y_float, dtype=int)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.