[SciPy-User] optimize.leastsq - Value Error: The truth value of an array with more than one element is ambiguous

Zhe Wang 3njoywind at gmail.com
Sat May 15 11:06:04 EDT 2010


Josef:

Thanks,
my example is just for test, it is not always have a fixed start, e.g

>>> T = np.array([1995,1996,1997,1998,1999])
>>> E = np.array([14,12,11,15,12])
>>> T-E
array([1981, 1984, 1986, 1983, 1987])

so, if I define the function:

def func(x):
    #....
    v = np.arange(...)
    Y = np.cumsum((a*(1+p)**v)*I(v))
    return Y

when I call leastsq(func, [1,0]), v should change as the element of T
change, e.g.

when t(one element of T) is 1995,v = np.arange(1981, 1995)
when t is 1996, v = np.arange(1984, 1996)
......

this troubles me so much.

Zhe Wang



On Sat, May 15, 2010 at 9:08 PM, <josef.pktd at gmail.com> wrote:

> On Sat, May 15, 2010 at 8:49 AM, Zhe Wang <3njoywind at gmail.com> wrote:
> > Josef:
> > Thanks for your reply:)
> > Actually I want to fit this equation:
> > Y(t) = sigma(v=t-e(t), t)[(a*(1+p)**v)*I(v)]
> > I got {t} and {Y(t)} and a, p are parameters. e(t) and I(v) can be
> > calculated by e() and I().
>
> Do you always have a fixed start date as in your example
>
> >>> T = np.array([1995,1996,1997,1998,1999])
> >>> E = np.array([10,11,12,13,14])
> >>> T-E
> array([1985, 1985, 1985, 1985, 1985])
>
> so that always v =range(T0, T+1)     with fixed T0=1985
>
> this would make it easier to work forwards than backwards, e.g. something
> like
> v = np.arange(...)
> Y = np.cusum((a*(1+p)**v)*I(v))
>
> Josef
>
>
>
>
> > I rewrote my code like this:
> >
> ----------------------------------------------------------------------------------------------
> > from scipy.optimize import leastsq
> > import numpy as np
> > def Iv(t):
> >     return 4
> > def Yt(x, et):
> >     a, pa = x
> >     sum = np.array([0,0,0,0,0])
> >     for i in range(0, len(et)):
> >         for j in range(0, et[i]):
> >             v = T[i] - et[i] + j
> >             sum[i] += a*(1+pa)**(v)*Iv(v)
> >     return sum - Y
> > T = np.array([1995,1996,1997,1998,1999])
> > Y =
> >
> np.array([639300.36866,664872.383407,691467.278743,719125.969893,747891.008688])
> > E = np.array([10,11,12,13,14])
> > r = leastsq(Yt, [1,0], args = (E), maxfev=10000000)
> > A, Pa = r[0]
> > print "A=",A,"Pa=",Pa
> >
> ----------------------------------------------------------------------------------------------
> > the output is:
> > A= 1.0 Pa = 0.0
> >
> ----------------------------------------------------------------------------------------------
> > I don't think it is correct. Hope for your guidence.
> > On Sat, May 15, 2010 at 7:02 PM, <josef.pktd at gmail.com> wrote:
> >>
> >> On Sat, May 15, 2010 at 5:25 AM, Zhe Wang <3njoywind at gmail.com> wrote:
> >> > Traceback (most recent call last):
> >> >   File "D:\Yt.py", line 31, in <module>
> >> >     r = leastsq(residuals, [1,0], args=(Y,T), maxfev=10000000)
> >> >   File "D:\Python26\lib\site-packages\scipy\optimize\minpack.py", line
> >> > 266,
> >> > in leastsq
> >> >     m = check_func(func,x0,args,n)[0]
> >> >   File "D:\Python26\lib\site-packages\scipy\optimize\minpack.py", line
> >> > 12,
> >> > in check_func
> >> >     res = atleast_1d(thefunc(*((x0[:numinputs],)+args)))
> >> >   File "D:\Yt.py", line 26, in residuals
> >> >     return y - Yt(x, p)
> >> >   File "D:\Yt.py", line 20, in Yt
> >> >     for i in range(0, Et(x)):
> >> >   File "D:\Yt.py", line 11, in Et
> >> >     if t == 1995:
> >> > ValueError: The truth value of an array with more than one element is
> >> > ambiguous. Use a.any() or a.all()
> >> >
> >> >
> ---------------------------------------------------------------------------------------------------------
> >> >
> >> > When running the following code:
> >> >
> >> >
> >> >
> --------------------------------------------------------------------------------------------
> >> >
> >> > from scipy.optimize import leastsq
> >> > import numpy as np
> >> >
> >> > def Iv(t):
> >> >     if t == 1995:
> >> >         return t + 2
> >> >     else:
> >> >         return t
> >> >
> >> > def Et(t):
> >> >     if t == 1995:
> >> >         return t + 2
> >> >     else:
> >> >         return t
> >> >
> >> > def Yt(x, p):
> >> >     a, pa = p
> >> >     sum = 0
> >> >
> >> >     for i in range(0, Et(x)):
> >> >         v = x - et + i
> >> >         sum += a*(1+p)**(v)*Iv(v)
> >> >     return sum
> >> >
> >> > def residuals(p, y, x):
> >> >     return y - Yt(x, p)
> >> >
> >> > T = np.array([1995,1996,1997,1998,1999])
> >> > Y =
> >> >
> >> >
> np.array([639300.36866,664872.383407,691467.278743,719125.969893,747891.008688])
> >> >
> >> > r = leastsq(residuals, [1,0], args=(Y,T), maxfev=10000000)
> >> > A, Pa = r[0]
> >> > print "A=",A,"Pa=",Pa
> >> >
> >> >
> >> >
> ----------------------------------------------------------------------------------------------
> >> >
> >> > I know the error occurs when I compare t like: "if t == 1995",but I
> have
> >> > no
> >> > idea how to handle it correctly.
> >>
> >> try the vectorized version of a conditional assignment, e.g.
> >> np.where(t == 1995, t, t+2)
> >>
> >> I didn't read enough of your example, to tell whether your Yt loop can
> >> be vectorized with a single sum, but I guess so.
> >>
> >> optimize leastsq expects an array, so residuals (and Yt) need to
> >> return an array not a single value, maybe np.cusum and conditional or
> >> data dependent slicing/indexing works
> >>
> >> Josef
> >>
> >>
> >> >
> >> > Any help would be greatly appreciated.
> >> >
> >> > Zhe Wang
> >> >
> >> > _______________________________________________
> >> > SciPy-User mailing list
> >> > SciPy-User at scipy.org
> >> > http://mail.scipy.org/mailman/listinfo/scipy-user
> >> >
> >> >
> >> _______________________________________________
> >> SciPy-User mailing list
> >> SciPy-User at scipy.org
> >> http://mail.scipy.org/mailman/listinfo/scipy-user
> >
> >
> > _______________________________________________
> > SciPy-User mailing list
> > SciPy-User at scipy.org
> > http://mail.scipy.org/mailman/listinfo/scipy-user
> >
> >
> _______________________________________________
> SciPy-User mailing list
> SciPy-User at scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20100515/8dcb2bbf/attachment.html>


More information about the SciPy-User mailing list