Leo S
Leo S

Reputation: 339

python gives array is 1-dimensional, but 2 were indexed error

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?

Upvotes: 1

Views: 6099

Answers (1)

ESDAIRIM
ESDAIRIM

Reputation: 651

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

Upvotes: 2

Related Questions