There are multiple questions on StackOverflow, asking how the comma syntax works, but most of them refer to m[:,n] which refers to the nth column. Similarly, m[n,:] refers to the nth row. I find this method of slicing used in the labs of Machine Learning Specialization by Andrew Ng. But does this slicing have any advantage over m[n]?
2 Answers
For an array with 2 or more dimensions, m[n] and m[n, :] are identical. The first can be considered shorthand for the second.
For an array with 1 dimension, m[n] will return element n, and m[n, :] will result in an error.
I personally would choose m[n, :] in some cases to make the code more human-readable: for example, when you know that m is two-dimensional, then m[n, :] immediately implies this to the reader, whereas m[n] might leave them having to guess at whether m is 1D or 2D.
m[n]andm[n, :]are equivalent. The first one is maybe more explicit, this suggests that you have at least two dimensions.m[n, :]means you have at least 2D):,are required to identify 'column' indexing, trailing:are optional. I think ofnumpyfilling those in as needed....(ellipsis) occasionally is useful as well.