# numpy.log() in Python

The numpy.log() is a mathematical function that is used to calculate the natural logarithm of x(x belongs to all the input array elements). It is the inverse of the exponential function as well as an element-wise natural logarithm. The natural logarithm log is the reverse of the exponential function, so that log(exp(x))=x. The logarithm in base e is the natural logarithm.

### Syntax

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

### Parameters

x: array_like

This parameter defines the input value for the numpy.log() function.

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

This parameter is used to define the location in which the result is stored. If we define this parameter, it must have a shape similar to the input broadcast; otherwise, a freshly-allocated array is returned. A tuple has a length equal to the number of outputs.

where: array_like(optional)

It is a condition that is broadcast over the input. At this location, where the 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)

This parameter controls the kind of data casting that may occur. The 'no' means the data types should not be cast at all. The 'equiv' means only byte-order changes are allowed. The 'safe' means the only cast, which can allow the preserved value. The 'same_kind' means only safe casts or casts within a kind. The 'unsafe' means any data conversions may be done.

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

This parameter specifies the calculation iteration order/ memory layout of the output array. By default, the order will be K. The order 'C' means the output should be C-contiguous. The order 'F' means F-contiguous, and '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 calculation and output arrays.

subok: bool(optional)

By default, this parameter is set to true. If we set it to false, the output will always be a strict array, not a subtype.

signature

This argument allows us to provide a specific signature to the 1-d loop 'for', used in the underlying calculation.

extobj

This parameter is a list of length 1, 2, or 3 specifying the ufunc buffer-size, the error mode integer, and the error callback function.

### Returns

This function returns a ndarray that contains the natural logarithmic value of x, which belongs to all elements of the input array.

### Example 1:

snippet
```import numpy as np
a=np.array([2, 4, 6, 3**8])
a
b=np.log(a)
b
c=np.log2(a)
c
d=np.log10(a)
d```

Output:

Output
array([ 2, 4, 6, 6561]) array([0.69314718, 1.38629436, 1.79175947, 8.78889831]) array([ 1. , 2. , 2.5849625 , 12.67970001]) array([0.30103 , 0.60205999, 0.77815125, 3.81697004])

In the above mentioned code

• We have imported numpy with alias name np.
• We have created an array 'a' using np.array() function.
• We have declared variable b, c, and, d and assigned the returned value of np.log(), np.log2(), and np.log10() functions respectively.
• We have passed the array 'a' in all the functions.
• Lastly, we tried to print the value of b, c, and d.

In the output, a ndarray has been shown, contains the log, log2, and log10 values of all the elements of the source array.

### Example 2:

snippet
```import numpy as np
import matplotlib.pyplot as plt
arr = [2, 2.2, 2.4, 2.6,2.8, 3]
result1=np.log(arr)
result2=np.log2(arr)
result3=np.log10(arr)
plt.plot(arr,arr, color='blue', marker="*")
plt.plot(result1,arr, color='green', marker="o")
plt.plot(result2,arr, color='red', marker="*")
plt.plot(result3,arr, color='black', marker="*")
plt.show()```

Output: In the above code

• We have imported numpy with alias name np.
• We have also imported matplotlib.pyplot with alias name plt.
• Next, we have created an array 'arr' using np.array() function.
• After that we declared variable result1, result2, result3 and assigned the returned values of np.log(), np.log2(), and np.log10() functions respectively.
• We have passed the array 'arr' in all the functions.
• Lastly, we tried to plot the values of 'arr', result1, result2, and result3.

In the output, a graph with four straight lines with different colors has been shown.

### Example 3:

snippet
```import numpy as np
x=np.log([2, np.e, np.e**3, 0])
x```

Output:

Output
__main__:1: RuntimeWarning: divide by zero encountered in log array([0.69314718, 1. , 3. , -inf])

In the above code

• Firstly, we have imported numpy with alias name np.
• We have declared the variable 'x' and assigned the returned value of np.log() functions.
• We have passed different values in the function, such as integer value, np.e, and np.e**2.
• Lastly, we tried to print the value of 'x'.

In the output, a ndarray has been shown, contains the log values of the elements of the source array.

Related Tutorial
Contents