Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which number represents rows and columns in the tuple returned by shape?

>>> A = np.matrix(np.zeros(2, 3)))  
>>> A.shape  
(2, 3)  
>>> A  
matrix([[ 0.,  0.,  0.],
        [ 0.,  0.,  0.]])

Does the matrix A have two rows with three zeros or two columns with three zeros?

like image 290
user2248727 Avatar asked Jun 17 '15 17:06

user2248727


People also ask

What does the shape tuple represent?

Shape of an Array The shape is a tuple of integers. These numbers denote the lengths of the corresponding array dimension. In other words: The "shape" of an array is a tuple with the number of elements per axis (dimension). In our example, the shape is equal to (6, 3), i.e. we have 6 lines and 3 columns.

How do you shape a tuple?

The shape of a simple Python tuple or list can be obtained with the built-in len() function. len() will return an integer that describes the number of objects in the tuple or list.

What does shape 0 do in Python?

Python numpy shape 0 shape is a tuple that always gives dimensions of the array. The shape is a tuple that gives you an indication of the no. of dimensions in the array. The shape function for numpy arrays returns the dimensions of the array.

Is Numpy shape row column?

numpy reports the shape of 3D arrays in the order layers, rows, columns.


3 Answers

A.shape will return a tuple (m, n), where m is the number of rows, and n is the number of columns.

like image 58
Anand S Kumar Avatar answered Oct 01 '22 06:10

Anand S Kumar


rows, columns are just the names we give, by convention, to the 2 dimensions of a matrix (or more generally a 2d numpy array).

np.matrix is, by definition, 2d, so this convention is useful. But np.array may have 0, 1, 2 or more dimensions. For that these 2 names are less useful. For example if 1d, does it have rows or columns? If 3d, what do we call the last dimension, depth? or maybe the first is pages?

So don't put too much emphasis on the names. Most numpy functions ask you to specify the 'axis', by number, 0, 1, 2 etc., not by name.

There may be further confusion if you load data from a csv file, and get a 1d array (one 'row' per line of the file) of dtype fields. Are fields the same as columns? Sort of, but not quite.

like image 10
hpaulj Avatar answered Oct 01 '22 08:10

hpaulj


The matrix in question has 2 rows and 3 columns (it is of dimension 2x3), where each of the matrix elements is of value zero.

like image 7
Todd Minehardt Avatar answered Oct 01 '22 06:10

Todd Minehardt