gpt4 book ai didi

python - NumPy ValueError : The truth value of an array with more than one element is ambiguous. 使用 a.any() 或 a.all() lesssq

转载 作者:行者123 更新时间:2023-11-30 23:14:53 26 4
gpt4 key购买 nike

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

感谢您的回答,但现在有一个问题:

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

我尝试将数据类型数组从complex128更改为float64,但没有帮助:(

我可能正在寻找 np.interp 但我不知道我必须更改哪个数组

你知道我必须做什么吗?

最佳答案

错误消息可以这样重现:

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()
<小时/>

现在,在您的代码中,z 是一个数组:

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

z 被传递给 residuals:

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

residuals内,xup被赋予值z

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

xlambda内部,xup——向量——直接传递给quad:

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

因此出现了 ValueError。

<小时/>

大概,您希望为 xup 中的每个值调用一次 xlambda。所以你可以通过使用来解决这个问题

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

关于python - NumPy ValueError : The truth value of an array with more than one element is ambiguous. 使用 a.any() 或 a.all() lesssq,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28530548/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com