Home » NumPy Binary Operators Tutorial

NumPy Binary Operators Tutorial

Java SE 11 Programmer II [1Z0-816] Practice Tests
1 Year Subscription
Oracle Java Certification
Java SE 11 Programmer I [1Z0-815] Practice Tests
Spring Framework Basics Video Course
Java SE 11 Developer (Upgrade) [1Z0-817]

NumPy provides a variety of binary operators that allow for element-wise operations between arrays.

Binary operations in NumPy typically work on two arrays (or an array and a scalar) and perform operations like addition, subtraction, multiplication, etc.

Here’s a detailed guide covering the major binary operations with several examples.

Binary operators in NumPy perform operations on two operands. The operands can be two arrays or an array and a scalar, and each operation is performed element-wise.

1. Importing NumPy

Make sure to import NumPy before performing any operations.

import numpy as np

2. Common Binary Operators

2.1 Addition (+ or np.add())

Adds two arrays or an array and a scalar element-wise.

# Define two arrays
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])

# Array addition
add_result = a + b
print("Addition Result:\n", add_result)

# Using np.add
add_result_func = np.add(a, b)
print("Addition Result using np.add:\n", add_result_func)

# Adding a scalar
add_scalar = a + 10
print("Adding Scalar Result:\n", add_scalar)

2.2 Subtraction (- or np.subtract())

Subtracts the elements of one array from another, element-wise.

# Array subtraction
sub_result = a - b
print("Subtraction Result:\n", sub_result)

# Using np.subtract
sub_result_func = np.subtract(a, b)
print("Subtraction Result using np.subtract:\n", sub_result_func)

# Subtracting a scalar
sub_scalar = b - 2
print("Subtracting Scalar Result:\n", sub_scalar)

2.3 Multiplication (* or np.multiply())

Performs element-wise multiplication.

# Array multiplication
mul_result = a * b
print("Multiplication Result:\n", mul_result)

# Using np.multiply
mul_result_func = np.multiply(a, b)
print("Multiplication Result using np.multiply:\n", mul_result_func)

# Multiplying with a scalar
mul_scalar = a * 3
print("Multiplying with Scalar Result:\n", mul_scalar)

2.4 Division (/ or np.divide())

Performs element-wise division.

# Array division
div_result = b / a
print("Division Result:\n", div_result)

# Using np.divide
div_result_func = np.divide(b, a)
print("Division Result using np.divide:\n", div_result_func)

# Dividing with a scalar
div_scalar = a / 2
print("Dividing with Scalar Result:\n", div_scalar)

2.5 Floor Division (// or np.floor_divide())

Performs element-wise floor division (rounds down the result of division to the nearest integer).

# Array floor division
floor_div_result = b // a
print("Floor Division Result:\n", floor_div_result)

# Using np.floor_divide
floor_div_result_func = np.floor_divide(b, a)
print("Floor Division Result using np.floor_divide:\n", floor_div_result_func)

# Floor division with a scalar
floor_div_scalar = b // 3
print("Floor Division with Scalar Result:\n", floor_div_scalar)

2.6 Modulus (% or np.mod())

Calculates the remainder of element-wise division.

# Array modulus
mod_result = b % a
print("Modulus Result:\n", mod_result)

# Using np.mod
mod_result_func = np.mod(b, a)
print("Modulus Result using np.mod:\n", mod_result_func)

# Modulus with a scalar
mod_scalar = b % 3
print("Modulus with Scalar Result:\n", mod_scalar)

2.7 Exponentiation (** or np.power())

Raises each element of one array to the power of the corresponding element in another array, element-wise.

# Array exponentiation
exp_result = a ** 2
print("Exponentiation Result:\n", exp_result)

# Using np.power
exp_result_func = np.power(a, 2)
print("Exponentiation Result using np.power:\n", exp_result_func)

# Exponentiation with a scalar
exp_scalar = b ** 2
print("Exponentiation with Scalar Result:\n", exp_scalar)

3. Comparison Operators

Comparison operators in NumPy compare elements of two arrays (or an array and a scalar) and return a boolean array.

3.1 Equal (== or np.equal())

Checks if elements in both arrays are equal.

# Element-wise equality check
equal_result = a == b
print("Equality Check Result:\n", equal_result)

# Using np.equal
equal_result_func = np.equal(a, b)
print("Equality Check using np.equal:\n", equal_result_func)

3.2 Not Equal (!= or np.not_equal())

Checks if elements in both arrays are not equal.

# Element-wise inequality check
not_equal_result = a != b
print("Not Equal Check Result:\n", not_equal_result)

# Using np.not_equal
not_equal_result_func = np.not_equal(a, b)
print("Not Equal Check using np.not_equal:\n", not_equal_result_func)

3.3 Greater Than (> or np.greater())

Checks if elements in the first array are greater than those in the second array.

# Greater than check
greater_result = a > b
print("Greater Than Check Result:\n", greater_result)

# Using np.greater
greater_result_func = np.greater(a, b)
print("Greater Than Check using np.greater:\n", greater_result_func)

3.4 Less Than (< or np.less())

Checks if elements in the first array are less than those in the second array.

# Less than check
less_result = a < b
print("Less Than Check Result:\n", less_result)

# Using np.less
less_result_func = np.less(a, b)
print("Less Than Check using np.less:\n", less_result_func)

3.5 Greater Than or Equal (>= or np.greater_equal())

Checks if elements in the first array are greater than or equal to those in the second array.

# Greater than or equal check
greater_equal_result = a >= b
print("Greater Than or Equal Check Result:\n", greater_equal_result)

# Using np.greater_equal
greater_equal_result_func = np.greater_equal(a, b)
print("Greater Than or Equal Check using np.greater_equal:\n", greater_equal_result_func)

3.6 Less Than or Equal (<= or np.less_equal())

Checks if elements in the first array are less than or equal to those in the second array.

# Less than or equal check
less_equal_result = a <= b
print("Less Than or Equal Check Result:\n", less_equal_result)

# Using np.less_equal
less_equal_result_func = np.less_equal(a, b)
print("Less Than or Equal Check using np.less_equal:\n", less_equal_result_func)

4. Logical Operations

Logical operations like and, or, and not can be performed element-wise with np.logical_and, np.logical_or, and np.logical_not.

4.1 Logical AND (np.logical_and())

Performs element-wise logical AND between two arrays.

# Logical AND
logical_and_result = np.logical_and(a > 1, b > 4)
print("Logical AND Result:\n", logical_and_result)

4.2 Logical OR (np.logical_or())

Performs element-wise logical OR between two arrays.

# Logical OR
logical_or_result = np.logical_or(a < 3, b > 5)
print("Logical OR Result:\n", logical_or_result)

4.3 Logical NOT (np.logical_not())

Inverts the boolean values element-wise.

# Logical NOT
logical_not_result = np.logical_not(a > 1)
print("Logical NOT Result:\n", logical_not_result)

5. Practical Examples

5.1 Masking Elements with Conditions

Using binary operators to filter elements from an array.

# Define an array
arr = np.array([10, 20, 30, 40, 50])

# Mask elements greater than 25
mask = arr > 25
filtered_arr = arr[mask]
print("Filtered Array (elements > 25):\n", filtered_arr)

5.2 Combining Multiple Conditions

You can use binary operators to combine conditions.

# Define an array
arr = np.array([10, 20, 30, 40, 50])

# Elements that are greater than 20 and less than 50
combined_condition = np.logical_and(arr > 20, arr < 50)
filtered_combined = arr[combined_condition]
print("Filtered Array with Combined Condition:\n", filtered_combined)

This tutorial provides a foundation on binary operators in NumPy.

With these tools, you can perform a variety of element-wise operations and comparisons on arrays, making data manipulation easier and more efficient!

You may also like

Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More