Numpy and Reshape are most puzzling ! and hence I'm stuck

The more I try, the more it gets weirder.
Help me pls

Does this topic help?

1 Like

Please pay attention to the numpy way of indexing rows and columns. The dataset you are dealing with has 2 dimensions. In this case, the 0th index represents row and 1st index represents column.

See this tutorial on numpy array indexing.

Sure sir. Will do. Thank u so much sir. I know thats where I’m going wrong…


@balaji.ambresh thank u sir…
I’m finally somewhere close
but the expected output has 3 cols(27455,28,28) whereas i get it as (27455,28)…Hmmm

Please pick your way of parsing text.

  1. Choice 1: np.loadtxt returns an nd-array of the specified data type. This array represents the entire file content. You should slice this array to get the labels and image pixels.
  2. Choise 2: csv.reader returns text for each row as a list. You have to do the following:
    a. Slice the columns that correspond to label and image.
    b. Convert each of label and image from string to float
    c. Once all rows are exhausted, create 2 nd-arrays for images and labels.

As far as your error is concerned, the problem is inside parse_data_from_input. labels has 1 column and has as many rows as the number of images i.e. rows in the original dataset (without the header). So, reshaping labels to (28, 28) is incorrect. labels as on line 23 in the stacktrace is in the right shape.

I did the slicing for loadtxt.Can u ls check sir;

[code removed - moderator]

Please don’t post code on public topic. It’s okay to share stacktrace though.

You’ve loaded the text correctly. However, the slicing for images is incorrect. The 0th column consists of the label (You got that right).
Image pixels are the rest of the columns. So, you’ll need from index 1 till end of column. Once you slice the image pixels correctly, reshape images such that each image is (28, 28) in dimension. See the expected output for hints.

1 Like

Sorry sir. I thought I was sending the code as a DM didn’t realize.
Finally got the output and thrilled.
For those struggling with the images part., reshape by setting the first parameter to -1.
All the best
Thanks again