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.
numpy.log(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) = <ufunc 'log'>
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.
This function returns a ndarray that contains the natural logarithmic value of x, which belongs to all elements of the input array.
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:
In the above mentioned code
In the output, a ndarray has been shown, contains the log, log2, and log10 values of all the elements of the source array.
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
In the output, a graph with four straight lines with different colors has been shown.
import numpy as np x=np.log([2, np.e, np.e**3, 0]) x
Output:
In the above code
In the output, a ndarray has been shown, contains the log values of the elements of the source array.