我在一个形状为(365,24,25(的长numpy数组中每小时采样25年。这是1975-01-01 00:00:00至1999-12-31 23:59:00 的数据
[[[0, 0.1, 0.2, ...], [0.3, 0.2, 0.2, ...], ...]]
现在我想把它展平成尺寸(1x219000(,然后把它们放进一个数据帧中,看起来像这样:
1975-01-01 00:00:00 0
1975-01-01 02:00:00 0.1
1975-01-01 03:00:00 0.2
1975-01-02 01:00:00 0.2
我只是试着像这样把它压平:
data1d = data.flatten()
df = pd.DataFrame(oneDimensionalData)
df.index = pd.date_range(start=pd.to_datetime('1975-01-01 00:00:00'), end=pd.to_datetime("1999-12-31 23:59:00"), periods=219000).to_series()
它产生了所需的向量,但我认为这个顺序是错误的,自1975年以来就没有连续的时间。
有人在压平这些3D矩阵方面更有经验吗?
使用np.transpose((重新排列维度。
import numpy as np
# test data in ydh ordering
data_ydh = np.array(range(365*24*25)).reshape((25,365,24))
data_ydh[0,0,:] # check y=d=0, the first 24 hours
Out[43]:
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23])
# ydh to dhy
data_dhy = data_ydh.transpose((1,2,0))
data_dhy[0,:,0] # d=0 and y=0
Out[47]:
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23])
# dhy to ydh
data_ydh_back = data_dhy.transpose((2,0,1))
data_ydh_back[0,0]
Out[49]:
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23])