Slicing numpy arrays

Viewed 30
mean = [0, 0]
cov = [[1, 0], [0, 100]] 

gg = np.random.multivariate_normal(mean, cov, size = [5, 12])

I get an array which has 2 columns and 12 rows, i want to take the first column which will include all 12 rows and convert them to columns. What is the appropriate method for sclicing and how can one convet the result to columns? To be precise, looking at the screen (the second one) one should take all 0 column columns and convert them in a normal way from the left to the right

the results should be like this (the first screen)enter image description here

enter image description here

1 Answers

The problem is that your array gg is not two- but three-dimensional. So, what you need is in fact the first column of each stacked 2D array. Here is an example:

import numpy as np

x = np.random.randint(0, 10, (3, 4, 5))
x[:, :, 0].flatten()

The colon in slicing means "all values in this dimension". So, x[:, :, 0] means "all values in the the first dimension and all values in the second dimension and with third dimension fixed on index 0". This results in a two-dimensional array, which you have to flatten additionally.

Related