Np.loadtxt error

Hi,
I am trying to parse data in C2W4 by using np.loadtxt. here is what I did:
csv_load=np.loadtxt(file,delimiter=‘,’)
** labels = csv_load[0]**
** images = csv_load[1:]**

but I got an error saying:
could not convert string to float: ‘label’

How do I correct my code? I know I should do something about data type. But I don’t know exact keys.

Could someone help? Appreciate it.

1st line is the header. Please pay attention to skiprows parameter in np.loadtxt

Hi,
Thanks for your help. But as I added skiprow command, I think the file should be something like this: there are 27455 images, and each image is a row, with the first slot as a label and the rest pixel numbers. on top of these 27455 images/rows, there is a header row.

It seems the output is not aligned with my thought. Would you please point out where I made mistakes? Thank you.
Here is my code:

Your error is tied to numpy array indexing and not the way the file is read.
In numpy, the 1st index is row(s) and the 2nd index is column(s).

In your case, loader[0] returns all columns of the 0th row.
loader[1:] returns all columns of all rows starting from the 1st row till end of dataset.

Consider this:
loader[:, 1] will return 1st column for all rows. Use this hint to access arrays properly.

1 Like

Thank you for your help. I have got labels right using your hint.
I am trying to solve for images.
I colored my thoughts in code in the following picture.

It says this
image
I don’t know how I should write it. Would you please help? Appreciate it.

You don’t have to use functions like np.split.

It’s valid to slice arrays using range syntax.
For example, loader[:, 1:3] will return all rows with columns 1 and 2

Thank you very much. I get it. :partying_face: