</>
Skip to content
NumPy lessons (28/28)

NumPy — Set Operations

Arithmetic

import numpy as np

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

print(a + b)    # [5, 7, 9]
print(a - b)    # [-3, -3, -3]
print(a * b)    # [4, 10, 18]
print(a / b)    # [0.25, 0.4, 0.5]
print(a ** b)   # [1, 32, 729]

Trigonometric

arr = np.array([0, np.pi/4, np.pi/2])
print(np.sin(arr))
print(np.cos(arr))
print(np.tan(arr))

Logarithmic

arr = np.array([1, 10, 100])
print(np.log(arr))    # Natural log
print(np.log10(arr))  # Base 10
print(np.log2(arr))   # Base 2

Exponential

arr = np.array([0, 1, 2, 3])
print(np.exp(arr))
print(np.exp2(arr))

Aggregations

arr = np.array([1, 2, 3, 4, 5])
print(np.sum(arr))
print(np.prod(arr))
print(np.cumsum(arr))
print(np.cumprod(arr))

Mini Practice

  1. Perform arithmetic
  2. Use trigonometric functions
  3. Calculate logarithms
  4. Practice aggregations

Up Next

Continue with Statistics - Statistical functions.

Related Topics

Frequently Asked Questions about Set Operations

What is Set Operations in NumPy?

Set Operations is a fundamental concept in NumPy. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn Set Operations?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Set Operations.

Why is Set Operations important in NumPy?

Set Operations is essential for NumPy development. Understanding this concept will help you write better code and solve real-world problems more effectively.