Week 4 Programming

In the get_data function, I keep getting different variations of the error “ValueError: cannot reshape array of size 1 into shape (28,28)”

If I try reshape outside the for loop, it gives me the same message with the file size instead.
I am totally lost on how to solve this… please help! Code below.

with open(filename) as training_file:

    imgs = []
    labels = []

    next(training_file, None)
    for row in training_file:
        label = row[0]
        data = row[1:]
        img = np.array(data).reshape((28, 28))


    images = np.array(imgs).astype(float)
    labels = np.array(labels).astype(float)
    return images, labels

Hi @JeainnyKim,

I believe your issue is in the way that you are reading the file. Data in this file are separated by a character, a comma in this particular case. When you read the file, in the way you are doing, you bring the whole line of text. In your case, row[0] is the first character in the row and row[1:] will bring the rest of the line.

You have two options here. Either you read the line and separate each element to list or you rather use csv.reader.

I prefer the second option as it’s widely used - you can google for some help on how to use it.

Hope it helps!