Numpy Arithmetic Operations with Examples

Arithmetic operations are fundamental, playing an important role in science and computer applications. In data manipulation and numerical computation, NumPy (Numerical Python) stands out as a powerful library that provides a wide range of mathematical functions for efficient data manipulation.

In this beginner-friendly course, we will explore the important arithmetic functions of NumPy and their use to perform element-wise operations on arrays. Let’s learn how to do it!

Why NumPy Arithmetic Operations Matter?

NumPy is a popular library in the Python ecosystem, known for its ability to perform numerical calculations. When it comes to arithmetic operations on arrays, it offers several advantages:

Performance: NumPy programs are optimized for speed, making them significantly faster than traditional Python programs.

Flexibility: NumPy simplifies complex numerical operations by providing a unified interface that allows you to manage arrays.

Propagation: NumPy allows you to perform operations on arrays of different sizes, thanks to its propagation feature that changes the dimensions automatically.

Versatility: NumPy supports a wide range of mathematical functions and applications, making it suitable for a variety of scientific and technical applications.

Now, let’s dive into some of the most commonly used arithmetic operations in NumPy.

List of NumPy Arithmetic Operations

Here is a list of commonly used arithmetic operations in NumPy, along with their associated operators and built-in functions:

Element-wise Operation Operator Function
Addition + add()
Subtraction subtract()
Multiplication * multiply()
Division / divide()
Exponentiation ** power()
Modulus % mod()

To perform each operation, you can choose either the associated operator or the corresponding built-in function. For example, to perform addition, you can use either the + operator or the add() function.

NumPy Array Element-Wise Addition

Element-wise addition allows you to add corresponding elements of two NumPy arrays.

Let’s see how to do this using both the + operator and the add() function:

import numpy as np

first_array = np.array([1, 3, 5, 7])
second_array = np.array([2, 4, 6, 8])

# Using the + operator
result1 = first_array + second_array
print("Using the + operator:", result1)

# Using the add() function
result2 = np.add(first_array, second_array)
print("Using the add() function:", result2)

In this example, we create two arrays, first_array and second_array, and then perform element-wise addition using both the + operator and the add() function. As you can see, both methods produce the same result.

Output:

Using the + operator: [ 3 7 11 15]
Using the add() function: [ 3 7 11 15]

Both + and add() deliver the same outcome, giving you the flexibility to choose the method that suits your coding style.

NumPy Array Element-Wise Subtraction

Similar to addition, subtraction in NumPy can be accomplished using either the – operator or the subtract() function:

import numpy as np

first_array = np.array([3, 9, 27, 81])
second_array = np.array([2, 4, 8, 16])

# Using the - operator
result1 = first_array - second_array
print("Using the - operator:", result1)

# Using the subtract() function
result2 = np.subtract(first_array, second_array)
print("Using the subtract() function:", result2)

This code snippet demonstrates subtraction between two NumPy arrays, first_array and second_array, using both the – operator and the subtract() function.

Output:

Using the – operator: [ 1 5 19 65]
Using the subtract() function: [ 1 5 19 65]

Again, you can choose either method based on your preference.

NumPy Array Element-Wise Multiplication

Element-wise multiplication involves multiplying the corresponding elements of two NumPy arrays.

You can achieve this using the * operator or the multiply() function:

import numpy as np

first_array = np.array([1, 3, 5, 7])
second_array = np.array([2, 4, 6, 8])

# Using the * operator
result1 = first_array * second_array
print("Using the * operator:", result1)

# Using the multiply() function
result2 = np.multiply(first_array, second_array)
print("Using the multiply() function:", result2)

In this example, we demonstrate two ways to multiply the two arrays element-wise, utilizing the * operator and the multiply() function.

Output:
Using the * operator: [ 2 12 30 56]
Using the multiply() function: [ 2 12 30 56]

Both approaches provide the same result, allowing you to choose the one that fits your coding style.

NumPy Array Element-Wise Division

Element-wise division entails dividing the corresponding elements of two NumPy arrays.

You can perform this operation using either the / operator or the divide() function:

import numpy as np

first_array = np.array([1, 2, 3])
second_array = np.array([4, 5, 6])

# Using the / operator
result1 = first_array / second_array
print("Using the / operator:", result1)

# Using the divide() function
result2 = np.divide(first_array, second_array)
print("Using the divide() function:", result2)

This code snippet demonstrates element-wise division between two NumPy arrays, first_array and second_array, using both the / operator and the divide() function.

Output:
Using the / operator: [0.25 0.4 0.5 ]
Using the divide() function: [0.25 0.4 0.5 ]

As shown, both the / operator and the divide() function yield the same result.

NumPy Array Element-Wise Exponentiation

Element-wise exponentiation involves raising each element of an array to a given power. NumPy offers two options for performing this operation: the ** operator and the power() function.

import numpy as np

array1 = np.array([1, 2, 3])

# Using the ** operator
result1 = array1 ** 2
print("Using the ** operator:", result1)

# Using the power() function
result2 = np.power(array1, 2)
print("Using the power() function:", result2)

In this example, we use the ** operator and the power() function to perform the element-wise exponentiation operation on the array1 array, raising each element to the power of 2.

Output:
Using the ** operator: [1 4 9]
Using the power() function: [1 4 9]

Both the ** operator and the power() function achieve the same result by raising each element of array1 to the power of 2.

NumPy Array Element-Wise Modulus

The modulus operation in NumPy arrays calculates the remainder of element-wise division between two arrays.

You can perform this operation using either the % operator or the mod() function:

import numpy as np

first_array = np.array([9, 10, 20])
second_array = np.array([2, 5, 7])

# Using the % operator
result1 = first_array % second_array
print("Using the % operator:", result1)

# Using the mod() function
result2 = np.mod(first_array, second_array)
print("Using the mod() function:", result2)

This code demonstrates element-wise modulus operations using both the % operator and the mod() function with the first_array and second_array.

Output:
Using the % operator: [1 0 6]
Using the mod() function: [1 0 6]

As illustrated, both the % operator and the mod() function yield the same result, making it convenient for you to choose the method that suits your coding style.

Arithmetic Operations Table:

Function Description
add(x1, x2, …) Perform element-wise addition.
reciprocal(x, …) Calculate the element-wise reciprocal of a number.
positive(x, …) Obtain the element-wise positive value of a number.
negative(x, …) Determine the element-wise negative value of a number.
multiply(x1, x2, …) Perform element-wise multiplication.
divide(x1, x2, …) Perform element-wise division.
power(x1, x2, …) Raise elements of the first array to the power of the elements in the second array, element-wise.
subtract(x1, x2, …) Perform element-wise subtraction.
true_divide(x1, x2, …) Perform element-wise division, ensuring accurate floating-point results.
floor_divide(x1, x2, …) Calculate the largest integer smaller than or equal to the division of the inputs.
float_power(x1, x2, …) Raise elements of the first array to the power of the elements in the second array, element-wise, with support for floating-point powers.
fmod(x1, x2, …) Calculate the element-wise remainder of division.
mod(x1, x2, …) Calculate the element-wise remainder of division, adhering to specified casting rules.
modf(x, …) Split an array into its fractional and integral parts, element-wise.
remainder(x1, x2, …) Calculate the element-wise remainder of division, following specified casting rules.
divmod(x1, x2, …) Calculate element-wise quotient and remainder simultaneously, with optional output arrays.

NumPy’s array arithmetic operations are essential for working with large amounts of numerical data efficiently in Python. This tutorial covers some of the most common arithmetic operations in NumPy, including addition, subtraction, multiplication, division, exponentiation, and modulus.

We show how to perform these operations using both operators and built-in functions, giving you flexibility and versatility in your data manipulation tasks. By mastering these operations, you’ll be well-prepared to tackle a wide range of numerical computing challenges with NumPy.

In simpler terms:

NumPy is a Python library that makes it easy to work with numerical data. Array arithmetic operations are a fundamental part of NumPy, and they allow you to perform complex calculations on large amounts of data quickly and efficiently.

Conclusion

This tutorial covered the most common array arithmetic operations in NumPy, and it showed you how to perform them using both operators and built-in functions. By learning how to use these operations, you’ll be able to tackle a wide range of numerical computing challenges with NumPy.
Happy coding!