What is length of axis in numpy

Viewed 768

I am just starting up with numpy. The quick start tutorial says:

The coordinates of a point in 3D space [1, 2, 1] has one axis. That axis has 3 elements in it, so we say it has a length of 3. In the example pictured below, the array has 2 axes. The first axis has a length of 2, the second axis has a length of 3.

[[ 1., 0., 0.],  
 [ 0., 1., 2.]]

I am not getting "The first axis has a length of 2, the second axis has a length of 3." I can see both [ 1., 0., 0.] and [ 0., 1., 2.] containing three elements, then why their length is not 3?

2 Answers

Consider a 2-D matrix of dimensions NxM. This means that it has N rows and M columns. And when represented graphically, this matrix would have 2 axes horizontal(against which column indexes are marked) and vertical(against which the row indexes are marked).

Taking the concept here, the array shown here is a 2-D array(an array of some arrays) so, it has 2 axes.

  • Number of rows = 2 = first axis length.
  • Number of columns = 3 = second axis length.

The example you show is not bad, but it is confusing you. The term "axis" is equivalent to "dimension". Take this array:

[[1, 2, 3],
 [4, 5, 6]]

This array is two-dimensional. The fact that the first dimension, or axis, has two elements is just a coincidence. The axes are not [1, 2, 3] and [4, 5, 6].

A less confusing example may be the following:

[[1, 2, 3],
 [2, 3, 4],
 [3, 4, 5],
 [4, 5, 6]]

This array is also two-dimensional, but it has 4 elements in the first dimension. The shape is (4, 3): 4 rows, 3 columns in each row.

Related