When creating training data for RNN, the target label for a given input label is the input label itself but shifted by one position. Please refer to the diagram below.

Since the characters are number coded, numpy functions can be invoked. numpy.roll is particularly useful in this regard.

import numpy as np
x = np.random.randint(0,100, size=(3,3))
x

array([[29, 82, 83],
[79, 38, 12],
[21, 20, 56]])

# To shift along columns by 1
np.roll(x ,shift = 1, axis = 1)

array([[83, 29, 82],
[12, 79, 38],
[56, 21, 20]])

shift parameter determines how many positions to offset and axis argument determines the axis along which transformation has to be done.

Additional demo of numpy.roll

# To shift along rows by 1
np.roll(x ,shift = 1, axis = 0)

array([[21, 20, 56],
[29, 82, 83],
[79, 38, 12]])

# To shift along columns by 2
np.roll(x ,shift = 2, axis = 1)