Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.8k views
in Technique[技术] by (71.8m points)

python - 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...

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

and I need to minimize the error 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])

basically p = optimize.fmin(e, p0, args=(x,y))

were x and y are np.arrays (14,) each.

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

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.

Any thoughts?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The issue may be caused by the fact the elements of your numpy array are not of type int.

This can be fixed by turning your float array into an int one, at the small cost of losing a bit of accuracy. This fixed the issue for me.

import numpy as np

y_int = np.array(y_float, dtype=int)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...