-3

How can I solve this error:

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

This is the line with the error:

Kz=(2.01*(max(4.75,z)/zg)**(2/a)*kz_StdRnd[j])

Python:

n_samples=20000 
a1= np.random.uniform(0,1,(n_samples,1))
a10 = np.linspace(1,20000,num=20000)-1+a1/n_samples
kz_StdRnd = 1+0.16*norm.ppf(a10,0.0,1.0)
kz_StdRnd = kz_StdRnd[np.random.permutation(n_samples)]

for j in range(n_samples):
    a=9.5 
    zg=274.32 
    Kz=(2.01*(max(4.75,z)/zg)**(2/a)*kz_StdRnd[j])
Ryan M
  • 18,333
  • 31
  • 67
  • 74
saeed
  • 29
  • 1
  • 4
  • 1
    It's really hard to debug when you have one giant equation on one line. If you break it up into multiple lines you'll be able to actually see which part of it is causing the error. – Random Davis Nov 05 '21 at 15:24
  • 1
    Please provide a complete [mre] - read [ask]. – AcK Nov 05 '21 at 15:26
  • the error is in the last line, for calculating KZ within the for loop. – saeed Nov 05 '21 at 15:34
  • @saeed You already said that, I know that. I was saying that that line has a lot going on, and that if you broke up that line into multiple lines, it would be possible to tell exactly what part of that line is causing the error. For example, instead of something like `x = f*g/h**(2/i)` you'd have the separate lines `a=2/i`, `b=h**a`, `c=f*g`, and `x=c/h`. – Random Davis Nov 05 '21 at 15:40
  • Everything in the last line is a scalar, except from `z`. Is that an array? `max(4.75, np.zeros((3,)))` seems to replicate your issue and is *THE* minimal reproducible example we need. Assuming `z` is an array, what were you expecting the result of that max call to be? – Reti43 Nov 05 '21 at 16:08
  • z=np.arange(0,Hn,dz+1) in which Hn is height and dz=Hn/mm. dz and z are gonna be a float number. – saeed Nov 05 '21 at 18:44
  • Well, there's your error. `max(integer, array)` will give you that error. You say `z` is going to be a float number, but what I see is that it's going to be an array of `mm` elements. Unless you meant to do something like `z[some_index]` in the last line. – Reti43 Nov 05 '21 at 22:41

1 Answers1

0

seems like you didn't use np.random.permutation properly https://numpy.org/doc/stable/reference/random/generated/numpy.random.permutation.html

This function takes an array or int and then returns ndarray.

Problem is that array is used as an index. I don't think this is normal.

Is kz_StdRnd a function? Then you should apply that function to the permutation array using map.

kz_StdRnd = list(map(kz_StdRnd, np.random.permutation(n_samples)))

instead of

kz_StdRnd = kz_StdRnd[np.random.permutation(n_samples)]

I mimicked your code and made a few modification to make it runnable without error at least. I am pretty sure the result is not what you intended though.

  1. I changed ppf function to pdf function in the same module. You might want to look at this page. Seems like there is some issue with ppf function.

https://github.com/scipy/scipy/issues/2888

  1. I changed 'z' variable in the last code to 'zg'

This is the code:

from scipy.stats import norm
import numpy as np
n_samples=20000
a1 = np.random.uniform(0,1,(n_samples,1))
a10=np.linspace(1,20000, num=n_samples)-1+a1/n_samples
#print(type(a10))
k=1+0.16*norm.pdf(a10,0,1)
p=np.random.permutation(n_samples)
#print("p: ", p)
#print("k: ", k)
k = k[p]
#print("k:", k)
for j in range(n_samples):
    a=9.5
    zg=274.32
    kz=(2.01*(max(4.75,zg)/zg)**(2/a)*k[j])
jeff pentagon
  • 796
  • 3
  • 12
  • actually, this is a code that I am converting from Matlab to Python. I am not sure if it is a function or not. the Matlab code is like this: – saeed Nov 05 '21 at 15:46
  • kz_StdRnd = kz_StdRnd(randperm(n_samples)) – saeed Nov 05 '21 at 15:46
  • `kz_StdRnd` is an array as it's created using `scipy.stats.norm.ppf`, so he's correctly using the square brackets here. – Reti43 Nov 05 '21 at 16:14
  • @Reti43 This is a very difficult question to answer. 1. what is 'z' in the last line? because there is no such variable in given code. maybe a typo of 'zg'? By the way, it seems that ppf function is not working as intended. I mimicked the code with n_samples=20 and kz_StdRnd was full of NaN except for first element of each row. That's probably why it's giving error. https://github.com/scipy/scipy/issues/2888 This link might be relevant, and I applied pdf function instead, and replaced the mysterious 'z' with 'zg'. Now the code at least 'runs' – jeff pentagon Nov 05 '21 at 19:06