python - 设置两个数组之间的时间步延迟

时间:2015-09-08 14:26:41

标签: python numpy machine-learning neural-network deep-learning

seq_u和seq_y是三维数组:

seq_u  float32 (10L, 10L, 1L) 
seq_y  float32 (10L, 10L, 1L) 

我想在seq_u和seq_y之间设置一个时间步延迟(即seq_y(1)= seq_u(0),seq_y(2)= seq_u(1),....)

image1

这是我的代码:

seq_y[:, 1:, 0] = seq_u[:, :-1, 0]

它只添加零作为第一个值。怎么修?另外,如何设置2个时间步延迟?谢谢!

enter image description here

1 个答案:

答案 0 :(得分:0)

以下是使用np.pad移动数组的方法:

import numpy as np

u = np.arange(1, 17).reshape(4, 4)
print u

[[ 1  2  3  4]
 [ 5  6  7  8]
 [ 9 10 11 12]
 [13 14 15 16]]

y1 = np.pad(u, ((0, 0), (1, 0)), mode='constant')[:, :-1]
print y1

[[ 0  1  2  3]     # each row shifted one to the left
 [ 0  5  6  7]
 [ 0  9 10 11]
 [ 0 13 14 15]]

y2 = np.pad(u, ((0, 0), (3, 0)), mode='constant')[:, :-3]
print y2

[[ 0  0  0  1]    # each row shifted 3 to the right
 [ 0  0  0  5]
 [ 0  0  0  9]
 [ 0  0  0 13]]


y3 = np.pad(u, ((2, 0), (0, 0)), mode='constant', constant_values=(999,))[:-2, :]
print y3

[[999 999 999 999]   # each column shifted down 2
 [999 999 999 999]   # new spaces filled with 999
 [  1   2   3   4]
 [  5   6   7   8]]