.log()

jhallen's avatar
Published Jul 15, 2024
Contribute to Docs

In NumPy, the .log() method is used to calculate the natural logarithm (base-e) of each element in an array. It is widely used in scientific computations, data analysis, and mathematical applications where logarithmic scaling is essential.

Syntax

numpy.log(array, out=None, where=True)
  • array: An array-like structure containing the elements for which the natural logarithm will be applied.
  • out (Optional): An array where the resulting logarithms are stored. If not provided, a new array is created to hold the results.
  • where (Optional): An array of boolean values that determines which elements undergo logarithm computation:
    • For elements where the condition is True, the natural logarithm is calculated.
    • For elements where the condition is False, the original element remains unchanged.
    • If where is not specified, the natural logarithm is computed for all elements in the input.

Example

The below example demonstrates the use of the .log() method:

import numpy as np
arr = np.array([1, np.e, 10, 100])
result = np.log(arr)
print(result)

The code above will generate the following output:

[0. 1. 2.30258509 4.60517019]

Codebyte Example

In this codebyte example, the .log() method only computes the natural logarithm of positive elements in the array:

Code
Output
Loading...

All contributors

Contribute to Docs

Learn Python:NumPy on Codecademy