
- 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 - Logarithmic Functions
NumPy Logarithmic Functions
logarithmic functions are the inverse of exponential functions. They are used to determine the power to which a number (called the base) must be raised to produce a given value. The most common logarithmic functions are the natural logarithm (ln), with base 10.
In NumPy, several logarithmic functions are available for various types of logarithms. These functions help calculate natural, base-10, and base-2 logarithms, which are useful in solving mathematical equations involving logarithmic relationships.
Natural Logarithm Using log() Function
The numpy.log() function calculates the natural logarithm (base e) of all elements in the input array. The natural logarithm is commonly used in calculus and other mathematical computations involving continuous growth or decay.
In mathematics, the natural logarithm of a number is the exponent to which the base e (Euler's number, approximately 2.71828) must be raised to obtain that number.
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. The output will be −
Natural Logarithm values: [0. 1. 2. 3.]
Base-10 Logarithm Using log10() Function
The numpy.log10() function calculates the base-10 logarithm of all elements in the input array.
The base-10 logarithm, also known as the common logarithm, is the inverse of the exponential function with base 10. It is widely used in fields that deal with large or small numbers, like sound intensity or the Richter scale for earthquake magnitudes.
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. The output is −
Base-10 Logarithm values: [0. 1. 2. 3.]
Base-2 Logarithm Using log2() Function
The numpy.log2() function calculates the base-2 logarithm of each element in the input array. Base-2 logarithms are used in various fields, including computer science, information theory, and coding theory, as binary systems and algorithms often rely on base-2 operations.
In computer science, the base-2 logarithm of a number is the number of times you can divide the number by 2 until you get 1. This function is often used in applications that involve binary data or computational complexity.
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.]
Logarithm with a Custom Base
Although NumPy doesn't provide a direct function for logarithms with arbitrary bases, you can compute the logarithm with a custom base by using the change of base formula −
logbase(x) = loge(x) / loge(base)
This allows you to compute logarithms with any base by dividing the natural logarithm of the number by the natural logarithm of the base.
Example: Custom Base Logarithm
In the following example, we compute the logarithm of an array of values with a custom base (e.g., base 3) using the change of base formula −
import numpy as np # Define an array of values and the custom base values = np.array([1, 3, 9, 27]) base = 3 # Calculate the logarithm with the custom base using the change of base formula log_base3_values = np.log(values) / np.log(base) print("Logarithm with base 3 values:", log_base3_values)
As expected, the logarithm of powers of 3 with base 3 follows the rule log3(3x) = x −
Logarithm with base 3 values: [0. 1. 2. 3.]
Handling Logarithms of Zero or Negative Numbers
Logarithms of zero or negative numbers are undefined in real numbers. If you try to compute the logarithm of 0 or a negative number using NumPy, it will return nan (Not a Number) or -inf (negative infinity) depending on the context.
To avoid errors, it is often helpful to use the numpy.errstate() function to handle such cases gracefully, suppressing warnings or handling invalid operations explicitly.
Example: Handling Invalid Logarithms
In the following example, we attempt to calculate the logarithms of 0 and negative values −
import numpy as np # Define an array with a zero and a negative value values = np.array([0, -1, 1, 10]) # Calculate the natural logarithm of each element log_values = np.log(values) print("Logarithm values:", log_values)
The output shows that the logarithm of 0 results in nan, and the logarithm of a negative number results in -inf −
Logarithm values: [ nan -inf 0. 2.30258509]