Suppose I have:
test = numpy.array([[1, 2], [3, 4], [5, 6]])
test[i]
gets me ith line of the array (eg [1, 2]
). How can I access the ith column? (eg [1, 3, 5]
). Also, would this be an expensive operation?
>>> test[:,0]
array([1, 3, 5])
Similarly,
>>> test[1,:]
array([3, 4])
lets you access rows. This is covered in Section 1.4 (Indexing) of the NumPy reference. This is quick, at least in my experience. It's certainly much quicker than accessing each element in a loop.
This create a copy, is it possible to get reference, like I get a reference to a column, any change in this reference is reflected in the original array.
Just to make sure, considering test.shape=(2,x,y). is it correct that test[:,0 :, :, ] is the way to access the first 'column' (coordinate)?
How would you go about selecting multiple columns and multiple rows?
@AAAlex123 - see Akavall's answer[stackoverflow.com/a/16121210/120261]
@mtrw I was, more precisely, referring to selecting a range of columns, not specific ones, for example columns 1-5. Reading the documentation I found this syntax
A[a:b, c:d]
which selects rows a to b and columns c to d.