1

I have an array that is 30x30 and inside each element is an array of 400 values.

Is there an easy way to take the standard deviation of the entire 'cube' or to recast it as a 30x30x400 cube?

In [295]: data.shape
Out[295]: (30, 30)

In [296]: data[0,0].shape
Out[296]: (400,)

Doing std(data) gives an array of the shape 400 so it does not apply it to the whole thing. And taking the std of the result is not the same.

Here is an example of such an array and how to make it.

a = np.zeros([3,3]).astype(object)

for i in range(3):
for j in range(3):
a[i,j] = np.array([0,i,j])


In [364]: a
Out[364]: 
array([[array([0, 0, 0]), array([0, 0, 1]), array([0, 0, 2])],
       [array([0, 1, 0]), array([0, 1, 1]), array([0, 1, 2])],
       [array([0, 2, 0]), array([0, 2, 1]), array([0, 2, 2])]], dtype=object)

In [365]: a.shape
Out[365]: (3, 3)

In [366]: a[0,0]
Out[366]: array([0, 0, 0])
6
  • Are these NumPy arrays? If not, it seems like they should be. Then, taking the std would be simple using np.std. Commented Mar 14, 2019 at 13:56
  • They are, but its not a 30x30x400 array its a 30x30 array with an array inside. Commented Mar 14, 2019 at 13:57
  • I'd share a .npy file of the cube but I'm not sure where I could upload it. Commented Mar 14, 2019 at 14:04
  • @Coolcrab, can you share a 3x3 array with 4-element arrays inside? this would let people come up with a code that you could scale later if needed Commented Mar 14, 2019 at 15:11
  • I added the code to make one and an example array to this. Commented Mar 14, 2019 at 15:20

2 Answers 2

1

If you only care about std, here is a straightforward solution for your specific case:

np.std(np.array([i for i in a.flatten()]).flatten())

Sign up to request clarification or add additional context in comments.

Comments

0

Here is what I got after a little trial and error. Improvements are welcome

omega = x
for dims in range(len(x.shape)):
    omega = np.concatenate(omega)

y = np.reshape(omega,[3,3,4])
print(y)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.