Operations on NumPy Arrays

NumPy arrays support a wide range of operations, making it a powerful library for numerical computations. These include:

  1. Arithmetic operations.
  2. Universal functions (like sin(), cos(), log()).
  3. Aggregation operations (like sum(), mean(), std()).

Arithmetic Operations

NumPy allows element-wise arithmetic operations between arrays or between arrays and scalars.

Examples

import numpy as np
 
# Create arrays
array1 = np.array([1, 2, 3])
array2 = np.array([4, 5, 6])
 
# Addition
print("Addition:", array1 + array2)  # Output: [5 7 9]
 
# Subtraction
print("Subtraction:", array1 - array2)  # Output: [-3 -3 -3]
 
# Multiplication
print("Multiplication:", array1 * array2)  # Output: [4 10 18]
 
# Division
print("Division:", array1 / array2)  # Output: [0.25 0.4  0.5 ]
 
# Scalar operations
print("Scalar Multiplication:", array1 * 2)  # Output: [2 4 6]

Universal Functions (ufuncs)

Universal functions operate element-wise on arrays. Some commonly used ufuncs are:

  • np.sin()
  • np.cos()
  • np.log()
  • np.exp()

Examples

# Create an array
array = np.array([0, np.pi / 2, np.pi])
 
# Sine function
print("Sine:", np.sin(array))  # Output: [0. 1. 0.]
 
# Cosine function
print("Cosine:", np.cos(array))  # Output: [ 1.  0. -1.]
 
# Logarithm (natural log)
log_array = np.array([1, np.e, np.e**2])
print("Logarithm:", np.log(log_array))  # Output: [0. 1. 2.]
 
# Exponential
print("Exponential:", np.exp([1, 2, 3]))  # Output: [ 2.71828183  7.3890561  20.08553692]

Aggregation Operations

Aggregation functions compute a single value from an array, such as the sum, mean, or standard deviation.

Common Aggregation Functions

FunctionDescription
np.sum()Sum of all elements
np.mean()Mean (average) of elements
np.std()Standard deviation
np.min()Minimum value
np.max()Maximum value

Examples

# Create an array
array = np.array([1, 2, 3, 4, 5])
 
# Sum
print("Sum:", np.sum(array))  # Output: 15
 
# Mean
print("Mean:", np.mean(array))  # Output: 3.0
 
# Standard Deviation
print("Standard Deviation:", np.std(array))  # Output: 1.4142135623730951
 
# Min and Max
print("Minimum:", np.min(array))  # Output: 1
print("Maximum:", np.max(array))  # Output: 5

Try It Yourself

Problem 1: Perform Arithmetic Operations

Create two arrays and perform addition, subtraction, multiplication, and division. Print the results.

Show Code
import numpy as np
 
# Arrays
array1 = np.array([10, 20, 30])
array2 = np.array([1, 2, 3])
 
# Perform operations
print("Addition:", array1 + array2)
print("Subtraction:", array1 - array2)
print("Multiplication:", array1 * array2)
print("Division:", array1 / array2)

Problem 2: Use Universal Functions

Create an array of angles (in radians) and compute the sine, cosine, and exponential values.

Show Code
import numpy as np
 
# Angles in radians
angles = np.array([0, np.pi / 4, np.pi / 2])
 
# Compute trigonometric values
print("Sine:", np.sin(angles))
print("Cosine:", np.cos(angles))
print("Exponential:", np.exp(angles))

Problem 3: Aggregate Array Data

Create an array and find its sum, mean, standard deviation, and maximum value.

Show Code
import numpy as np
 
# Create an array
array = np.array([4, 7, 1, 8, 3])
 
# Perform aggregations
print("Sum:", np.sum(array))
print("Mean:", np.mean(array))
print("Standard Deviation:", np.std(array))
print("Maximum:", np.max(array))

Pyground

Play with Python!

Output: