I have such a numpy array:
And I want to shift this array with N so I can get a new array, such that first 4 rows will be the first 4 rows from the beginning array, and the below rows will be the rows from the previous step.
I dont know how to explain it correctly, but look at the example where I shift with 1 (not the whole array, but I would like to do this for the whole array):
And when I shift with 2 (not the whole array, but I would like to do this for the whole array):
And if I shift with 3, I will have 12 zeros in first column, and 3 previous rows ..
And here's the code:
import numpy as np
import numpy
def shift_one(arr):
arrT = arr.transpose()
arrT_shape = arrT.shape[0]
col1 = arrT[:, [0]]
prev1 = numpy.zeros([arrT_shape, 1])
x1 = numpy.vstack([col1, prev1])
col2 = arrT[:, [1]]
prev2 = arrT[:, [0]]
x2 = numpy.vstack([col2, prev2])
col3 = arrT[:, [2]]
prev3 = arrT[:, [1]]
x3 = numpy.vstack([col3, prev3])
col4 = arrT[:, [3]]
prev4 = arrT[:, [2]]
x4 = numpy.vstack([col4, prev4])
col5 = arrT[:, [4]]
prev5 = arrT[:, [3]]
x5 = numpy.vstack([col5, prev5])
# ... and so on, until we have index [23] and [22] and x24
res = numpy.hstack([x1, x2, x3, x4, x5])
return res
def shift_two(arr):
arrT = arr.transpose()
arrT_shape = arrT.shape[0]
new_size = 2 * arrT_shape
col1 = arrT[:, [0]]
prev1 = numpy.zeros([new_size, 1])
x1 = numpy.vstack([col1, prev1])
col22 = arrT[:, [1]]
col21 = arrT[:, [0]]
prev2 = numpy.zeros([arrT_shape, 1])
x2 = numpy.vstack([col22, col21, prev2])
col32 = arrT[:, [2]]
col31 = arrT[:, [1]]
col30 = arrT[:, [0]]
x3 = numpy.vstack([col32, col31, col30])
col42 = arrT[:, [3]]
col41 = arrT[:, [2]]
col40 = arrT[:, [1]]
x4 = numpy.vstack([col42, col41, col40])
col52 = arrT[:, [4]]
col51 = arrT[:, [3]]
col50 = arrT[:, [2]]
x5 = numpy.vstack([col52, col51, col50])
# ... and so on, until we have index [23], [22], [21] and x24
res = numpy.hstack([x1, x2, x3, x4, x5])
return res
arr1 = np.array([[0, 2, 0, 324],
[1, 2, 0,324],
[2, 2, 0, 324],
[3, 2, 0, 324],
[4, 2, 0, 324],
[5, 2, 0, 324],
[6, 2, 0, 324],
[7, 2, 0, 324],
[8, 2, 0, 324],
[9, 2, 0, 324],
[ 10, 2, 0, 324],
[ 11, 2, 0, 324],
[ 12, 2, 0, 324],
[ 13, 2, 0, 324],
[ 14, 2, 0, 324],
[ 15, 2, 0, 324],
[ 16, 2, 0, 324],
[ 17, 2, 0, 324],
[ 18, 2, 0, 324],
[ 19, 2, 0, 324],
[ 20, 2, 0, 324],
[ 21, 2, 0, 324],
[ 22, 2, 0, 324],
[ 23, 2, 0, 324]])
print(arr1)
print('\n\n')
one = shift_one(arr1)
print(one)
print('\n\n')
two = shift_two(arr1)
print(two)
Basically, I have a problem how to write a function, which will shift it with a given N ... I know how to write it step by step as I did, but I have a problem transforming it into something more usable. Thank you.
So again, an example for array with 5 columns (the original array has 24 columns):



