Implement a batch iterator that produces batches from a numpy array X and an optional numpy array y. The function should produce batches of a specified size. If y is provided, each batch is an (X_batch, y_batch) pair; otherwise each batch is an X_batch only. Returning a list of batches or yielding them from a generator are both acceptable — tests consume the result with list().
X = np.array([[1, 2],
[3, 4],
[5, 6],
[7, 8],
[9, 10]])
y = np.array([1, 2, 3, 4, 5])
batch_size = 2
batch_iterator(X, y, batch_size)[[[[1, 2], [3, 4]], [1, 2]],
[[[5, 6], [7, 8]], [3, 4]],
[[[9, 10]], [5]]]The dataset X contains 5 samples, and we are using a batch size of 2. Therefore, the function will divide the dataset into 3 batches. The first two batches will contain 2 samples each, and the last batch will contain the remaining sample. The corresponding values from y are also included in each batch.