-1

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?

  • It runs for me on `1.18.2` (with overflow warning is `x,y` are too large). – hpaulj Apr 14 '20 at 16:14
  • 1
    Your error is produced by an expression like `np.exp([1,None])`, in other words when the argument is an object dtype array. – hpaulj Apr 14 '20 at 16:29

2 Answers2

0

Or I just found that I was getting that error because I had inserted an integer into what was otherwise an array of floats, no need to sacrifice accuracy, just turned the one integer into a float and its working

-1

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)
Amar Mesic
  • 69
  • 6