|
| 1 | +# Operations on Arrays |
| 2 | + |
| 3 | +## NumPy Arithmetic Operations |
| 4 | + |
| 5 | +NumPy offers a broad array of operations for arrays, including arithmetic functions. |
| 6 | + |
| 7 | +The arithmetic operations in NumPy are popular for their simplicity and efficiency in handling array calculations. |
| 8 | + |
| 9 | +**Addition** |
| 10 | + |
| 11 | +we can use the `+` operator to perform element-wise addition between two or more NumPy arrays. |
| 12 | + |
| 13 | +**Code** |
| 14 | +```python |
| 15 | +import numpy as np |
| 16 | +array_1 = np.array([9, 10, 11, 12]) |
| 17 | +array_2 = np.array([1, 3, 5, 7]) |
| 18 | +result_1 = array_1 + array_2 |
| 19 | +print("Utilizing the + operator:", result_1) |
| 20 | +``` |
| 21 | + |
| 22 | +**Output:** |
| 23 | +``` |
| 24 | +Utilizing the + operator: [10 13 16 19] |
| 25 | +``` |
| 26 | + |
| 27 | +**Subtraction** |
| 28 | + |
| 29 | +we can use the `-` operator to perform element-wise subtraction between two or more NumPy arrays. |
| 30 | + |
| 31 | +**Code** |
| 32 | +```python |
| 33 | +import numpy as np |
| 34 | +array_1 = np.array([9, 10, 11, 12]) |
| 35 | +array_2 = np.array([1, 3, 5, 7]) |
| 36 | +result_1 = array_1 - array_2 |
| 37 | +print("Utilizing the - operator:", result_1) |
| 38 | +``` |
| 39 | + |
| 40 | +**Output:** |
| 41 | +``` |
| 42 | +Utilizing the - operator: [8 7 6 5] |
| 43 | +``` |
| 44 | + |
| 45 | +**Multiplication** |
| 46 | + |
| 47 | +we can use the `*` operator to perform element-wise multiplication between two or more NumPy arrays. |
| 48 | + |
| 49 | +**Code** |
| 50 | +```python |
| 51 | +import numpy as np |
| 52 | +array_1 = np.array([9, 10, 11, 12]) |
| 53 | +array_2 = np.array([1, 3, 5, 7]) |
| 54 | +result_1 = array_1 * array_2 |
| 55 | +print("Utilizing the * operator:", result_1) |
| 56 | +``` |
| 57 | + |
| 58 | +**Output:** |
| 59 | +``` |
| 60 | +Utilizing the * operator: [9 30 55 84] |
| 61 | +``` |
| 62 | + |
| 63 | +**Division** |
| 64 | + |
| 65 | +we can use the `/` operator to perform element-wise division between two or more NumPy arrays. |
| 66 | + |
| 67 | +**Code** |
| 68 | +```python |
| 69 | +import numpy as np |
| 70 | +array_1 = np.array([9, 10, 11, 12]) |
| 71 | +array_2 = np.array([1, 3, 5, 7]) |
| 72 | +result_1 = array_1 / array_2 |
| 73 | +print("Utilizing the / operator:", result_1) |
| 74 | +``` |
| 75 | + |
| 76 | +**Output:** |
| 77 | +``` |
| 78 | +Utilizing the / operator: [9. 3.33333333 2.2 1.71428571] |
| 79 | +``` |
| 80 | + |
| 81 | +**Exponentiation** |
| 82 | + |
| 83 | +we can use the `**` operator to perform element-wise exponentiation between two or more NumPy arrays. |
| 84 | + |
| 85 | +**Code** |
| 86 | +```python |
| 87 | +import numpy as np |
| 88 | +array_1 = np.array([9, 10, 11, 12]) |
| 89 | +array_2 = np.array([1, 3, 5, 7]) |
| 90 | +result_1 = array_1 ** array_2 |
| 91 | +print("Utilizing the ** operator:", result_1) |
| 92 | +``` |
| 93 | + |
| 94 | +**Output:** |
| 95 | +``` |
| 96 | +Utilizing the ** operator: [9 1000 161051 35831808] |
| 97 | +``` |
| 98 | + |
| 99 | +**Modulus** |
| 100 | + |
| 101 | +We can use the `%` operator to perform element-wise modulus operations between two or more NumPy arrays. |
| 102 | + |
| 103 | +**Code** |
| 104 | +```python |
| 105 | +import numpy as np |
| 106 | +array_1 = np.array([9, 10, 11, 12]) |
| 107 | +array_2 = np.array([1, 3, 5, 7]) |
| 108 | +result_1 = array_1 % array_2 |
| 109 | +print("Utilizing the % operator:", result_1) |
| 110 | +``` |
| 111 | + |
| 112 | +**Output:** |
| 113 | +``` |
| 114 | +Utilizing the % operator: [0 1 1 5] |
| 115 | +``` |
| 116 | + |
| 117 | +<br> |
| 118 | + |
| 119 | +## NumPy Comparision Operations |
| 120 | + |
| 121 | +<br> |
| 122 | + |
| 123 | +NumPy provides various comparison operators that can compare elements across multiple NumPy arrays. |
| 124 | + |
| 125 | +**less than operator** |
| 126 | + |
| 127 | +The `<` operator returns `True` if the value of operand on left is less than the value of operand on right. |
| 128 | + |
| 129 | +**Code** |
| 130 | +```python |
| 131 | +import numpy as np |
| 132 | +array_1 = np.array([12,15,20]) |
| 133 | +array_2 = np.array([20,15,12]) |
| 134 | +result_1 = array_1 < array_2 |
| 135 | +print("array_1 < array_2:",result_1) |
| 136 | +``` |
| 137 | +**Output:** |
| 138 | +``` |
| 139 | +array_1 < array_2 : [True False False] |
| 140 | +``` |
| 141 | + |
| 142 | +**less than or equal to operator** |
| 143 | + |
| 144 | +The `<=` operator returns `True` if the value of operand on left is lesser than or equal to the value of operand on right. |
| 145 | + |
| 146 | +**Code** |
| 147 | +```python |
| 148 | +import numpy as np |
| 149 | +array_1 = np.array([12,15,20]) |
| 150 | +array_2 = np.array([20,15,12]) |
| 151 | +result_1 = array_1 <= array_2 |
| 152 | +print("array_1 <= array_2:",result_1) |
| 153 | +``` |
| 154 | +**Output:** |
| 155 | +``` |
| 156 | +array_1 <= array_2: [True True False] |
| 157 | +``` |
| 158 | + |
| 159 | +**greater than operator** |
| 160 | + |
| 161 | +The `>` operator returns `True` if the value of operand on left is greater than the value of operand on right. |
| 162 | + |
| 163 | +**Code** |
| 164 | +```python |
| 165 | +import numpy as np |
| 166 | +array_1 = np.array([12,15,20]) |
| 167 | +array_2 = np.array([20,15,12]) |
| 168 | +result_2 = array_1 > array_2 |
| 169 | +print("array_1 > array_2:",result_2) |
| 170 | +``` |
| 171 | +**Output:** |
| 172 | +``` |
| 173 | +array_1 > array_2 : [False False True] |
| 174 | +``` |
| 175 | + |
| 176 | +**greater than or equal to operator** |
| 177 | + |
| 178 | +The `>=` operator returns `True` if the value of operand on left is greater than or equal to the value of operand on right. |
| 179 | + |
| 180 | +**Code** |
| 181 | +```python |
| 182 | +import numpy as np |
| 183 | +array_1 = np.array([12,15,20]) |
| 184 | +array_2 = np.array([20,15,12]) |
| 185 | +result_2 = array_1 >= array_2 |
| 186 | +print("array_1 >= array_2:",result_2) |
| 187 | +``` |
| 188 | +**Output:** |
| 189 | +``` |
| 190 | +array_1 >= array_2: [False True True] |
| 191 | +``` |
| 192 | + |
| 193 | +**equal to operator** |
| 194 | + |
| 195 | +The `==` operator returns `True` if the value of operand on left is same as the value of operand on right. |
| 196 | + |
| 197 | +**Code** |
| 198 | +```python |
| 199 | +import numpy as np |
| 200 | +array_1 = np.array([12,15,20]) |
| 201 | +array_2 = np.array([20,15,12]) |
| 202 | +result_3 = array_1 == array_2 |
| 203 | +print("array_1 == array_2:",result_3) |
| 204 | +``` |
| 205 | +**Output:** |
| 206 | +``` |
| 207 | +array_1 == array_2: [False True False] |
| 208 | +``` |
| 209 | + |
| 210 | +**not equal to operator** |
| 211 | + |
| 212 | +The `!=` operator returns `True` if the value of operand on left is not equal to the value of operand on right. |
| 213 | + |
| 214 | +**Code** |
| 215 | +```python |
| 216 | +import numpy as np |
| 217 | +array_1 = np.array([12,15,20]) |
| 218 | +array_2 = np.array([20,15,12]) |
| 219 | +result_3 = array_1 != array_2 |
| 220 | +print("array_1 != array_2:",result_3) |
| 221 | +``` |
| 222 | +**Output:** |
| 223 | +``` |
| 224 | +array_1 != array_2: [True False True] |
| 225 | +``` |
| 226 | + |
| 227 | +<br> |
| 228 | + |
| 229 | +## NumPy Logical Operations |
| 230 | + |
| 231 | +Logical operators perform Boolean algebra. A branch of algebra that deals with `True` and `False` statements. |
| 232 | + |
| 233 | +It illustrates the logical operations of AND, OR, and NOT using np.logical_and(), np.logical_or(), and np.logical_not() functions, respectively. |
| 234 | + |
| 235 | +**Logical AND** |
| 236 | + |
| 237 | +Evaluates the element-wise truth value of `array_1` AND `array_2` |
| 238 | + |
| 239 | +**Code** |
| 240 | +```python |
| 241 | +import numpy as np |
| 242 | +array_1 = np.array([True, False, True]) |
| 243 | +array_2 = np.array([False, False, True]) |
| 244 | +print(np.logical_and(array_1, array_2)) |
| 245 | +``` |
| 246 | +**Output:** |
| 247 | +``` |
| 248 | +[False False True] |
| 249 | +``` |
| 250 | + |
| 251 | +**Logical OR** |
| 252 | + |
| 253 | +Evaluates the element-wise truth value of `array_1` OR `array_2` |
| 254 | + |
| 255 | +**Code** |
| 256 | +```python |
| 257 | +import numpy as np |
| 258 | +array_1 = np.array([True, False, True]) |
| 259 | +array_2 = np.array([False, False, True]) |
| 260 | +print(np.logical_or(array_1, array_2)) |
| 261 | +``` |
| 262 | +**Output:** |
| 263 | +``` |
| 264 | +[True False True] |
| 265 | +``` |
| 266 | + |
| 267 | +**Logical NOT** |
| 268 | + |
| 269 | +Evaluates the element-wise truth value of `array_1` NOT `array_2` |
| 270 | + |
| 271 | +**Code** |
| 272 | +```python |
| 273 | +import numpy as np |
| 274 | +array_1 = np.array([True, False, True]) |
| 275 | +array_2 = np.array([False, False, True]) |
| 276 | +print(np.logical_not(array_1)) |
| 277 | +``` |
| 278 | +**Output:** |
| 279 | +``` |
| 280 | +[False True False] |
| 281 | +``` |
0 commit comments