Open In App

NumPy ufuncs – Logs

NumPy library is used for numerical computation in Python. NumPy makes it easy for us to perform calculations on arrays and complex matrices. It provides a wide variety of mathematical functions and operations. NumPy provides universal functions (or unfuncs) that operate element-wise on arrays. In this article, we will explore NumPy unfunc log operations at different bases.

NumPy ufuncs

NumPy ufuncs are functions that can be applied to elements of NumPy arrays. These functions are implemented in C, which makes them significantly faster than equivalent Python code.



Log at base 2

To calculate the logarithm at base 2, we can use the numpy.log2() function.

Example: Calculate log base 2 of an array






# Import numpy
import numpy as np
 
# Create an numpy array
arr = np.array([2, 4, 8, 16, 32])
 
# Calculate log2
log_base_2 = np.log2(arr)
 
print(f"Original array: {arr}")
print(f"log_base_2: {log_base_2}")
 
# This code is contributed by arunkumar2403gg

Output:

Original array: [ 2  4  8 16 32]
log_base_2: [1. 2. 3. 4. 5.]

Log at base 10

To calculate the logarithm at base 10, we can use the numpy.log10() function.

Example : Calculate log base 10 of an array




# Import numpy
import numpy as np
 
# Create an numpy array
arr = np.array([1, 10, 100, 1000, 10000])
 
# Calculate log10
log_base_10 = np.log10(arr)
 
print(f"Original array: {arr}")
print(f"log_base_10: {log_base_10}")
 
# This code is contributed by arunkumar2403gg

Output:

Original array: [    1    10   100  1000 10000]
log_base_10: [0. 1. 2. 3. 4.]

Natural log or log at base e

The natural logarithm is often denoted as ln or log_e.NumPy provides the numpy.log() function to calculate the natural logarithm.

Example : Calculate the natural logarithm of an array




# Import numpy
import numpy as np
 
# Create an numpy array
arr = np.array([1, 2.718, 7.389])
 
# Calculate natural log
natural_log = np.log(arr)
 
print(f"Original array: {arr}")
print(f"natural_log: {natural_log}")
 
# This code is contributed by arunkumar2403gg

Output:

Original array: [1. 2.718 7.389]
natural_log: [0. 0.99989632 1.99999241]

Log at any bases

In some scenarios, we may need to calculate logarithms at bases other than 2, 10, or e.NumPy does not provide any functions to calculate log at any base. However, the numpy.log() function can be used with a base argument to compute logarithms at any desired base.

Example : Calculate log base 5 of an array




# Import numpy
import numpy as np
 
# Create an numpy array
arr = np.array([5, 25, 125, 625])
 
# Calculate log5
log_base_5 = np.log(arr) / np.log(5)
 
print(f"Original array: {arr}")
print(f"log_base_5: {log_base_5}")
 
# This code is contributed by arunkumar2403gg

Output:

Original array: [  5  25 125 625]
log_base_5: [1. 2. 3. 4.]

Note: In this example, we first calculated the natural log and then divided it by the natural logarithm of the desired base (in this case, 5) to calculate the logarithm at base 5.

Log at any bases using np.frompyfunc() and math.log()

The numpy.frompyfunc function is used to create a universal function (ufunc) from an existing Python function.Here, we use it to create a log function that accepts any base.

Syntax:

numpy.frompyfunc(func, nin, nout)

Parameters:

Example: Calculate log at base 7




from math import log
import numpy as np
 
# Create an universal log function using np.frompyfunc
# log_at_any_base takes two inputs and returns one output
log_at_any_base = np.frompyfunc(log, 2, 1)
 
# Calculate log base 7
log_at_base_7 = log_at_any_base(343, 7)
 
print(f"log7(343) = {log_at_base_7}")
 
# Create an array
arr = np.array([7, 49, 343])
 
# Passing a numpy array
log_at_base_7 = log_at_any_base(arr, 7)
 
print(f"Original array: {arr}")
print(f"Log_base_7: {log_at_base_7}")
 
# This code is contributed by arunkumar2403gg

Output:

log7(343) = 3.0
Original array: [ 7 49 343]
Log_base_7: [ 1. 2. 3.]

Conclusion:

NumPy provide inbuilt functions to calculate log at base 2, 10 and e. To find log at any bases we must different approaches discussed in this article.


Article Tags :