88

I'm trying to break down a program line by line. Y is a matrix of data but I can't find any concrete data on what .shape[0] does exactly.

for i in range(Y.shape[0]):
    if Y[i] == -1:

This program uses numpy, scipy, matplotlib.pyplot, and cvxopt.

dwitvliet
  • 7,242
  • 7
  • 36
  • 62
HipsterCarlGoldstein
  • 1,029
  • 1
  • 9
  • 8
  • 6
    http://docs.scipy.org/doc/numpy/reference/generated/numpy.ma.shape.html or http://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.shape.html#numpy.ndarray.shape – Felix Kling Apr 17 '12 at 22:46

6 Answers6

142

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
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
47

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

haccks
  • 104,019
  • 25
  • 176
  • 264
Wickkiey
  • 4,446
  • 2
  • 39
  • 46
11

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 Link

 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.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Levon
  • 138,105
  • 33
  • 200
  • 191
  • 2
    @HipsterCarlGoldstein Just a friendly note, if any one of these answers provided solved your problem please consider [accepting it by clicking the checkmark next to the answer](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work/5235#5235). This will give you and the answerer both some rep points and also mark this problem as solved - thanks. – Levon Jun 17 '12 at 19:31
5

In python, Suppose you have loaded up the data in some variable train:

train = pandas.read_csv('file_name')
>>> train
train([[ 1.,  2.,  3.],
        [ 5.,  1.,  2.]],)

I want to check what are the dimensions of the 'file_name'. I have stored the file in train

>>>train.shape
(2,3)
>>>train.shape[0]              # will display number of rows
2
>>>train.shape[1]              # will display number of columns
3
Drool
  • 119
  • 1
  • 7
4

In Python shape() is use in pandas to give number of row/column:

Number of rows is given by:

train = pd.read_csv('fine_name') //load the data
train.shape[0]

Number of columns is given by

train.shape[1]
rlandster
  • 7,294
  • 14
  • 58
  • 96
HeadAndTail
  • 804
  • 8
  • 9
2

shape() consists of array having two arguments rows and columns.

if you search shape[0] then it will gave you the number of rows. shape[1] will gave you number of columns.

ajay
  • 43
  • 6