AppDividend
Latest Code Tutorials

The Numpy log() function helps the user calculate the Natural logarithm of x where x belongs to all the input array elements.

np.log

The np.log() is a mathematical function used to calculate the Natural logarithm of x where x belongs to all the input array elements. The natural logarithm log is an inverse of the exponential method, so that log(exp(x)) = x. The natural logarithm is logarithm in base e.

Syntax

```numpy.log(x, /, out=None, *, where=True, casting='same_kind', order='K',
dtype=None, subok=True[, signature, extobj]) = <ufunc 'log'>```

Parameters

The np.log() method is straightforward in that it only has extensive parameters.

The three parameters of np.log() are x, out, and where, but there are more than three parameters.

Two of those parameters, the out and a where parameter, are less commonly used. Let’s see one by one.

x: array_like

The x parameter defines an input value for a numpy log() function.

out: ndarray, None, or tuple of ndarray and None(optional)

The out argument defines the location in which the result is stored. If we define the out argument, it must have a shape similar to an input broadcast; otherwise, a freshly-allocated array is returned. The tuple has a length equal to the number of outputs.

where: array_like(optional)

It is a condition that is broadcast over an input. At this location, where a condition is True, the out array will be set to the ufunc(universal function) result; otherwise, it will retain its original value.

casting: {‘no’,’ equiv’, ‘safe’, ‘same_kind’, ‘unsafe’}(optional)

The casting parameter controls the type of datacasting that may occur.

The ‘no’ means the data types should not be cast.

The ‘equiv’ means only the byte-order changes are allowed.

The ‘safe’ argument means the only cast allowing the preserved value.

The parametersame_kind’ means only safe casts or casts within the kind.

The ‘unsafe’ parameter means any data conversions may be done.

order: {‘K’, ‘C’, ‘F’, ‘A’}(optional)

The order argument defines an output array’s calculation iteration order/memory layout. By default, the order will be K.

An order ‘C’ means the output should be C-contiguous.

An order ‘F’ means F-contiguous,

The order ‘A’ means F-contiguous if the inputs are F-contiguous, and if inputs are in C-contiguous, then ‘A’ means C-contiguous. ‘K’ means to match the element ordering of the inputs(as closely as possible).

dtype: data-type(optional)

It overrides the dtype of the computation and output arrays.

subok: bool(optional)

By default, the subok parameter is set to True. If we set it to False value, the output will always be the strict array, not a subtype.

signature

The signature argument provides a specific signature to the 1D loop ‘for’, used in the underlying calculation.

extobj

The extobj argument is a list of lengths 1, 2, or 3 specifying the ufunc buffer-size, error mode integer, and error callback method.

Return Value

Numpy log() function returns the ndarray that contains the natural logarithmic value of x, which belongs to all input array elements.

Example

The syntax for using the log() function is pretty straightforward, but it’s always easier to understand code when you have a few examples of working with it.

That being said, let’s take a look at some examples.

```import numpy as np

data = np.array([1, 3, 5, 9])
print('The data array is: ', data)

logval = np.log(data)
print('The "logval" array is: ', logval)
```

Output

```The data array is:  [1 3 5 9]
The "logval" array is:  [0.         1.09861229 1.60943791 2.19722458]```

In the above code, we first imported the numpy with the alias name np and then created an array of data using the np.array() function. Then we have used the np log() method to get the natural logarithmic.

Graphical representation of np log

We can use the matplotlib library to create a graphical representation of log values.

```import numpy as np
import matplotlib.pyplot as plt

arr = [3, 3.4, 3.8, 4.2, 4.6, 5]
res = np.log(arr)
res2 = np.log2(arr)
res3 = np.log10(arr)
plt.plot(arr, arr, color='blue', marker="*")
plt.plot(res, arr, color='green', marker="o")
plt.plot(res2, arr, color='red', marker="*")
plt.plot(res3, arr, color='black', marker="*")
plt.show()
```

Using np.log() with a single number

Here, we’ll compute the natural logarithm of a mathematical constant e, also known as Euler’s number.

To do this, we will use a constant np.e from the Numpy array.

If you print then, you can see the value:

`print(np.e)`

Output

`2.718281828459045`

And now, let’s compute  with np.log().

```import numpy as np

print(np.log(np.e))
```

Output

``` python3 app.py
1.0```

This is relatively simple.

Here, we’re computing the natural log of the constant  because the function  is the inverse of the exponential.

If you’d like, try to run this code with some other numbers besides .

Applying log on a two-dimension Numpy Array

First, let’s create the Numpy array. We’ll use the np.arange() to create the Numpy array with the values from 1 to 6, and we’ll reshape that array into two dimensions using the Numpy reshape() method.

```import numpy as np

D2_Array = np.arange(start=1, stop=7).reshape((2, 3))
print(D2_Array)
```

Output

```[[1 2 3]
[4 5 6]]```

Now, compute the natural logarithm using the Numpy log().

```import numpy as np

D2_Array = np.arange(start=1, stop=7).reshape((2, 3))
print(np.log(D2_Array))
```

Output

```[[0.         0.69314718 1.09861229]
[1.38629436 1.60943791 1.79175947]]```

Again, np.log() computes the natural log of every element in the input array.

In this case, an input was a 2 X 3 array (a two-dimensional array with two rows and three columns), so the output has the same shape.

Wrap up

To wrap up, the Numpy log function is relatively simple: we use it to compute the natural logarithm of the values in the Numpy array. You’ll probably remember logarithms from mathematics classes.

The natural logarithm is the inverse of the exponential function, such that:

Computing the log is reasonably common in scientific tasks, and the Numpy log() method gives us an easy way to calculate the natural logarithm in Python.

That’s it for this tutorial.