This is a detailed tutorial of the NumPy Logs Universal Functions. Learn the usage of these functions with the help of examples.

Table of Contents

## NumPy Logs

These are the mathematical function which is helpful in calculating the natural logarithm of x where x is the input we give in the form of arrays. It is the inverse of the exponential function and also of the element-wise natural algorithm.

In NumPy, we can perform log at three bases which are at base 2, base e and base 10. These log function will place -inf or inf in the element if the log can’t be computed.

### Log at Base 2

In order to get this log, we need to use `log2()`

the function which will give us the log at base 2.

Let us take an example to understand it better:

#First we will import the numpy package and then make an alias as np import numpy as np # here we will take the values which we are going to find the log for a=np.arange(1,10) #now we will apply the lof function b=np.log2(a) #now we will print the array print(b)

**Output:**

[ 0. 1. 1.5849625 2. 2.32192809 2.5849625 2.80735492 3. 3.169925 ]

Here in this example, it is returning us the log for integers starting at 1 to 9 where nine is not included.

### Log to Base 10

In order to get the log for base 10, we will use `log10()`

function.

Let us take an example to understand it better:

#First we will import the numpy package and then make an alias as np import numpy as np # here we will take the values which we are going to find the log for a=np.arange(1,10) #now we will apply the lof function b=np.log10(a) #now we will print the array print(b)

**Output:**

[ 0. 0.30103 0.47712125 0.60205999 0.69897 0.77815125 0.84509804 0.90308999 0.95424251]

So in this example, we get all the terms with log to base 10 in the array.

### Log at Base e

This is also known as Natural log. And in order to find this natural log, we will use `log()`

function.

Let us take an example:

#First we will import the numpy package and then make an alias as np import numpy as np # here we will take the values which we are going to find the log for a=np.arange(1,10) #now we will apply the lof function b=np.log(a) #now we will print the array print(b)

**Output:**

[ 0. 0.69314718 1.09861229 1.38629436 1.60943791 1.79175947 1.94591015 2.07944154 2.19722458]

### Log at Any Base

In order to find the log at any base irrespective of the bases which already defined NumPy has no such function. So to achieve this goal, we will use `frompyfunc()`

function along with `math.log()`

which is an inbuilt function. It will take two parameters as input and will return one parameter as output.

let us take an example to understand it better:

#here first we will take the math library which will import log from math import log # we will also import the numpy package and then make an alias as np import numpy as np # here we will take the values which we are going to find the log for nplog=np.frompyfunc(log,2,1) #now we will print the array print(nplog(100,15))

**Output:**

1.7005483074552052

I hope you found this guide useful. If so, do share it with others who are willing to learn Numpy and Python. If you have any questions related to this article, feel free to ask us in the comments section.

And do not forget to subscribe to WTMatter!