Shape, Dtype, and Axis

Three array facts should become automatic:

Shape tells you the size of each dimension. Dtype tells you the kind of values stored. An axis identifies one dimension of the array.

Shape

The output is:

(2, 3)

Read this as two rows and three columns.

Dtype

print(A.dtype)

For the integer array above, the dtype is an integer type such as int64 or int32, depending on the environment.

Dtype matters because arrays store values in a consistent numerical format.

Axis

For a two-dimensional array, axis 0 indexes rows and axis 1 indexes columns. When a reduction names an axis, it combines values along that axis and normally removes it from the result:

  • axis=0 combines rows and leaves one result per column;
  • axis=1 combines columns and leaves one result per row.

Axis 0 and axis 1

Runs locally with Python in your browser.

Ready to run.

axis=0 produces three column sums.

axis=1 produces two row sums.

Say what each axis means

Do not stop at the tuple. Attach meaning:

(2, 3) means 2 examples and 3 features

or:

(2, 3) means 2 rows and 3 columns

The numbers alone are not enough. The interpretation matters.

Exercise: Read a shape

An array has shape (4, 2). If axis 0 is examples and axis 1 is features, how many examples are there?

Answer it first, then check.

Hint

Shape entries follow axis order. The first entry is the length of axis 0.

Solution

There are 4 examples. In shape (4, 2), the first value belongs to axis 0, which the question defines as the examples axis.