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

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:

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:

Output:

numpy.log()

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:

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.


Next Topicnumpy-where




Latest Courses