1

I want to get n rows from x_train and y_train in for loop using zip() each time. So the code below is what i have tried to do.At each iteration i update prev batch and next batch so i will get [0-5],[5-10],... rows from both 2d numpy arrays.

batch_size = 3
next_b = batch_size
prev_b = 0

//sample input
x_train = [ [0,1,2],[3,4,5],[6,7,8],[9,10,11],[12,13,14] ]
y_train = [ [3],[6],[9],[12],[15] ]    

for X,y in zip(x_train,y_train)[prev_b:next_b]:
    print(X,y)

    //prev_b = 0, next_b = 3, so i want to get below values at first iter
    //X => [[0,1,2],[3,4,5],[6,7,8]]
    //y => [ [3],[6],[9] ]

    prev_b = next_b //-> prev_b = 3, for the next iteration
    next_b += batch_size //-> next_b = 6, for the next iteration

Any help is welcome.

Marios Nikolaou
  • 1,326
  • 1
  • 13
  • 24

1 Answers1

0

Here you go, just slice your lists with properly defined indices:

x_train = [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11], [12, 13, 14]]
y_train = [[3], [6], [9], [12], [15]]
batch_size = 3

for loop_number, start in enumerate(range(0, len(x_train), batch_size)):
    print(f"loop {loop_number}")
    end = start + batch_size
    X = x_train[start:end]
    y = y_train[start:end]
    print(f"X equals {X}")
    print(f"y equals {y}\n")

Result:

loop 0
X equals [[0, 1, 2], [3, 4, 5], [6, 7, 8]]
y equals [[3], [6], [9]]

loop 1
X equals [[9, 10, 11], [12, 13, 14]]
y equals [[12], [15]]
arnaud
  • 3,293
  • 1
  • 10
  • 27