لَآ إِلَـٰهَ إِلَّا هُوَ
LA ILAHA ILLA HU
Allah, Your Lord There Is No Deity Except Him.

Python Data Science NumPy ufunc Logs NumPy's Log Functions

Logs: NumPy provides functions to perform log at the base 2, e and 10.

Python's numpy. log() is a mathematical function that computes the natural logarithm of an input array's elements.

The natural logarithm is the inverse of the exponential function, such that log (exp(x)) = x

we can also take log for any base by creating a custom ufunc.

All log functions will place -inf or inf in the elements if the log can not be computed.

Log at Base 2: Use the log2() function to perform log at the base 2..

Example 1: Find log at base 2 of all elements of following array.

Code

import numpy as np

arr = np.arange(1, 20)

print(np.log2(arr))

the output will be

[0. 1. 1.5849625 2. 2.32192809 2.5849625

2.80735492 3. 3.169925 3.32192809 3.45943162 3.5849625

3.70043972 3.80735492 3.9068906 4. 4.08746284 4.169925

4.24792751]

Note: The arange(1, 20) function returns an array with integers starting from 1 (included) to 20 (not included).

Log at Base 10

Use the log10() function to perform log at the base 10.

Example 2: Find log at base 10 of all elements of following array.

Code

import numpy as np
arr = np.arange(1, 20)
print(np.log10(arr))

the output will be

[0. 0.30103 0.47712125 0.60205999 0.69897 0.77815125

0.84509804 0.90308999 0.95424251 1. 1.04139269 1.07918125

1.11394335 1.14612804 1.17609126 1.20411998 1.23044892 1.25527251

1.2787536 ]

Natural Log, or Log at Base e

Use the log() function to perform log at the base e.

Example 3: Find log at base e of all elements of following array.

Code
import numpy as np

arr = np.arange(1, 20)

print(np.log(arr))

the output will be

[0. 0.69314718 1.09861229 1.38629436 1.60943791 1.79175947

1.94591015 2.07944154 2.19722458 2.30258509 2.39789527 2.48490665

2.56494936 2.63905733 2.7080502 2.77258872 2.83321334 2.89037176

2.94443898]

Log at Any Base
NumPy does not provide any function to take log at any base,

So we can use the frompyfunc() function along with inbuilt function math.log() with two input parameters and one output parameter.

Example 4
from math import log
import numpy as np
nplog = np.frompyfunc(log, 2, 1)
print(nplog(100, 10))
the output will be
2.0
Note: NumPy ufunc Universal function or ufuncs is a function which operates on ndarrays in an element by element fashion and supports array broadcasting, type casting, and many other standard features.