3.4. Array Attributes

3.4.1. Rationale

3.4.2. SetUp

>>> import numpy as np

3.4.3. Itemsize

  • int64 takes 64 bits (8 bytes of memory)

../../_images/array-attributes-itemsize.png
>>> a = np.array([1, 2, 3], dtype=int)
>>> a.itemsize
8
>>>
>>> b = np.array([1, 2, 3], dtype=np.int0)
>>> b.itemsize
8
>>>
>>> c = np.array([1, 2, 3], dtype=np.int8)
>>> c.itemsize
1
>>>
>>> d = np.array([1, 2, 3], dtype=np.int16)
>>> d.itemsize
2
>>>
>>> e = np.array([1, 2, 3], dtype=np.int32)
>>> e.itemsize
4
>>>
>>> f = np.array([1, 2, 3], dtype=np.int64)
>>> f.itemsize
8
>>> a = np.array([1, 2, 3], dtype=float)
>>> a.itemsize
8
>>>
>>> b = np.array([1, 2, 3], dtype=np.float16)
>>> b.itemsize
2
>>>
>>> c = np.array([1, 2, 3], dtype=np.float32)
>>> c.itemsize
4
>>>
>>> d = np.array([1, 2, 3], dtype=np.float64)
>>> d.itemsize
8

3.4.4. Strides

  • int64 takes 64 bits (8 bytes of memory)

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

../../_images/array-attributes-strides.png
>>> a = np.array([1, 2, 3])
>>>
>>> a.strides
(8,)
>>> b = np.array([[1, 2, 3],
...               [4, 5, 6]])
>>>
>>> b.strides
(24, 8)
>>> c = np.array([[1, 2, 3],
...               [4, 5, 6],
...               [7, 8, 9]])
>>>
>>> c.strides
(24, 8)
>>> d = np.array([[[ 1,  2,  3],
...                [ 4,  5,  6],
...                [ 5,  6,  7]],
...
...               [[11, 22, 33],
...                [44, 55, 66],
...                [77, 88, 99]]])
>>>
>>> d.strides
(72, 24, 8)

3.4.5. Data

>>> a = np.array([1, 2, 3])
>>>
>>> a.data  
<memory at 0x...>
>>> b = np.array([[1, 2, 3],
...               [4, 5, 6]])
>>>
>>> b.data  
<memory at 0x...>
>>> c = np.array([[[ 1,  2,  3],
...                [ 4,  5,  6],
...                [ 5,  6,  7]],
...
...               [[11, 22, 33],
...                [44, 55, 66],
...                [77, 88, 99]]])
>>>
>>> c.data  
<memory at 0x...>
../../_images/array-attributes-data.png

3.4.6. Recap

../../_images/array-attributes-recap.png

3.4.7. Assignments

Code 3.51. Solution
"""
* Assignment: Numpy Attributes
* Complexity: easy
* Lines of code: 7 lines
* Time: 5 min

English:
    1. Define `result: dict` with:
        a. number of dimensions;
        b. number of elements;
        c. data type;
        d. element size;
        e. shape;
        f. strides.
    2. Run doctests - all must succeed

Polish:
    1. Zdefiniuj `result: dict` z:
        a. liczbę wymiarów,
        b. liczbę elementów,
        c. typ danych,
        d. rozmiar elementu,
        e. kształt,
        f. przeskoki (strides).
    2. Uruchom doctesty - wszystkie muszą się powieść

Tests:
    >>> import sys; sys.tracebacklimit = 0

    >>> assert result is not Ellipsis, \
    'Assign result to variable: `result`'
    >>> assert type(result) is dict, \
    'Variable `result` has invalid type, expected: dict'

    >>> result  # doctest: +NORMALIZE_WHITESPACE
    {'number of dimensions': 2,
     'number of elements': 6,
     'data type': dtype('float64'),
     'element size': 8,
     'shape': (2, 3),
     'strides': (24, 8)}
"""

import numpy as np

DATA = np.array([[-1.1, 0.0, 1.1],
                 [2.2, 3.3, 4.4]])

result = {
    'number of dimensions': ...,
    'number of elements': ...,
    'data type': ...,
    'element size': ...,
    'shape': ...,
    'strides': ...,
}