Vectorizing NumPy covariance for 3D array
问题 I have a 3D numpy array of shape (t, n1, n2) : x = np.random.rand(10, 2, 4) I need to calculate another 3D array y which is of shape (t, n1, n1) such that: y[0] = np.cov(x[0,:,:]) ...and so on for all slices along the first axis. So, a loopy implementation would be: y = np.zeros((10,2,2)) for i in np.arange(x.shape[0]): y[i] = np.cov(x[i, :, :]) Is there any way to vectorize this so I can calculate all covariance matrices in one go? I tried doing: x1 = x.swapaxes(1, 2) y = np.dot(x, x1) But