[英]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
.我目前正在使用
np.concatenate
将 (n_columns,n_rows,n_positions) ndarray
重塑为 (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?因为
np.concatenate
首先复制,并且因为数据是“连续的”,所以我想知道 reshape 是否没有更快的方法来获取我正在寻找的数组?
But whatever C
, F
or A
order I use with reshape, I can't get the alignment I am looking for.但是无论我使用 reshape 使用的
C
、 F
或A
订单,我都无法获得我正在寻找的 alignment。
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.因此,由于
strides
的处理,我得到如下array
。
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
.感谢
np.concatenate(array, axis=1)
,我得到了我在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))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.