NumPy arrays: creating them from lists and with zeros, ones and arange; their attributes ndim, shape, size, dtype and itemsize; and element-wise arithmetic.
| Shape | 2-D: (rows, columns); 1-D: (n,) with a comma |
| size | rows × columns: np.zeros((3, 4)).size → 12 |
| dtype from the items | all whole numbers → int; any float → float; any string → string |
| itemsize | float64 = 8 bytes; int32 = 32 bits = 4 bytes |
| Total bytes | size × itemsize: np.ones((3, 5), dtype=np.int32) → 15 × 4 = 60 |
| arange | np.arange(1, 10, 2) → 1, 3, 5, 7, 9; np.arange(6) → 0 to 5 |
| Array and list | import numpy as np L = [2, 5, 7] A = np.array(L) print(L * 2) # [2, 5, 7, 2, 5, 7] print(A * 2) # [ 4 10 14] |
What are the elements of c? Write them separated by commas.
import numpy as np
a = np.array([24, 13, 17, 45])
b = np.array([32, 30, 33, 28])
c = a + b
Working: element by element: 24 + 32, 13 + 30, 17 + 33, 45 + 28
Answer: 56, 43, 50, 73