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*!