I recently stumbled over a strange numpy behavior which I do not understand: I have a list of experiments. Each of the experiments itself is again a list of samples. So I end up with a list of lists. Experiments were conducted under various conditions, so some of them contain more samples than others. They all have in common that they contain well over 100 samples.
Now I wanted to compute the mean and standard deviation of the samples for every experiment. What works for me is
sDevPD = [np.std(x) for x in f0PD]
where I simpy iterate over all the lists in my list of experiments f0PD
. Okay, now I tried using numpy:
sDevPD = np.std(f0PD, axis = 1)
This does not work, numpy will throw IndexError: tuple index out of range
. I tried to track down the error the best I could, and I found that the numpy function throws this error only if the experiments vary in size. If I have a list of lists that are all of the same length, everything works fine. The same applies for np.mean
.
Can anybody please explain this behavior to me? I think it is absolutely legit to compute standard deviations for differently sized lists.