4.7. Array Attributes

4.7.1. Dimensions

import numpy as np


a = np.array([1, 2, 3])

a.ndim          # 1
a.size          # 3
a.shape         # (3,)
len(a)          # 3
import numpy as np


a = np.array([[1, 2, 3],
              [4, 5, 6]])

a.ndim          # 2
a.shape         # (2, 3)
a.size          # 6
len(a)          # 2
import numpy as np


a = np.array([[1, 2, 3],
              [4, 5, 6],
              [7, 8, 9]])

a.ndim          # 2
a.shape         # (3, 3)
a.size          # 9
len(a)          # 3
import numpy as np


a = np.array([[[ 1,  2,  3],
               [ 4,  5,  6],
               [ 5,  6,  7]],
              [[11, 22, 33],
               [44, 55, 66],
               [77, 88, 99]]])

a.ndim          # 3
a.shape         # (2, 3, 3)
a.size          # 18
len(a)          # 2

4.7.2. Data

  • int64 takes 64 bits (8 bytes of memory)

  • strides inform how many bytes numpy has to jump to access values in each dimensions

import numpy as np


a = np.array([1, 2, 3])

a.itemsize      # 8
a.strides       # (8,)
a.data          # <memory at 0x10cdfaa10>
list(a.data)    # NotImplementedError: multi-dimensional sub-views are not implemented
import numpy as np


a = np.array([[1, 2, 3],
              [4, 5, 6]])

a.itemsize      # 8
a.strides       # (24, 8)
a.data          # <memory at 0x10caefbb0>
import numpy as np


a = np.array([[1, 2, 3],
              [4, 5, 6],
              [7, 8, 9]])

a.itemsize      # 8
a.strides       # (24, 8)
a.data          # <memory at 0x10cf92210>

4.7.3. Assignments

4.7.3.1. Numpy Attributes

English
  1. Set random seed to zero.

  2. Create a: ndarray with size 16x16.

  3. Structure must contains random digits 0-9 (inclusive).

  4. Print:

    • number of dimensions;

    • number of elements;

    • data type;

    • element size;

    • shape;

    • strides.

Polish
  1. Ustaw ziarno losowości na zero.

  2. Stwórz a: ndarray o rozmiarze 16x16.

  3. Struktura musi zawierać losowe cyfry 0-9 (włącznie).

  4. Wypisz:

    • liczbę wymiarów,

    • liczbę elementów,

    • typ danych,

    • rozmiar elementu,

    • kształt,

    • przeskoki (strides).

The whys and wherefores
  • Defining ndarray

  • Using np.random.seed()

  • Generating random np.array