.log()
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.
- For elements where the condition is
Example
The below example demonstrates the use of the .log()
method:
import numpy as nparr = 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:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python:NumPy on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Skill path
Data Science Foundations
Learn to clean, analyze, and visualize data with Python and SQL.Includes 15 CoursesWith CertificateBeginner Friendly55 hours - Career path
Data Scientist: Machine Learning Specialist
Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.Includes 27 CoursesWith Professional CertificationBeginner Friendly95 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours