Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

NumPy Array Attributes

NumPy Array Attributes

In this chapter, we will learn about some basic attributes of NumPy arrays.

The number of dimensions of a NumPy array is called the rank, which is the number of axes, i.e., the array's dimensionality.

A one-dimensional array has rank 1, a two-dimensional array has rank 2, and so on.

In NumPy, each dimension of an array is called an axis, which is also a dimension.

For example, a two-dimensional array can be seen as "a one-dimensional array of one-dimensional arrays": each element of the outer array is itself a one-dimensional array.

The 0th axis corresponds to the direction of the outermost array, the 1st axis is the direction of the inner array, and so on.

The number of axes - the rank - is the dimensionality of the array.

Many NumPy functions support the axis parameter.

For a two-dimensional array, axis=0 means operating along the 0th axis, i.e., operating on each column; axis=1 means operating along the 1st axis, i.e., operating on each row.

The following uses a two-dimensional array with shape (2, 3) as an example to visually show the direction of the two axes and the meaning of each attribute:

axis = 1 (the 1st axis) Horizontal direction, along each row axis = 0 (the 0th axis) Vertical direction, along each column Column 0 Column 1 Column 2 Row 0 Row 1 1 2 3 4 5 6 shape = (2, 3) 2 → length of the 0th axis (number of rows) 3 → length of the 1st axis (number of columns) ndim = 2 (number of axes, i.e., rank) · size = 2 × 3 = 6 (total number of elements)

axis indicates "which direction to compress", not "which row/column": summing along axis=0 compresses multiple rows into one row, so the result is the sum of each column.

Example

import

numpy

as

np

a

=

np.

array

(

[

[

1

,

2

,

3

]

,

[

4

,

5

,

6

]

]

)

Sum along the 0th axis: compress across rows, sum each column

print

(

a.

sum

(

axis

=

0

)

)

Sum along the 1st axis: compress across columns, sum each row

print

(

a.

sum

(

axis

=

1

)

)

The output is:

[5 7 9]

[ 6 15]

The following interactive demo can more intuitively show the summing direction of the two axes, click the button to switch:

↓

↓

↓

1

2

3

4

5

6

5

7

9

→

6

→

15

a.sum(axis=0)

→

[5 7 9]

, compress the elements of each column into one number along the 0th axis


The more important ndarray object attributes in NumPy arrays are:

property Description
ndarray.ndim The rank of an array, i.e., the number of dimensions or the number of axes of the array.
ndarray.shape The dimensions of an array, indicating the size of the array along each axis. For a two-dimensional array (matrix), it indicates its number of rows and columns.
ndarray.size The total number of elements in the array, equal to the product of the sizes along each axis of ndarray.shape the shape.
ndarray.dtype The data type of the elements in the array.
ndarray.itemsize The size of each element in the array, in bytes.
ndarray.flags Contains information about the memory layout, such as whether it is C or Fortran contiguous, whether it is read-only, etc.
ndarray.real The real part of each element in the array (if the element type is complex).
ndarray.imag The imaginary part of each element in the array (if the element type is complex).
ndarray.data The buffer that actually stores the array elements. Elements are generally accessed via indexing, and this attribute is not used directly.

ndarray.ndim

ndarray.ndim is used to get the number of dimensions of an array (i.e., the number of axes of the array), which is the rank.

Example

import

numpy

as

np

a

=

np.

arange

(

24

)

print

(

a.

ndim

)

           

a now has only one dimension

Resize it with reshape to become 2 pages, 4 rows, and 3 columns

b

=

a.

reshape

(

2

,

4

,

3

)

   

b now has three dimensions

print

(

b.

ndim

)

The output is:

1

3


ndarray.shape

ndarray.shape represents the dimensions of an array and returns a tuple whose length is the number of dimensions, i.e., the ndim attribute (rank).

For example, the shape of a two-dimensional array represents its "number of rows" and "number of columns".

The order of the shape tuple is consistent with the order of the axes: the 0th element is the size of the 0th axis (number of rows), and the 1st element is the size of the 1st axis (number of columns).

Example

import

numpy

as

np

a

=

np.

array

(

[

[

1

,

2

,

3

]

,

[

4

,

5

,

6

]

]

)

print

(

a.

shape

)

The output is:

(2, 3)

ndarray.shape can also be used to resize an array.

Example

import

numpy

as

np

a

=

np.

array

(

[

[

1

,

2

,

3

]

,

[

4

,

5

,

6

]

]

)

a.

shape

=

(

3

,

2

)

print

(

a

)

The output is:

[[1 2]

 [3 4]

 [5 6]]

NumPy also provides the reshape function to resize arrays.

Example

import

numpy

as

np

a

=

np.

array

(

[

[

1

,

2

,

3

]

,

[

4

,

5

,

6

]

]

)

b

=

a.

reshape

(

3

,

2

)

print

(

b

)

The output is:

[[1 2]

 [3 4]

 [5 6]]

The difference between the two methods: assigning directly to a.shape modifies the array a itself in place; while a.reshape(3, 2) returns a new array object (usually sharing the same data with a), and a itself remains unchanged.


ndarray.size

ndarray.size returns the total number of elements in the array, equal to the product of the sizes along each axis in shape.

Example

import

numpy

as

np

a

=

np.

array

(

[

[

1

,

2

,

3

]

,

[

4

,

5

,

6

]

]

)

print

(

a.

shape

)

 

shape is (2, 3)

print

(

a.

size

)

   

Total number of elements: 2 × 3 = 6

The output is:

(2, 3)

6


ndarray.dtype

ndarray.dtype returns the data type of the elements in the array.

The dtype object contains information such as the type name and element bit width. For example, int64 represents a 64-bit integer, and float32 represents a 32-bit floating-point number.

Example

import

numpy

as

np

An integer list creates an int64 array by default (int32 by default on Windows)

a

=

np.

array

(

[

1

,

2

,

3

]

)

print

(

a.

dtype

)

You can explicitly specify the data type with the dtype parameter when creating

b

=

np.

array

(

[

1

,

2

,

3

]

,

dtype

=

np.

float32

)

print

(

b.

dtype

)

The output is:

int64

float32


ndarray.real and ndarray.imag

ndarray.real and ndarray.imag return the real part and imaginary part of each element in the array, respectively, mainly used when the element type is complex.

Example

import

numpy

as

np

Create a complex array, the default type is complex128

a

=

np.

array

(

[

1

+2j

,

3

+4j

,

5

+6j

]

)

print

(

a.

real

)

 

The real part of each element

print

(

a.

imag

)

 

The imaginary part of each element

The output is:

[1. 3. 5.]

[2. 4. 6.]


ndarray.itemsize

ndarray.itemsize returns the size of each element in the array in bytes.

For example, for an array whose element type is float64, the itemsize attribute value is 8 (float64 occupies 64 bits, every 8 bits is 1 byte, 64 ÷ 8 = 8 bytes).

As another example, for an array whose element type is int32, the itemsize attribute value is 4 (32 ÷ 8).

The product of size and itemsize is the total number of bytes occupied by the array data, which is the value of the attribute ndarray.nbytes.

Example

import

numpy

as

np

The array's dtype is int8, each element occupies 1 byte

x

=

np.

array

(

[

1

,

2

,

3

,

4

,

5

]

,

dtype

=

np.

int8

)

print

(

x.

itemsize

)

The array's dtype is float64, each element occupies 8 bytes

y

=

np.

array

(

[

1

,

2

,

3

,

4

,

5

]

,

dtype

=

np.

float64

)

print

(

y.

itemsize

)

The output is:

1

8


ndarray.flags

ndarray.flags returns the memory information of the ndarray object, including the following attributes:

property Description
C_CONTIGUOUS (C) The data is in a single, C-style contiguous memory region
F_CONTIGUOUS (F) The data is in a single, Fortran-style contiguous memory region
OWNDATA (O) The array owns the memory it uses, rather than borrowing it from other objects
WRITEABLE (W) The data area can be written to. Setting this value to False makes the data read-only
ALIGNED (A) The data and all elements are properly aligned to the hardware
WRITEBACKIFCOPY (X) This array is a copy of another array. When this array is freed, its contents will be written back to the original array

The old version UPDATEIFCOPY (U) is the old name of WRITEBACKIFCOPY. It was deprecated in NumPy 1.14 and completely removed since NumPy 2.0, so it should no longer be used.

C_CONTIGUOUS and F_CONTIGUOUS describe the arrangement order of the same data in memory. A two-dimensional array has two typical storage methods:

C style (row-major) Store the 0th row first, then the 1st row 123 456 1 2 3 4 5 6 Expand row by row 123 456 1 2 3 4 5 6 012 345 Memory addresses (from left to right) C_CONTIGUOUS : True Fortran style (column-major) Store the 0th column first, then the 1st column 123 456 1 2 3 4 5 6 Expand column by column 142 536 1 2 3 4 5 6 012 345 Memory addresses (from left to right) F_CONTIGUOUS : True NumPy arrays are stored in C style (row-major) by default

Example

import

numpy

as

np

x

=

np.

array

(

[

1

,

2

,

3

,

4

,

5

]

)

print

(

x.

flags

)

The output is:

  C_CONTIGUOUS : True

  F_CONTIGUOUS : True

  OWNDATA : True

  WRITEABLE : True

  ALIGNED : True

  WRITEBACKIFCOPY : False