Binary operations in NumPy involve element-wise operations between two arrays. Here are some examples of binary operations on NumPy arrays:

- Element-wise addition:

import numpy as np # Create arrays arr1 = np.array([1, 2, 3]) arr2 = np.array([4, 5, 6]) # Element-wise addition result = arr1 + arr2 print(result) # [5 7 9]

- Element-wise subtraction:

import numpy as np # Create arrays arr1 = np.array([1, 2, 3]) arr2 = np.array([4, 5, 6]) # Element-wise subtraction result = arr1 - arr2 print(result) # [-3 -3 -3]

- Element-wise multiplication:

import numpy as np # Create arrays arr1 = np.array([1, 2, 3]) arr2 = np.array([4, 5, 6]) # Element-wise multiplication result = arr1 * arr2 print(result) # [ 4 10 18]

- Element-wise division:

import numpy as np # Create arrays arr1 = np.array([10, 20, 30]) arr2 = np.array([2, 5, 3]) # Element-wise division result = arr1 / arr2 print(result) # [ 5. 4. 10.]

- Element-wise exponentiation:

import numpy as np # Create array arr = np.array([2, 3, 4]) # Element-wise exponentiation result = np.power(arr, 2) print(result) # [ 4 9 16]

These examples demonstrate various binary operations, such as addition, subtraction, multiplication, division, and exponentiation, performed element-wise between two NumPy arrays. The resulting array contains the corresponding operation applied to each pair of elements from the input arrays.

Here are a few more examples of binary operations on NumPy arrays based on relational operators:

- Element-wise comparison:

import numpy as np # Create arrays arr1 = np.array([1, 2, 3]) arr2 = np.array([2, 2, 2]) # Element-wise comparison (greater than) result = arr1 > arr2 print(result) # [False False True]

In this example, the comparison `arr1 > arr2`

returns an array with `True`

for elements where `arr1`

is greater than `arr2`

, and `False`

otherwise.

- Element-wise logical operations:

import numpy as np # Create arrays arr1 = np.array([True, True, False]) arr2 = np.array([False, True, True]) # Element-wise logical AND result_and = np.logical_and(arr1, arr2) # Element-wise logical OR result_or = np.logical_or(arr1, arr2) print(result_and) # [False True False] print(result_or) # [ True True True]

Here, `np.logical_and`

performs an element-wise logical AND operation between `arr1`

and `arr2`

, while `np.logical_or`

performs an element-wise logical OR operation.

- Element-wise bitwise operations:

import numpy as np # Create arrays arr1 = np.array([2, 5, 7]) arr2 = np.array([3, 4, 6]) # Element-wise bitwise AND result_and = np.bitwise_and(arr1, arr2) # Element-wise bitwise OR result_or = np.bitwise_or(arr1, arr2) print(result_and) # [2 4 6] print(result_or) # [3 5 7]

Here, `np.bitwise_and`

performs an element-wise bitwise AND operation between `arr1`

and `arr2`

, while `np.bitwise_or`

performs an element-wise bitwise OR operation.

These examples demonstrate additional binary operations available in NumPy, including element-wise comparison, logical operations (AND, OR), and bitwise operations (AND, OR).

Here are a few more examples of binary operations on NumPy arrays:

- Element-wise maximum and minimum:

import numpy as np # Create arrays arr1 = np.array([1, 3, 5]) arr2 = np.array([2, 4, 6]) # Element-wise maximum result_max = np.maximum(arr1, arr2) # Element-wise minimum result_min = np.minimum(arr1, arr2) print(result_max) # [2 4 6] print(result_min) # [1 3 5]

The `np.maximum`

function returns an array containing the element-wise maximum between `arr1`

and `arr2`

, while `np.minimum`

returns an array with the element-wise minimum.

- Element-wise equality and inequality:

import numpy as np # Create arrays arr1 = np.array([1, 2, 3]) arr2 = np.array([2, 2, 2]) # Element-wise equality result_equal = np.equal(arr1, arr2) # Element-wise inequality result_not_equal = np.not_equal(arr1, arr2) print(result_equal) # [False True False] print(result_not_equal) # [ True False True]

The `np.equal`

function compares elements of `arr1`

and `arr2`

element-wise for equality and returns a boolean array. Similarly, `np.not_equal`

compares elements for inequality.

- Element-wise bitwise shifts:

import numpy as np # Create array arr = np.array([4, 8, 16]) # Element-wise left shift result_left_shift = np.left_shift(arr, 2) # Element-wise right shift result_right_shift = np.right_shift(arr, 1) print(result_left_shift) # [16 32 64] print(result_right_shift) # [2 4 8]

The `np.left_shift`

function performs an element-wise left shift operation on the elements of `arr`

, while `np.right_shift`

performs an element-wise right shift operation.

These examples demonstrate additional binary operations in NumPy, including element-wise maximum, minimum, equality, inequality, and bitwise shifts. NumPy provides a wide range of binary operations to manipulate and compare arrays element-wise.

Here are a few more examples of binary operations on NumPy arrays:

- Element-wise logical XOR:

import numpy as np # Create arrays arr1 = np.array([True, True, False]) arr2 = np.array([False, True, True]) # Element-wise logical XOR result_xor = np.logical_xor(arr1, arr2) print(result_xor) # [ True False True]

The `np.logical_xor`

function performs an element-wise logical XOR (exclusive OR) operation between `arr1`

and `arr2`

.

- Element-wise division with floor division:

import numpy as np # Create arrays arr1 = np.array([10, 20, 30]) arr2 = np.array([3, 5, 7]) # Element-wise division with floor division result_floor_div = np.floor_divide(arr1, arr2) print(result_floor_div) # [3 4 4]

The `np.floor_divide`

function performs an element-wise division between `arr1`

and `arr2`

and rounds the result down to the nearest whole number.

- Element-wise bitwise XOR:

import numpy as np # Create arrays arr1 = np.array([5, 10, 15]) arr2 = np.array([3, 6, 9]) # Element-wise bitwise XOR result_bitwise_xor = np.bitwise_xor(arr1, arr2) print(result_bitwise_xor) # [6 12 6]

The `np.bitwise_xor`

a function performs an element-wise bitwise XOR operation between `arr1`

and `arr2`

.

These examples showcase additional binary operations available in NumPy, including logical XOR, division with floor division, and bitwise XOR. NumPy provides a comprehensive set of binary operations to manipulate and perform element-wise operations on arrays.