|
| 1 | +# NumPy Array Iteration |
| 2 | + |
| 3 | +Iterating over arrays in NumPy is a common task when processing data. NumPy provides several ways to iterate over elements of an array efficiently. |
| 4 | +Understanding these methods is crucial for performing operations on array elements effectively. |
| 5 | + |
| 6 | +## 1. Basic Iteration |
| 7 | + |
| 8 | +- Iterating using basic `for` loop. |
| 9 | + |
| 10 | +### Single-dimensional array |
| 11 | + |
| 12 | +Iterating over a single-dimensional array is straightforward using a basic `for` loop |
| 13 | + |
| 14 | +```python |
| 15 | +import numpy as np |
| 16 | + |
| 17 | +arr = np.array([1, 2, 3, 4, 5]) |
| 18 | +for i in arr: |
| 19 | + print(i) |
| 20 | +``` |
| 21 | + |
| 22 | +#### Output |
| 23 | + |
| 24 | +```python |
| 25 | +1 |
| 26 | +2 |
| 27 | +3 |
| 28 | +4 |
| 29 | +5 |
| 30 | +``` |
| 31 | + |
| 32 | +### Multi-dimensional array |
| 33 | + |
| 34 | +Iterating over multi-dimensional arrays, each iteration returns a sub-array along the first axis. |
| 35 | + |
| 36 | +```python |
| 37 | +marr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) |
| 38 | + |
| 39 | +for arr in marr: |
| 40 | + print(arr) |
| 41 | +``` |
| 42 | + |
| 43 | +#### Output |
| 44 | + |
| 45 | +```python |
| 46 | +[1 2 3] |
| 47 | +[4 5 6] |
| 48 | +[7 8 9] |
| 49 | +``` |
| 50 | + |
| 51 | +## 2. Iterating with `nditer` |
| 52 | + |
| 53 | +- `nditer` is a powerful iterator provided by NumPy for iterating over multi-dimensional arrays. |
| 54 | +- In each interation it gives each element. |
| 55 | + |
| 56 | +```python |
| 57 | +import numpy as np |
| 58 | + |
| 59 | +arr = np.array([[1, 2, 3], [4, 5, 6]]) |
| 60 | +for i in np.nditer(arr): |
| 61 | + print(i) |
| 62 | +``` |
| 63 | + |
| 64 | +#### Output |
| 65 | + |
| 66 | +```python |
| 67 | +1 |
| 68 | +2 |
| 69 | +3 |
| 70 | +4 |
| 71 | +5 |
| 72 | +6 |
| 73 | +``` |
| 74 | + |
| 75 | +## 3. Iterating with `ndenumerate` |
| 76 | + |
| 77 | +- `ndenumerate` allows you to iterate with both the index and the value of each element. |
| 78 | +- It gives index and value as output in each iteration |
| 79 | + |
| 80 | +```python |
| 81 | +import numpy as np |
| 82 | + |
| 83 | +arr = np.array([[1, 2], [3, 4]]) |
| 84 | +for index,value in np.ndenumerate(arr): |
| 85 | + print(index,value) |
| 86 | +``` |
| 87 | + |
| 88 | +#### Output |
| 89 | + |
| 90 | +```python |
| 91 | +(0, 0) 1 |
| 92 | +(0, 1) 2 |
| 93 | +(1, 0) 3 |
| 94 | +(1, 1) 4 |
| 95 | +``` |
| 96 | + |
| 97 | +## 4. Iterating with flat |
| 98 | + |
| 99 | +- The `flat` attribute returns a 1-D iterator over the array. |
| 100 | + |
| 101 | +```python |
| 102 | +import numpy as np |
| 103 | + |
| 104 | +arr = np.array([[1, 2], [3, 4]]) |
| 105 | +for element in arr.flat: |
| 106 | + print(element) |
| 107 | +``` |
| 108 | + |
| 109 | +#### Output |
| 110 | + |
| 111 | +```python |
| 112 | +1 |
| 113 | +2 |
| 114 | +3 |
| 115 | +4 |
| 116 | +``` |
| 117 | + |
| 118 | +Understanding the various ways to iterate over NumPy arrays can significantly enhance your data processing efficiency. |
| 119 | + |
| 120 | +Whether you are working with single-dimensional or multi-dimensional arrays, NumPy provides versatile tools to iterate and manipulate array elements effectively. |
0 commit comments