Open In App

NumPy ufuncs – Logs

Last Updated : 17 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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

Python




# 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

Python




# 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

Python




# 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

Python




# 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:

  • func: The Python function we want to convert into a ufunc.
  • nin: The number of input arguments the function takes.
  • nout: The number of output values the function returns.

Example: Calculate log at base 7

Python




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.



Similar Reads

NumPy ufuncs | Universal functions
NumPy Universal functions (ufuncs in short) are simple mathematical functions that operate on ndarray (N-dimensional array) in an element-wise fashion. It supports array broadcasting, type casting, and several other standard features. NumPy provides various universal functions like standard trigonometric functions, functions for arithmetic operatio
6 min read
Python | Numpy numpy.resize()
With the help of Numpy numpy.resize(), we can resize the size of an array. Array can be of any shape but to resize it we just need the size i.e (2, 2), (2, 3) and many more. During resizing numpy append zeros if values at a particular place is missing. Parameters: new_shape : [tuple of ints, or n ints] Shape of resized array refcheck : [bool, optio
2 min read
Python | Numpy numpy.transpose()
With the help of Numpy numpy.transpose(), We can perform the simple function of transpose within one line by using numpy.transpose() method of Numpy. It can transpose the 2-D arrays on the other hand it has no effect on 1-D arrays. This method transpose the 2-D numpy array. Parameters: axes : [None, tuple of ints, or n ints] If anyone wants to pass
2 min read
Python | Numpy numpy.ndarray.__lt__()
With the help of numpy.ndarray.__lt__() method of Numpy, We can find that which element in an array is less than the value which is provided in the parameter. It will return you numpy array with boolean type having only values True and False. Syntax: ndarray.__lt__($self, value, /) Return: self<value Example #1 : In this example we can see that
1 min read
Python | Numpy numpy.ndarray.__gt__()
With the help of numpy.ndarray.__gt__() method of Numpy, We can find that which element in an array is greater then the value which is provided in the parameter. It will return you numpy array with boolean type having only values True and False. Syntax: ndarray.__gt__($self, value, /) Return: self>value Example #1 : In this example we can see th
1 min read
Python | Numpy numpy.ndarray.__le__()
With the help of numpy.ndarray.__le__() method of Numpy, We can find that which element in an array is less than or equal to the value which is provided in the parameter. It will return you numpy array with boolean type having only values True and False. Syntax: ndarray.__le__($self, value, /) Return: self<=value Example #1 : In this example we
1 min read
Python | Numpy numpy.ndarray.__ge__()
With the help of numpy.ndarray.__ge__() method of Numpy, We can find that which element in an array is greater then or equal to the value which is provided in the parameter. It will return you numpy array with boolean type having only values True and False. Syntax: ndarray.__ge__($self, value, /) Return: self>=value Example #1 : In this example
1 min read
Python | Numpy numpy.ndarray.__ne__()
With the help of numpy.ndarray.__ne__() method of Numpy, We can find that which element in an array is not equal to the value which is provided in the parameter. It will return you numpy array with boolean type having only values True and False. Syntax: ndarray.__ne__($self, value, /) Return: self!=value Example #1 : In this example we can see that
1 min read
Python | Numpy numpy.ndarray.__neg__()
With the help of numpy.ndarray.__neg__() method of Numpy, one can multiply each and every element of an array with -1. Hence, the resultant array having values like positive values becomes negative and negative values become positive. Syntax: ndarray.__neg__($self, /) Return: -self Example #1 : In this example we can see that after applying numpy._
1 min read
Python | Numpy numpy.ndarray.__pos__()
With the help of numpy.ndarray.__pos__() method of Numpy, one can multiply each and every element of an array with 1. Hence, the resultant array having values same as original array. Syntax: ndarray.__pos__($self, /) Return: +self Example #1 : In this example we can see that after applying numpy.__pos__(), we get the simple array that can be the sa
1 min read
Article Tags :
Practice Tags :