I have data formatted as a 3-dimensional array, and I am performing a regression for each element along one of the axes. The following code works as I expect and returns a list of the regression slopes.
import numpy as np
from scipy import stats
import numpy.ma as ma
#make array
np.random.seed(0)
array = np.random.random((4,3,2))
def regress_slope(array):
N=array.shape[0]
alpha=0.9
y = array[:,:,1]
x = array[:,:,0]
result = [stats.mstats.theilslopes(y[i,...],x[i,...],alpha)[0] for i in range(0,N)]
return result
result = regress_slope(array)
list(result)
print(result)
My "real" data includes invalid values and I have defined a threshold (<0.1) and tried to mask these values from the array. However, when I use the masked array it throws this error:
array2 = ma.masked_less_equal(array, 0.1)
result2 = regress_slope(array2)
list(result2)
IndexError: index 0 is out of bounds for axis 0 with size 0
I am not sure what this error message means, but I think it might be because there are not enough unmasked values to compute the regression? If this is the case, how could I adjust the code to return nan in the result?
regress_slope(array)but in the secondtheil_slope(array2)? I'm assuming that's a typo and you mean to callregress_slope(array2)?