简体   繁体   中英

Python/Numpy - How to reshape this (2,7,4) ndarray into a (7,8) ndarray without concatenating?

I am currently using np.concatenate to reshape a (n_columns,n_rows,n_positions) ndarray into a (n_rows,n_columns * n_positions) ndarray .

Because np.concatenate copies first, and because data is "contiguous", I wonder if there is no faster way with reshape to get the array I am looking for?

But whatever C , F or A order I use with reshape, I can't get the alignment I am looking for.

I am using this test data.

import pandas as pd
import numpy as np
from random import seed, randint

# Data

n_rows = 4
start = '2020-01-01 00:00+00:00'
end = '2020-01-01 12:00+00:00'

pr2h = pd.period_range(start=start, end=end, freq='2h')
seed(1)
values1 = [randint(0,10) for ts in pr2h]
values2 = [randint(20,30) for ts in pr2h]
df = pd.DataFrame({'Values1' : values1, 'Values2': values2}, index=pr2h)

# Processing

array = np.concatenate((np.full((n_rows-1,len(df.columns)), np.nan), df), axis=0)
array = array.T
shape = array.shape[:-1] + (array.shape[-1] - n_rows + 1, n_rows)

strides = array.strides + (array.strides[-1],)
array = np.lib.stride_tricks.as_strided(array, shape=shape, strides=strides)

transposed = np.concatenate(array, axis=1)     # -> the line of code I would like to change

So, because of the processing with strides , I get array as follow.

array([[[nan, nan, nan,  2.],
        [nan, nan,  2.,  9.],
        [nan,  2.,  9.,  1.],
        [ 2.,  9.,  1.,  4.],
        [ 9.,  1.,  4.,  1.],
        [ 1.,  4.,  1.,  7.],
        [ 4.,  1.,  7.,  7.]],

       [[nan, nan, nan, 27.],
        [nan, nan, 27., 30.],
        [nan, 27., 30., 26.],
        [27., 30., 26., 23.],
        [30., 26., 23., 21.],
        [26., 23., 21., 27.],
        [23., 21., 27., 20.]]])

Thanks to np.concatenate(array, axis=1) , I get the shape and value ordering I am looking for in transposed .

array([[nan, nan, nan,  2., nan, nan, nan, 27.],
       [nan, nan,  2.,  9., nan, nan, 27., 30.],
       [nan,  2.,  9.,  1., nan, 27., 30., 26.],
       [ 2.,  9.,  1.,  4., 27., 30., 26., 23.],
       [ 9.,  1.,  4.,  1., 30., 26., 23., 21.],
       [ 1.,  4.,  1.,  7., 26., 23., 21., 27.],
       [ 4.,  1.,  7.,  7., 23., 21., 27., 20.]])

Is there a way to get the same shape and value ordering without making a copy of the array?

I thank you in advance for any help. Bests,

Try this:

np.reshape(np.transpose(array,(1,0,2)),(7,8))

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