3.3. Array Attributes¶
3.3.1. SetUp¶
>>> import numpy as np
3.3.2. Size¶
Number of elements

>>> a = np.array([1, 2, 3])
>>>
>>> a.size
3
>>> b = np.array([[1, 2, 3],
... [4, 5, 6]])
>>>
>>> b.size
6
>>> c = np.array([[1, 2, 3],
... [4, 5, 6],
... [7, 8, 9]])
>>>
>>> c.size
9
>>> d = np.array([[[ 1, 2, 3],
... [ 4, 5, 6],
... [ 5, 6, 7]],
...
... [[11, 22, 33],
... [44, 55, 66],
... [77, 88, 99]]])
>>>
>>> d.size
18
3.3.3. Shape¶

>>> a = np.array([1, 2, 3])
>>>
>>> a.shape
(3,)
>>> b = np.array([[1, 2, 3],
... [4, 5, 6]])
>>>
>>> b.shape
(2, 3)
>>> c = np.array([[1, 2, 3],
... [4, 5, 6],
... [7, 8, 9]])
>>>
>>> c.shape
(3, 3)
>>> d = np.array([[[ 1, 2, 3],
... [ 4, 5, 6],
... [ 5, 6, 7]],
...
... [[11, 22, 33],
... [44, 55, 66],
... [77, 88, 99]]])
>>>
>>> d.shape
(2, 3, 3)
3.3.4. NDim¶
Number of Dimensions
len(ndarray.shape)

>>> a = np.array([1, 2, 3])
>>>
>>> a.ndim
1
>>> b = np.array([[1, 2, 3],
... [4, 5, 6]])
>>>
>>> b.ndim
2
>>> c = np.array([[1, 2, 3],
... [4, 5, 6],
... [7, 8, 9]])
>>>
>>> c.ndim
2
>>> d = np.array([[[ 1, 2, 3],
... [ 4, 5, 6],
... [ 5, 6, 7]],
...
... [[11, 22, 33],
... [44, 55, 66],
... [77, 88, 99]]])
>>>
>>> d.ndim
3
3.3.5. Length¶
Number of elements in first dimension
ndarray.shape[0]
>>> import numpy as np
>>>
>>>
>>> a = np.array([1, 2, 3])
>>>
>>> len(a)
3
>>> b = np.array([[1, 2, 3],
... [4, 5, 6]])
>>>
>>> len(b)
2
>>> c = np.array([[1, 2, 3],
... [4, 5, 6],
... [7, 8, 9]])
>>>
>>> len(c)
3
>>> d = np.array([[[ 1, 2, 3],
... [ 4, 5, 6],
... [ 5, 6, 7]],
...
... [[11, 22, 33],
... [44, 55, 66],
... [77, 88, 99]]])
>>>
>>> len(d)
2
3.3.6. Recap¶

3.3.7. Assignments¶
"""
* 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': ...,
}