With a series of articles in AskPython elaborating on the various functions available within the *numpy *library of Python, it is time to say hello to logarithms! Before getting to know about the function in Python let’s try to understand what a logarithm is & why bother to use one.

Say, we have a number ‘10’ & would like to raise it to the power of ‘3’. This shall yield us ‘1,000’ as the result. Logarithms are an alternate method of expressing this operation. The power to which a certain number is to be raised to obtain a desired number is called a ‘logarithm’

*Also read: Numpy log10 – Return the base 10 logarithm of the input array, element-wise..*

In the above case, the logarithm of 10 to obtain 1,000 is ‘3’! Now, we shall turn our focus upon the scope of this article – the *log1p( )* function. This function returns the natural logarithm of the input number after being added with one.

Let’s get things started by first importing the *numpy *library using the following code.

```
import numpy as np
```

Thereafter, we shall explore further the *log1p( ) *function through each of the following sections.

**Syntax of the***log1p( )*function**Why use***log1p(x)*instead of*log(x+1)*?**Using***log1p( )*on N-Dimensional Arrays

**Syntax of the*** log1p( )* function

*log1p( )*function

How the *log1p( )* function works is similar to that of the* log( )* function, but the only difference is that the input numbers will be added with ‘1’ before calculating the result. Following is the syntax of the *log1p( ) *function which contains both the mandatory and the optional inputs required for its functioning.

```
numpy. log1p(x, out=None, *, where=True, dtype=None)
```

where,

a N-dimensional array or scalar*x –*an optional construct set to*out –**none*by default, but could be used to store the results in the desired array which is of the same length as the outputkwargs or keyword argument which is an optional construct used to pass keyword variable length of argument to a function***–an optional construct which is used to calculate the universal function (ufunc) at the given position when set to*where –**True*(default setting) or not calculate when set to*False*an optional construct used to specify the data type which is being used*dtype –*

**Why use ***log1p(x) *instead of *log(x+1)*?

*log1p(x)*instead of

*log(x+1)*?

One may why bother to build a specific function for adding ‘1’ to the input number when the same can be done by using the *log( )* function which is already available. To find the reason behind this, let’s compare the results between these two functions to find out whether there are any observable differences.

```
x = 10
np.log(x+1)
np.log1p(x)
```

Following are the results of the above code.

The results are identical even to the last decimal place! But, what if we reduce *‘x’ *way lesser and put it through the same functions? Will those be able to return the same results? Let’s find out!

```
x = 1e-10
np.log(x+1)
np.log1p(x)
```

The results aren’t the same now, are they? *(Evil smirk!)*

The main reason behind this is that the *log1p(x)* function seems to work accurately even for smaller numbers which start several digits after the decimal point. But, the same can’t be said for the *log(x+1) *function since the results would be offset due to rounding off errors. Thusly, use *log1p( ) *for returning accurate results when numbers of smaller magnitudes are involved.

**Using ***log1p( ) *on N-Dimensional Arrays

*log1p( )*on N-Dimensional Arrays

In this section let’s try deploying the *log1p( ) *function only at select positions of the N-dimensional array.

```
ar1 = np.array([[1, 8, 0.09],
[5.007, 33, 2]])
np.log1p(ar1, where = [[False, True, True],
[True, True, False]])
```

Since the *where *option has been exercised, it could be seen that the results are returned only in the positions given as *True *& the results elsewhere are zero.

**Conclusion**

Now that we have reached the end of this article, hope it has elaborated on how to use the *log1p( ) *function from the *numpy *library. Here’s another article that details the usage of *expm1( ) *function from the *numpy *library in Python. There are numerous other enjoyable and equally informative articles in AskPython that might be of great help to those who are looking to level up in Python. *Mazel tov*!