2.4. Array Attributes¶
2.4.1. Size¶
Number of elements
import numpy as np
a = np.array([1, 2, 3])
b = np.array([[1, 2, 3],
[4, 5, 6]])
c = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
d = np.array([[[ 1, 2, 3],
[ 4, 5, 6],
[ 5, 6, 7]],
[[11, 22, 33],
[44, 55, 66],
[77, 88, 99]]])
a.size
# 3
b.size
# 6
c.size
# 9
d.size
# 18
2.4.2. Shape¶
import numpy as np
a = np.array([1, 2, 3])
b = np.array([[1, 2, 3],
[4, 5, 6]])
c = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
d = np.array([[[ 1, 2, 3],
[ 4, 5, 6],
[ 5, 6, 7]],
[[11, 22, 33],
[44, 55, 66],
[77, 88, 99]]])
a.shape
# (3,)
b.shape
# (2, 3)
c.shape
# (3, 3)
d.shape
# (2, 3, 3)
2.4.3. NDim¶
Number of Dimensions
len(ndarray.shape)
import numpy as np
a = np.array([1, 2, 3])
b = np.array([[1, 2, 3],
[4, 5, 6]])
c = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
d = np.array([[[ 1, 2, 3],
[ 4, 5, 6],
[ 5, 6, 7]],
[[11, 22, 33],
[44, 55, 66],
[77, 88, 99]]])
a.ndim
# 1
b.ndim
# 2
c.ndim
# 2
d.ndim
# 3
2.4.4. Length¶
Number of elements in first dimension
ndarray.shape[0]
import numpy as np
a = np.array([1, 2, 3])
b = np.array([[1, 2, 3],
[4, 5, 6]])
c = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
d = np.array([[[ 1, 2, 3],
[ 4, 5, 6],
[ 5, 6, 7]],
[[11, 22, 33],
[44, 55, 66],
[77, 88, 99]]])
len(a)
# 2
len(b)
# 2
len(c)
# 2
len(d)
# 2
2.4.5. Itemsize¶
int64
takes 64 bits (8 bytes of memory)
import numpy as np
a = np.array([1, 2, 3], dtype=np.int16)
b = np.array([1, 2, 3], dtype=np.int32)
c = np.array([1, 2, 3], dtype=np.int64)
a.itemsize
# 2
b.itemsize
# 4
c.itemsize
# 8
import numpy as np
a = np.array([1, 2, 3], dtype=np.float16)
b = np.array([1, 2, 3], dtype=np.float32)
c = np.array([1, 2, 3], dtype=np.float64)
a.itemsize
# 2
b.itemsize
# 4
c.itemsize
# 8
2.4.6. Strides¶
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])
b = np.array([[1, 2, 3],
[4, 5, 6]])
c = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
d = np.array([[[ 1, 2, 3],
[ 4, 5, 6],
[ 5, 6, 7]],
[[11, 22, 33],
[44, 55, 66],
[77, 88, 99]]])
a.strides
# (8,)
b.strides
# (24, 8)
c.strides
# (24, 8)
d.strides
# (72, 24, 8)
2.4.7. Data¶
import numpy as np
a = np.array([1, 2, 3])
a.shape
# (3,)
a.itemsize
# 8
a.strides
# (8,)
a.data
# <memory at 0x10cdfaa10>
import numpy as np
a = np.array([[1, 2, 3],
[4, 5, 6]])
a.shape
# (2, 3)
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],
[ 5, 6, 7]],
[[11, 22, 33],
[44, 55, 66],
[77, 88, 99]]])
a.shape
# (2, 3, 3)
a.itemsize
# 8
a.strides
# (72, 24, 8)
a.data
# <memory at 0x107933c70>
2.4.8. Assignments¶
"""
* Assignment: Numpy Attributes
* Complexity: easy
* Lines of code: 7 lines
* Time: 5 min
English:
1. Use data from "Given" section (see below)
2. Define `result: dict` with:
a. number of dimensions;
b. number of elements;
c. data type;
d. element size;
e. shape;
f. strides.
Polish:
1. Użyj danych z sekcji "Given" (patrz poniżej)
2. Zdefiniuj `result: dict` z:
a. liczbę wymiarów,
b. liczbę elementów,
c. typ danych,
d. rozmiar elementu,
e. kształt,
f. przeskoki (strides).
Tests:
>>> type(result) is dict
True
>>> 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)}
"""
# Given
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': ...,
}