Course 2, Wk 4 prog. assign: Parsing the CSV file - is it possible to avoid a for-loop?

My code (see attached) seems to be parsing the CSV file correctly. I have a feeling though that there should a shorter/more efficient way to achieve the same result, perhaps without the for-loop.
I would appreciate any tips.

[snippet deleted by mentor]

You don’t need a for loop once you have read content using np.loadtxt. Use array slicing to extract columns from data read from the file.
On a related note, a single call to np.loadtxt is sufficient.

Thanks, Balaji! Should the slicing be somehow wrapped around the np.loadtxt (as it returns an ndarray)? The np.loadtxt() with the parameters I set returns:

Training images has shape: (27455, 784) and dtype: float64
Training labels has shape: (27455,) and dtype: float64
Validation images has shape: (7172, 784) and dtype: float64
Validation labels has shape: (7172,) and dtype: float64

That’s why I resorted, for a lack of a better solution, to the for-loop in order to slice the 784-dimensional vector into a 28x28 matrix.

Please do the following:

  1. Use np.loadtxt to read the entire file.
  2. Slice the columns you want to create labels and images arrays.
  3. Use np.reshape to change an array to the desired shape.