How to Use NumPy’s Basic Mathematical Functions

Updated: January 22, 2024 By: Guest Contributor Post a comment

Introduction

NumPy is a core library for numerical computing in Python, providing a high-performance multidimensional array object, and tools for working with these arrays. Understanding basic mathematical functions in NumPy is essential for anyone handling data analysis or scientific computing tasks. In this tutorial, we’ll cover a range of NumPy mathematical functions with a focus on their practical application.

Setting Up Your Environment

Before diving into NumPy’s mathematical functions, you should have Python and NumPy installed. You can install NumPy using pip:

pip install numpy

Understanding NumPy Arrays

At the core of NumPy is the ndarray, or N-dimensional array. You can create a NumPy array using the array function:

import numpy as np

# Creating a simple NumPy array
a = np.array([1, 2, 3])
print(a)

Output:

 array([1, 2, 3])

Basic Arithmetic Functions

NumPy allows you to perform element-wise operations on arrays efficiently. Here are some basic arithmetic functions:

  • Addition: np.add
  • Subtraction: np.subtract
  • Multiplication: np.multiply
  • Division: np.divide
  • Modulus: np.mod

Example:

# Creating two NumPy arrays
b = np.array([4, 5, 6])
c = np.array([2, 3, 4])

# Performing element-wise addition
sum_array = np.add(b, c)
print('Sum:', sum_array)

Output:

Sum: [6 8 10]

Further Mathematical Functions

Beyond basic arithmetic, NumPy supports a broad range of mathematical operations. Here are a few with their respective code examples:

Exponents and Logarithms

# Exponents
expo_array = np.exp(c)
print('Exponential:', expo_array)

# Logarithms
log_array = np.log(a)
print('Natural Logarithm:', log_array)

Output:

Exponential: [ 7.3890561 20.08553692 54.59815003] Natural Logarithm: [0. 0.69314718 1.09861229]

Trigonometric Functions

d = np.array([0, np.pi/2, np.pi])

# Sine function
sin_array = np.sin(d)
print('Sine:', sin_array)

Output:

Sine: [0.0000e+00 1.0000e+00 1.2246e-16]

Statistical Functions

NumPy also provides functions for statistical analysis such as mean, median, and standard deviation:

e = np.array([1, 5, 3, 8, 9])

# Mean
mean_val = np.mean(e)
print('Mean:', mean_val)

# Median
median_val = np.median(e)
print('Median:', median_val)

# Standard Deviation
std_val = np.std(e)
print('Standard Deviation:', std_val)

Output: Mean:

5.20 Median: 5.0 Standard Deviation: 2.9706864235520193

Advanced Mathematical Operations

NumPy also excels in handling more complex mathematical tasks like linear algebra, polynomials, and more.

Linear Algebra Functions

NumPy’s sub-module linalg offers many functions related to linear algebra, including matrix multiplication, determinants, and solving linear equations:

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

# Matrix multiplication
mat_mul = np.dot(f, g)
print('Matrix Multiplication:\n', mat_mul)

Output:

Matrix Multiplication: [[19 22] [43 50]]

Polynomial Functions

You can use NumPy to define and manipulate polynomials as well. NumPy has a special sub-module polynomial dedicated to polynomial operations:

from numpy.polynomial import Polynomial as poly
p1 = poly([1, 2, 3])
print('Polynomial:', p1)

Output:

Polynomial: 1.0 + 2.0*x**1 + 3.0*x**2

Conclusion

In the world of scientific computing and data analysis, NumPy is an essential library. Its range of mathematical functions, combined with the performance of its core array data structure, enables efficient and complex calculations. By mastering these functions, you can simplify and accelerate many computational tasks.