What does .shape[] do in "for i in range(Y.shape[0])"?
The shape
attribute for numpy arrays returns the dimensions of the array. If Y
has n
rows and m
columns, then Y.shape
is (n,m)
. So Y.shape[0]
is n
.
In [46]: Y = np.arange(12).reshape(3,4)
In [47]: Y
Out[47]:
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
In [48]: Y.shape
Out[48]: (3, 4)
In [49]: Y.shape[0]
Out[49]: 3
shape is a tuple that gives dimensions of the array..
>>> c = arange(20).reshape(5,4)
>>> c
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15],
[16, 17, 18, 19]])
c.shape[0]
5
Gives the number of rows
c.shape[1]
4
Gives number of columns
shape
is a tuple that gives you an indication of the number of dimensions in the array. So in your case, since the index value of Y.shape[0]
is 0, your are working along the first dimension of your array.
From http://www.scipy.org/Tentative_NumPy_Tutorial#head-62ef2d3c0a5b4b7d6fdc48e4a60fe48b1ffe5006
An array has a shape given by the number of elements along each axis:
>>> a = floor(10*random.random((3,4)))
>>> a
array([[ 7., 5., 9., 3.],
[ 7., 2., 7., 8.],
[ 6., 8., 3., 2.]])
>>> a.shape
(3, 4)
and http://www.scipy.org/Numpy_Example_List#shape has some more examples.