Using numpy.log() function (5 examples)

Updated: February 26, 2024 By: Guest Contributor Post a comment

Introduction

NumPy is a fundamental package for scientific computing with Python. It provides a high-performance multidimensional array object, and tools for working with these arrays. The numpy.log() function is utilized to compute the natural logarithm of an array of numbers.

In this tutorial, we’ll delve deep into the numpy.log() function, an essential tool in the numpy library for numerical computing in Python. We will progress from basic to advanced usage with five illustrative examples. Whether you’re a beginner or more advanced in Python programming, understanding how to effectively use the numpy.log() function can be incredibly useful for a wide range of mathematical and scientific computations.

Example 1: Basic Usage of numpy.log()

Let’s start by looking at a basic example of the numpy.log() function. We’ll calculate the natural logarithm of a single number, an array, and a two-dimensional array.

import numpy as np
from  math import e

# Single number
single_number = np.log(10)
print("Natural log of 10 is:", single_number)

# Array
array = np.log(np.array([1, e, e**2, 100]))
print("Natural log of array:", array)

# Two-dimensional array
two_d_array = np.log(np.array([[1, e], [e**3, 100]]))
print("Natural log of 2D array:", two_d_array)

The output will display the natural logarithm for the single number, the array, and the two-dimensional array respectively. This illustrates the fundamental use of numpy.log() across different types of inputs.

Output:

Natural log of 10 is: 2.302585092994046
Natural log of array: [0.         1.         2.         4.60517019]
Natural log of 2D array: [[0.         1.        ]
 [3.         4.60517019]]

Example 2: Logarithm base change

By default, numpy.log() calculates natural logarithms. However, it’s possible to calculate logarithms with different bases by using a simple mathematical transformation. Here we’ll show how to calculate the logarithm base 10 of a number, utilizing the change of base formula.

import numpy as np

counting = np.log(np.array([10, 100, 1000])) / np.log(10)
print("Log base 10 of numbers:", counting)

This example utilizes the formula log_b(a) = log_c(a) / log_c(b) to calculate the base 10 logarithm of an array. The result will be the logarithm, base 10, of the numbers 10, 100, and 1000.]

Output:

Log base 10 of numbers: [1. 2. 3.]

Example 3: Handling Complex Numbers

NumPy’s numpy.log() function can also handle complex numbers, which can be quite useful in various scientific and engineering computations. Here’s how you can compute the natural logarithm of complex numbers.

import numpy as np
from math import e

complex_number = np.log(np.array([1+1j, e**(1+1j)]))
print("Natural log of complex numbers:", complex_number)

In this example, we’re illustrating the calculation of the natural logarithm for an array containing complex numbers. The result showcases the function’s flexibility in dealing with complex inputs:

Natural log of complex numbers: [0.34657359+0.78539816j 1.        +1.j        

Example 4: Logarithm of Multidimensional Arrays

Moving on to a more advanced example, we’ll explore how to apply the numpy.log() function on multidimensional arrays, demonstrating its capability to handle data in higher dimensions efficiently.

import numpy as np
from math import e

multi_dimension = np.log(np.array([[[1, e], [e**2, 100]], [[10, 100], [1000, 10000]]]))
print("Natural log of multidimensional array:", multi_dimension)

This example reinforces the numpy.log() function’s ability to effortlessly manage and compute the logarithm of multidimensional arrays, showcasing its vital role in more complex mathematical computations.

Output:

Natural log of multidimensional array: [[[0.         1.        ]
  [2.         4.60517019]]

 [[2.30258509 4.60517019]
  [6.90775528 9.21034037]]]

Example 5: Application in Data Analysis

Our final example demonstrates the application of the numpy.log() function in data analysis, specifically in transforming data to better meet the assumptions of linear models.

import numpy as np
import matplotlib.pyplot as plt

# Generating sample data
x = np.linspace(1, 100, 400)
y = np.exp(x)  # Exponential growth

# Applying logarithmic transformation
log_y = np.log(y)

# Plotting both original and transformed data
plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1)
plt.plot(x, y)
plt.title('Original Data')
plt.subplot(1, 2, 2)
plt.plot(x, log_y)
plt.title('Log-Transformed Data')
plt.show()

In this example, the natural logarithm is employed to transform a dataset showing exponential growth, which is a common scenario in data analysis. This transformation makes the data more linear and easier to work with in linear models.

Conclusion

The numpy.log() function is a versatile tool for computing natural logarithms across various data types and structures. Through these five examples, we’ve seen its application from basic calculations to more complex data structures and analysis tasks. Understanding how to leverage numpy.log() effectively can enhance your data processing and scientific computing projects.