1

I have coded mini_batch creator for miniBatchGradientDescent

The code is here:

# function to create a list containing mini-batches 
def create_mini_batches(X,y, batch_size): 
    print(X.shape, y.shape) # gives (280, 34) (280,)
    splitData=[]
    splitDataResults=[]
    batchCount=X.shape[0] // batch_size #using floor division for getting indexes integer form 
    for i in range(batchCount):
            splitData.append(X[(i) * batch_size : (i+1) * batch_size, :])
            splitDataResults.append(y[(i) * batch_size : (i+1) * batch_size, :]) # GIVES ERROR
    splitData=np.asarray(splitData)
    splitDataResults=np.asarray(splitDataResults)
    return splitData, splitDataResults, batchCount

the error says:

splitDataResults.append(y[(i) * batch_size : (i+1) * batch_size, :])
IndexError: too many indices for array: array is 1-dimensional, but 2 were indexed

I am sure that the shape is correct but it gives me an error. What is wrong?

1 Answer 1

2

try reshaping y:

print(X.shape, y.shape) # gives (280, 34) (280,)
y = y.reshape(-1, 1)

this should fix your problem, since y will become 2 dimentional

2
  • Yeah, thanks. That solved my problem. Can you explain why this solved?
    – Leo S
    Nov 9, 2020 at 21:18
  • 3
    y shape was (280,) this is a 1D NumPy array, so you can't really use [ i , j ] to index as you did in the line that raised the exception, you tried to index into two dimension of a 1D array. after doing the reshape, it becomes (280, 1) so it is a 2D array and your indexing works as expected
    – ESDAIRIM
    Nov 9, 2020 at 21:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.