简体   繁体   中英

Flatten a numpy ndarray array elements to rows of features

I have a 4D numpy array in which each column represents 1 quantity and the rows are a statistics derivatives of the quantities, like.

[mean mean mean
 std  std  std
 med  med  med]

Let's say column 1 represents speed column 2 acceleration etc. I would like to flatten each column of the array for all the quantities available into a row of features forming:

mean std med mean std med mean std med ...

To clarify my concern, I give the following MWE :

input_shape = (1,3,4)
n_sample =20

X = np.random.randint(1, 10, size=(n_sample, )+ input_shape)
X.shape
(20,1,3,4)

So the first 2 entiries of X are:

X[:2]
array([[[[1, 7, 7, 8],
         [9, 3, 4, 1],
         [1, 1, 7, 1]]],


       [[[2, 8, 9, 4],
         [8, 4, 2, 7],
         [3, 9, 8, 4]]]])

The columns represent quantities q1, q2, q3, q4 and the rows represents the statistics: mean, std, med for each of the arrays.

Then I want to flatten these statistics for each quantity to row of features so that the end result is like this:

  q1_mean q1_std q1_med q2_mean q2_std q2_med q3_mean q3_std q3_med q4_mean q4_std q4_med
     1      9       1      7      3      1      7       4       7      8      1       1
     2      8       3      8      4      9      9       2       8      4      7       4  

So each array is a single observation. So I can easily transform to a dataframe, adding appropriate column head(name).

If I understood correctly, this should do what you want:

np.transpose(X,(0,1,3,2)).reshape(X.shape[0],-1)

Note that transposing axis 2 and 3 is necessary since reshape will be done in a row-major (C-style) order.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM