0

Sorry if this is some basic stuff, but I can't seem to find any information on this.

I'm wondering what the commas do in this situation?

f = loadtxt(filename)  
return f[:,:4],f[:,4:]

It's code from Programming Computer Vision and it is throwing up errors.

IndexError: too many indices for array

When I remove the commas I get no errors but I think I am getting an incorrect result.

Paul
  • 26,170
  • 12
  • 85
  • 119

1 Answers1

0

The commas within the [] brackets separate dimensions in multi-dimensional array indexing.

The error states that whatever is returned by f=loadtxt() is not a multidimensional array.

Multidimensional arrays have a .shape property that could be tested with a print statement, e.g. print f.shape

The colons specify a range of indexes, a slice from the array.
If you remove the commas, you get, e.g. f[::4], which is still a valid python array slice spec for a single dimension array. It equals every 4th element from the entire array. That might not be what you want, and certainly isn't what is originally intended with the comma in place.

For more on the slice notation see Explain Python's slice notation

Community
  • 1
  • 1
Paul
  • 26,170
  • 12
  • 85
  • 119