简体   繁体   English

python 中的“具有多个元素的数组的真值不明确”

[英]“The truth value of an array with more than one element is ambiguous” in python

this is how I got the two arrays (array 1 and array2) for my function:这就是我为我的 function 获得两个 arrays(数组 1 和数组 2)的方式:

x = np.arange(-5, 5,0.01)
prob=stats.norm.pdf(x,0,1)
prob_array=numpy.array(prob).reshape(1000,1) #array1
x_tran=m.transpose()
x_tran_array=array(x_tran)
mu_array=array(mu)           # mu is stock return 
mu_array1=numpy.array(mu_array).reshape(54966,1)
sigma_array=array(sigma)     #sigma is the historical volatility
sigma_array1=numpy.array(sigma_array).reshape(54966,1)
mu1_mat=mat(ones((1,1000)))  #for matrix calculation
original_x=mu_array1*mu1_mat+sigma_array1*x_tran_array #array2

I defined a function:我定义了一个 function:

def TK_value(z,p):
    if z >= 0:
        utility=z**0.88
        prob=(p**0.61)/(p**0.61+(1-p)**0.61)**(1/0.61)
    else:
        utility= -2.25*(-z)**0.88
        prob=(p**0.69)/(p**0.69+(1-p)**0.69)**(1/0.69)
    return utility*prob


tks=TK_value(original_x,prob_array)

I have two arrays with original_x with shape((54966, 1000) and prob_array with shape (1000,1). I want to use original_x as z and prob_array as p in this function.我有两个 arrays,original_x 的形状((54966, 1000)和 prob_array 的形状(1000,1)。我想在这个 function 中使用 original_x 作为 z 和 prob_array 作为 p。

But the error is:但错误是:

ValueError: The truth value of an array with more than one element is ambiguous. ValueError:具有多个元素的数组的真值不明确。 Use a.any() or a.all()使用 a.any() 或 a.all()

Welcome to SO: The problem seems to be this line: if z >= 0: If you use the '>'/'<' operator on an array it will return the following:欢迎来到 SO:问题似乎出在这一行: if z >= 0:如果您在数组上使用 '>'/'<' 运算符,它将返回以下内容:

>>> import numpy as np
>>> a = np.array([1,2,3])
>>> a > 2
array([False, False,  True])

This array can't be converted to bool by default, you have to be more specific, for example by using any() to test if atleast one element falls under the given condition.默认情况下,此数组无法转换为 bool,您必须更具体,例如使用 any() 来测试是否至少一个元素属于给定条件。 Numpy arrays can do it like this: https://numpy.org/doc/stable/reference/generated/numpy.ndarray.any.html . Numpy arrays can do it like this: https://numpy.org/doc/stable/reference/generated/numpy.ndarray.any.html .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 具有多个元素的数组的真值是模棱两可的Python错误 - The truth value of an array with more than one element is ambiguous Python error 具有多个元素的数组的真值是模棱两可的错误? Python - The truth value of an array with more than one element is ambiguous error? python “具有多个元素的数组的真值不明确”错误 [Python] - "The truth value of an array with more than one element is ambiguous" error [Python] python-&#39;具有多个元素的数组的真值不明确&#39;-什么真值? - python - 'the truth value of an array with more than one element is ambiguous' - what truth value? 值错误:具有多个元素的数组的真值不明确 - Value error: The truth value of an array with more than one element is ambiguous 具有多个元素的数组的真值不明确 - Truth value of an array with more than one element is ambiguous 具有多个元素的数组的真值是模棱两可的错误? - The truth value of an array with more than one element is ambiguous error? 具有多个元素的数组的真值是不明确的(数字的) - The truth value of an array with more than one element is ambiguous (numerical) 错误:具有多个元素的数组的真值不明确 - Error: The truth value of an array with more than one element is ambiguous 具有多个元素的数组的Pyplot真值不明确 - Pyplot truth value of an array with more than one element is ambiguous
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM