NumPy ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() leastsq

匿名 (未验证) 提交于 2019-12-03 09:10:12

问题:

from sympy import * from scipy import * from scipy.integrate import quad import scipy.optimize as optimize import numpy as np import collections import math from scipy.optimize import leastsq  file= DATA+'Union21.dat' with open(file, "r") as f:     data0=[(float(v[1]),float(v[2]), float(v[3])) for v in [x.split() for x in f.readlines()][1:]] #print data0  z=np.array([float(t[0]) for t in data0]) mu=np.array([float(t[1]) for t in data0]) dmu=np.array([float(t[2]) for t in data0]) c=3*10^8  def calka(x, OmM):     return 1./math.sqrt(OmM*(1.+x)**3 + (1.-OmM))  def xlambda(p,xup):      H0=p     calka1 = quad(calka, 0., xup, args=(p[0]))[0]     mu_obs = 5.*math.log(c*calka1/p[1]) + 25      return mu_obs   def residuals(p, xup,y,dmu):     return ((y-xlambda(p,xup))/dmu)**2  leastsq(residuals,(0.25, 70), args=(z, mu, dmu)) 

Thank you for your answer but now there was a problem:

--------------------------------------------------------------------------- TypeError                                 Traceback (most recent call     last) TypeError: Cannot cast array data from dtype('complex128') to     dtype('float64') according to the rule 'safe'  --------------------------------------------------------------------------- error                                     Traceback (most recent call     last) <ipython-input-38-00c118ea80ce> in <module>() ----> 1 leastsq(residuals,[0.25, 70], args=(z, mu, dmu))  /opt/anaconda/envs/np18py27-1.9/lib/python2.7/site-packages/scipy    /optimize/minpack.pyc in leastsq(func, x0, args, Dfun, full_output,     col_deriv, ftol, xtol, gtol, maxfev, epsfcn, factor, diag) 377             maxfev = 200*(n + 1) 378         retval = _minpack._lmdif(func, x0, args, full_output, ftol, xtol, --> 379                                  gtol, maxfev, epsfcn, factor, diag) 380     else: 381         if col_deriv:  error: Result from function call is not a proper array of floats. 

I try change dtype array from complex128 to float64 but it didn't help :(

I looking for maybe np.interp but i don't know which array a i must change

Can you any idea what i have to do?

回答1:

The error message can be reproduced like this:

import numpy as np import scipy.integrate as integrate  xup = np.random.random(10)  def calka(x, OmM):     return 1./math.sqrt(OmM*(1.+x)**3 + (1.-OmM))   # Passing a scalar value, 10, for the upper limit is fine: integrate.quad(calka, 0., 10, args=(0.25,)) # (2.3520760256393554, 1.9064918795817483e-12)  # passing a vector, xup, raises a ValueError: integrate.quad(calka, 0., xup, args=(0.25,)) # ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() 

Now, in your code, z is an array:

z=np.array([float(t[0]) for t in data0]) 

z gets passed to residuals:

leastsq(residuals,(0.25, 70), args=(z, mu, dmu)) 

Inside residuals, xup gets assigned the value z.

def residuals(p, xup,y,dmu):     return ((y-xlambda(p,xup))/dmu)**2 

Inside xlambda, xup -- the vector -- is passed directly to quad:

def xlambda(p,xup):      H0=p     calka1 = quad(calka, 0., xup, args=(p[0]))[0] 

Hence the ValueError.


Presumably, you'd want xlambda to be called once for each value in xup. So you could fix the problem by using

def residuals(p, xup, y, dmu):     xl = np.array([xlambda(p, x) for x in xup])     return ((y-xl)/dmu)**2 


回答2:

if (b != Inf and a != -Inf): 

This is trying to perform a Python if operation, but (b != Inf and a != -Inf) is returning an array of boolean values, rather than one True/False value. If this was your own code you could add np.any(...) to combine the values. But in this context I suspect that problem is that either a or b is an array, when it should be a scalar.

Can you trace those values back up the calling stack?

There have been many SO questions regarding this type of error message.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!