Introduction to NumPy Log Functions in Python

In This Article, You Will Learn About Numpy Log Functions.

Numpy Log Functions – Before moving ahead, let’s know a bit of Python Numpy Simple Arithmetic Operators

Python Numpy Works with the Log method therefore it provides log functions to work with.

Log functions are following –

1. base 2

2. e

3. 10

Log at Base 2

To perform log at the base 2 using the log2() function.

Example – Finding all elements of the array at base 2 of log.

import numpy as np

array = np.arange(2, 8)

print(np.log2(array))
Output - 

[1. 1.5849625 2. 2.32192809 2.5849625 2.80735492]

As shown above, it returned an array with all elements of the array at base 2 of log.

Log at Base 10

To perform log at the base 10 using the log10() function.

Example – Finding the all elements of array at base 10 of log.

import numpy as np

array = np.arange(2, 8)

print(np.log10(array))
Output - 

[0.30103 0.47712125 0.60205999 0.69897 0.77815125 0.84509804]

As shown above, it returned an array with all elements of the array at base 10 of log.

Natural Log, or Log at Base e

To perform log at the base e using the log() function.

Example – Finding all elements of the array at base e of log.

import numpy as np

array = np.arange(2, 8)

print(np.log(array))
Output - 

[0.69314718 1.09861229 1.38629436 1.60943791 1.79175947 1.94591015]

As shown above, it returned an array with all elements of the array at the base e of log.

Log at Any Base

NumPy doesn’t have any function that can take log at any base. We can instead use the frompyfunc() function with the built-in function math.log(), which has two input parameters and one out parameter.

Example – Using frompyfunc() and math.log() in exchange for a function that could take log at any function.

from math import log
import numpy as np

nplog = np.frompyfunc(log, 2, 1)

print(nplog(75, 30))
numpy-log-functions

As shown above, it returned a number based on given log’s value.

If you find anything incorrect in the above-discussed topic and have any further questions, please comment below.

Like us on

1 thought on “Introduction to NumPy Log Functions in Python”

Leave a Comment

Stay in the loop

codingstreets