
- NumPy - Home
- NumPy - Introduction
- NumPy - Environment
- NumPy Arrays
- NumPy - Ndarray Object
- NumPy - Data Types
- NumPy Creating and Manipulating Arrays
- NumPy - Array Creation Routines
- NumPy - Array Manipulation
- NumPy - Array from Existing Data
- NumPy - Array From Numerical Ranges
- NumPy - Iterating Over Array
- NumPy - Reshaping Arrays
- NumPy - Concatenating Arrays
- NumPy - Stacking Arrays
- NumPy - Splitting Arrays
- NumPy - Flattening Arrays
- NumPy - Transposing Arrays
- NumPy Indexing & Slicing
- NumPy - Indexing & Slicing
- NumPy - Indexing
- NumPy - Slicing
- NumPy - Advanced Indexing
- NumPy - Fancy Indexing
- NumPy - Field Access
- NumPy - Slicing with Boolean Arrays
- NumPy Array Attributes & Operations
- NumPy - Array Attributes
- NumPy - Array Shape
- NumPy - Array Size
- NumPy - Array Strides
- NumPy - Array Itemsize
- NumPy - Broadcasting
- NumPy - Arithmetic Operations
- NumPy - Array Addition
- NumPy - Array Subtraction
- NumPy - Array Multiplication
- NumPy - Array Division
- NumPy Advanced Array Operations
- NumPy - Swapping Axes of Arrays
- NumPy - Byte Swapping
- NumPy - Copies & Views
- NumPy - Element-wise Array Comparisons
- NumPy - Filtering Arrays
- NumPy - Joining Arrays
- NumPy - Sort, Search & Counting Functions
- NumPy - Searching Arrays
- NumPy - Union of Arrays
- NumPy - Finding Unique Rows
- NumPy - Creating Datetime Arrays
- NumPy - Binary Operators
- NumPy - String Functions
- NumPy - Matrix Library
- NumPy - Linear Algebra
- NumPy - Matplotlib
- NumPy - Histogram Using Matplotlib
- NumPy Sorting and Advanced Manipulation
- NumPy - Sorting Arrays
- NumPy - Sorting along an axis
- NumPy - Sorting with Fancy Indexing
- NumPy - Structured Arrays
- NumPy - Creating Structured Arrays
- NumPy - Manipulating Structured Arrays
- NumPy - Record Arrays
- Numpy - Loading Arrays
- Numpy - Saving Arrays
- NumPy - Append Values to an Array
- NumPy - Swap Columns of Array
- NumPy - Insert Axes to an Array
- NumPy Handling Missing Data
- NumPy - Handling Missing Data
- NumPy - Identifying Missing Values
- NumPy - Removing Missing Data
- NumPy - Imputing Missing Data
- NumPy Performance Optimization
- NumPy - Performance Optimization with Arrays
- NumPy - Vectorization with Arrays
- NumPy - Memory Layout of Arrays
- Numpy Linear Algebra
- NumPy - Linear Algebra
- NumPy - Matrix Library
- NumPy - Matrix Addition
- NumPy - Matrix Subtraction
- NumPy - Matrix Multiplication
- NumPy - Element-wise Matrix Operations
- NumPy - Dot Product
- NumPy - Matrix Inversion
- NumPy - Determinant Calculation
- NumPy - Eigenvalues
- NumPy - Eigenvectors
- NumPy - Singular Value Decomposition
- NumPy - Solving Linear Equations
- NumPy - Matrix Norms
- NumPy Element-wise Matrix Operations
- NumPy - Sum
- NumPy - Mean
- NumPy - Median
- NumPy - Min
- NumPy - Max
- NumPy Set Operations
- NumPy - Unique Elements
- NumPy - Intersection
- NumPy - Union
- NumPy - Difference
- NumPy Random Number Generation
- NumPy - Random Generator
- NumPy - Permutations & Shuffling
- NumPy - Uniform distribution
- NumPy - Normal distribution
- NumPy - Binomial distribution
- NumPy - Poisson distribution
- NumPy - Exponential distribution
- NumPy - Rayleigh Distribution
- NumPy - Logistic Distribution
- NumPy - Pareto Distribution
- NumPy - Visualize Distributions With Sea born
- NumPy - Matplotlib
- NumPy - Multinomial Distribution
- NumPy - Chi Square Distribution
- NumPy - Zipf Distribution
- NumPy File Input & Output
- NumPy - I/O with NumPy
- NumPy - Reading Data from Files
- NumPy - Writing Data to Files
- NumPy - File Formats Supported
- NumPy Mathematical Functions
- NumPy - Mathematical Functions
- NumPy - Trigonometric functions
- NumPy - Exponential Functions
- NumPy - Logarithmic Functions
- NumPy - Hyperbolic functions
- NumPy - Rounding functions
- NumPy Fourier Transforms
- NumPy - Discrete Fourier Transform (DFT)
- NumPy - Fast Fourier Transform (FFT)
- NumPy - Inverse Fourier Transform
- NumPy - Fourier Series and Transforms
- NumPy - Signal Processing Applications
- NumPy - Convolution
- NumPy Polynomials
- NumPy - Polynomial Representation
- NumPy - Polynomial Operations
- NumPy - Finding Roots of Polynomials
- NumPy - Evaluating Polynomials
- NumPy Statistics
- NumPy - Statistical Functions
- NumPy - Descriptive Statistics
- NumPy Datetime
- NumPy - Basics of Date and Time
- NumPy - Representing Date & Time
- NumPy - Date & Time Arithmetic
- NumPy - Indexing with Datetime
- NumPy - Time Zone Handling
- NumPy - Time Series Analysis
- NumPy - Working with Time Deltas
- NumPy - Handling Leap Seconds
- NumPy - Vectorized Operations with Datetimes
- NumPy ufunc
- NumPy - ufunc Introduction
- NumPy - Creating Universal Functions (ufunc)
- NumPy - Arithmetic Universal Function (ufunc)
- NumPy - Rounding Decimal ufunc
- NumPy - Logarithmic Universal Function (ufunc)
- NumPy - Summation Universal Function (ufunc)
- NumPy - Product Universal Function (ufunc)
- NumPy - Difference Universal Function (ufunc)
- NumPy - Finding LCM with ufunc
- NumPy - ufunc Finding GCD
- NumPy - ufunc Trigonometric
- NumPy - Hyperbolic ufunc
- NumPy - Set Operations ufunc
- NumPy Useful Resources
- NumPy - Quick Guide
- NumPy - Cheatsheet
- NumPy - Useful Resources
- NumPy - Discussion
- NumPy Compiler
NumPy - Exponential Functions
NumPy Exponential Functions
In NumPy, exponential functions are provided to calculate powers of Euler's number (e), and to perform operations involving exponential growth or decay. NumPy provides the numpy.exp() function to calculate exponentials.
In this tutorial, we will explore how to use NumPy's exponential functions to calculate powers of e, and perform other related operations.
The numpy.exp() Function
The numpy.exp() function calculates the exponential of all elements in the input array. The function calculates the value of ex, where e is Euler's number (approximately 2.71828), and x is the exponent.
It is commonly used in applications that involve continuous growth or decay, such as calculating compound interest or solving differential equations.
Example: Exponential Function
In the following example, we calculate the exponential of an array of values using the exp() function −
import numpy as np # Define an array of exponents exponents = np.array([0, 1, 2, 3]) # Calculate the exponential of each element exp_values = np.exp(exponents) print("Exponential values:", exp_values)
The output will be −
Exponential values: [ 1. 2.71828183 7.3890561 20.08553692]
Note that the exponential of 0 is always 1, and as the exponent increases, the value grows exponentially.
Natural Logarithm Using log() Function
The numpy.log() function calculates the natural logarithm (base e) of all elements in the input array. It is the inverse of the numpy.exp() function. In mathematics, the natural logarithm of a number is the exponent to which e must be raised to obtain that number.
The numpy.log() function is useful for solving equations that involve exponential growth or decay.
Example: Natural Logarithm
In the following example, we calculate the natural logarithm of an array of values using NumPy's log() function −
import numpy as np # Define an array of values values = np.array([1, np.e, np.e**2, np.e**3]) # Calculate the natural logarithm of each element log_values = np.log(values) print("Natural Logarithm values:", log_values)
As expected, the natural logarithm of ex is simply x. We get the following output −
Natural Logarithm values: [0. 1. 2. 3.]
Base-10 Logarithm Using log10() Function
In addition to the natural logarithm, NumPy provides the numpy.log10() function to compute the base-10 logarithm of each element in the input array.
This function is commonly used in scientific fields that use logarithms with a base of 10, such as in sound intensity or earthquake magnitude calculations.
Example: Base-10 Logarithm
In the following example, we calculate the base-10 logarithm of an array of values using NumPy's log10() function −
import numpy as np # Define an array of values values = np.array([1, 10, 100, 1000]) # Calculate the base-10 logarithm of each element log10_values = np.log10(values) print("Base-10 Logarithm values:", log10_values)
As expected, the base-10 logarithm of powers of 10 follows the rule log10(10x) = x. We get the following output −
Base-10 Logarithm values: [0. 1. 2. 3.]
Exponential Function with Base-2
In some cases, it is useful to calculate logarithms with base-2, especially in areas like computer science and information theory. NumPy provides the numpy.log2() function for this purpose.
Example: Base-2 Logarithm
In the following example, we calculate the base-2 logarithm of an array of values using NumPy's log2() function −
import numpy as np # Define an array of values values = np.array([1, 2, 4, 8]) # Calculate the base-2 logarithm of each element log2_values = np.log2(values) print("Base-2 Logarithm values:", log2_values)
As expected, the base-2 logarithm of powers of 2 follows the rule log2(2x) = x. The output is −
Base-2 Logarithm values: [0. 1. 2. 3.]
Exponential Growth and Decay
Exponential functions can model both growth and decay. In growth, the quantity increases over time, such as the growth of a population or investment. In decay, the quantity decreases over time, such as the decay of a radioactive substance.
Exponential growth is typically modeled by the function y = y0 * ekt, where y0 is the initial value, k is the growth rate, and t is time. In decay, the function is y = y0 * e-kt.
Example: Exponential Growth
In the following example, we simulate exponential growth by using NumPy's exp() function. We assume an initial population size of 10, a growth rate of 0.1, and a time period of 10 units −
import numpy as np import matplotlib.pyplot as plt # Define initial parameters # initial population size y0 = 10 # growth rate k = 0.1 # time array t = np.linspace(0, 10, 100) # Calculate population size at each time point population = y0 * np.exp(k * t) # Plot the result plt.plot(t, population) plt.title("Exponential Growth") plt.xlabel("Time") plt.ylabel("Population Size") plt.grid(True) plt.show()
This code generates a plot showing the exponential growth of the population over time −
