3.7. Array Attributes

3.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

3.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>

3.7.3. Assignments

3.7.3.1. Create

  • Complexity level: easy

  • Lines of code to write: 4 lines

  • Estimated time of completion: 5 min

  • Filename: solution/numpy_astype.py

English
  1. Set random seed to zero

  2. Create a: ndarray with size 16x16

  3. Structure must contains random integers (0-9)

  4. Print number of:

    • dimensions

    • columns

    • rows

    • element count

Polish
  1. Ustaw ziarno losowości na zero

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

  3. Struktura musi zawierać losowe liczby (0-9)

  4. Wypisz liczbę:

    - wymiarów - kolumn - wierszy - ilość elementów

The whys and wherefores
  • Defining ndarray

  • Using np.random.seed()

  • Generating random np.array