In this tutorial, we will learn about the **numpy.log() in Python**. The Numpy module offers powerful data manipulation methods. It mostly deals with data stored in arrays.

The Numpy.log() method lets you calculate the mathematical log of any number or array.

Let’s learn how to use numpy.log() to calculate log in python.

Table of Contents

## Using numpy.log() in Python

To use numpy.log() we will first have to import the Numpy module.

```
import numpy
```

Now we can use numpy.log() to find out the log of different numbers.

```
import numpy as np
print(np.log(10))
```

**Output:**

```
2.302585092994046
```

Let’s try another example.

```
import numpy as np
print(np.log(np.e))
```

Output :

```
1.0
```

We get 1 as the output as numpy.log by default calculates the natural log. The natural log is calculated with a base of e. The value of e is :

```
2.718281828459
```

Let’s try calculating the log of 0.

## Using numpy.log() on 0

Let’s see what happens when we use the numpy.log function on 0.

```
import numpy as np
print(np.log(0))
```

Output:

```
-inf
/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:1: RuntimeWarning: divide by zero encountered in log
```

The logarithm of zero is not defined. It’s not a real number, because you can never get **zero** by raising anything to the power of anything else.

There are some other logs that you can calculate using np.log. These are **log2** and** log10 **which are logarithms with base **2 **and** 10 **respectively.

### 1. Calculating log with base 2

To calculate logarithm with base 2, use **log2** in place of log.

```
import numpy as np
print(np.log2(8))
```

Output:

```
3.0
```

Let’s try another example.

```
import numpy as np
print(np.log2(32))
```

Output:

```
5.0
```

### 2. Calculating log with base 10

To calculate logarithm with base 10, use **log10** in place of log.

```
import numpy as np
print(np.log10(100))
```

Output :

```
2.0
```

Let’s try another example.

```
import numpy as np
print(np.log10(10000))
```

Output :

```
4.0
```

## Using Numpy.log() on Arrays

Let’s see how to use numpy.log on arrays.

### 1. Calculating Logarithm of a 1D array

To calculate the logarithm of a 1D array use :

```
import numpy as np
arr = np.array([1,2,4,5,6,8])
print(np.log2(arr))
```

Output:

```
[0. 1. 2. 2.32192809 2.5849625 3.]
```

### 2. Calculating Logarithm of a 2D array

To calculate the logarithm of a 2D array use :

```
import numpy as np
arr_2d = np.arange(4,10).reshape((2,3))
print(arr_2d)
print(np.log2(arr_2d))
```

Output :

```
[[4 5 6]
[7 8 9]]
[[2. 2.32192809 2.5849625 ]
[2.80735492 3. 3.169925 ]]
```

## Plotting numpy.log() function using Matplotlib

Let’s try plotting a graph for the logarithmic function. To plot a graph we will need a lot of points in our array. Our approach is as follows :

We will create a Numpy array of integers from 1 to 1000. Then we will store the log of this array. Finally, we will create a plot using the stored values.

Let’s see the code for the same.

```
import numpy as np
import matplotlib.pyplot as plt
arr = np.arange(start = 1, stop = 1000)
log_val=np.log(arr)
plt.plot(log_val,arr,color='purple')
```

Output :

## Conclusion

This tutorial was about the Numpy.log function in Python. We learn how to use **numpy.log** for calculating logs of integers and arrays. We also learned how to plot a graph using numpy.log and matplotlib.