jeudi 22 avril 2021

IndexError: index 9 is out of bounds for axis 0 with size 8

I would like to delete a Numpy Array element in a for loop if it exceedes my limits. If an element is out of limits and the numpy delete function deletes it, the end of the range of my for loop, n, decreases by 1. but inside the for loop it doesn't decrease and I get the following error. How can I remove elements from numpy array without breaking my for loop?

IndexError: index 9 is out of bounds for axis 0 with size 8

n=9
ucl=0 + (3 * 1/ np.sqrt(n))
lcl=0 - (3 * 1/ np.sqrt(n))
x=np.random.normal(0,1,size=n)
for i in range(0,n):
  if (xbar[i]>ucl) | (xbar[i]<lcl):
    xbar=np.delete(xbar, i)
    n=n-1 

Aucun commentaire:

Enregistrer un commentaire