Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content
No results
  • About AskPython
  • Blog
  • Contact Me
  • Free Online Developer Tools Hub
  • Home
  • Meet the Team
  • Privacy Policy
  • Python
  • Python Course for Beginners Online FREE
  • Python Tutorial – Mega Index
  • Terms and Conditions

🚀 Supercharge your YouTube channel's growth with AI.

Try YTGrowAI Free
  • Home
  • Blog
  • About AskPython
  • Python Course
  • Start Here

Understanding Python Numpy log

  • Safa MulaniSafa Mulani
  • May 25, 2020
  • NumPy
Understanding Python Numpy log

Hey, folks! In this article, we will be focusing on Python Numpy logarithm functions.


Getting started with Python NumPy log

Python NumPy module deals with creation and manipulation of array data elements.

The numpy.log() method is used calculate the natural logarithmic value of a data value of an element/array values.

Syntax:

numpy.log(element/array)

Example 1:

import numpy as np
data = 12.2
log_val = np.log(data)
print(log_val)

Output:

2.501435951739211

Example 2:

import numpy as np
data = np.e
log_val = np.log(data)
print(log_val)

Output:

1.0

NumPy logarithm with base 2

Apart from the default functionality of log() method, we can calculate the log value of a NumPy array or an element with the base 2 using the below command:

numpy.log2(element)

Example:

import numpy as np
data = 4
log_val = np.log2(data)
print(log_val)

Output:

2.0

NumPy logarithm with base 10

The numpy.log10() function is used to calculate the natural logarithmic value of an element to the base 10.

Syntax:

numpy.log10(data)

Example:

import numpy as np
data = 1000
log_val = np.log10(data)
print(log_val)

Output:

3.0

NumPy logarithm with a custom base

NumPy log() function offers a possibility of finding logarithmic value with respect to user-defined bases.

Syntax:

numpy.log(data)/numpy.log(base)

Example:

import numpy as np
data = 1000
base = 40
log_val = np.log(data)/np.log(base)
print(log_val)

In the above example, we have calculated the logarithmic value of 1000 with base 40.

As we all know,

Log Value With Custom Base
Log Value With Custom Base

Thus, the above mathematical concept is used to calculate the log value of a data value to custom base value.

Output:

1.8725890517453545

Performing NumPy log on a 2-D array

The numpy.log() method can be applied to a 2-D NumPy array to calculate the logarithmic values of all the array elements.

Syntax:

numpy.log(array)

Example:

import numpy as np
arr = np.arange(1,5).reshape(2,2)
print("Original Array:\n")
print(arr)
print("\Logarithmic value of array elements:\n")
log_val=np.log(arr)
print(log_val)

In the above example, we have created a 2×2 array using numpy.reshape() function and used random numbers to create data values using numpy.arange() method.

Further, numpy.log() method is used to find the log value of every element of the array.

Output:

Original Array:

[[1 2]
 [3 4]]

Logarithmic value of array elements:

[[0.         0.69314718]
 [1.09861229 1.38629436]]

Applying NumPy log on a NumPy array

Example:

import numpy as np
arr = np.array([10,20,30,4,5])
print("Original Array:\n")
print(arr)
print("\nLogarithm value of array elements:\n")
log_val=np.log(arr)
print(log_val)

We have created a NumPy array using numpy.array() function and used the numpy.log() method to calculate the log values of all the data items of the 1-D array.

Output:

Original Array:

[10 20 30  4  5]

Logarithm value of array elements:

[2.30258509 2.99573227 3.40119738 1.38629436 1.60943791]

Graphical representation of NumPy log

In order to have a better understanding of the calculated log values, we can plot the log values against the original values using Python Matplotlib module.

Example:

import numpy as np
import matplotlib.pyplot as plt
arr = np.array([10,20,30,40,50])
log_val=np.log(arr)
plt.plot(log_val,arr,marker='*',color='green')

In the above example, we have used pyplot.plot() method to plot the log values against the original array values.

Output:

Graphical Representation Of Numpy Log
Graphical Representation Of Numpy Log

Conclusion

Thus, in this article, we have understood the working of Python NumPy log method along with different cases.


References

  • Python NumPy log() — Documentation
  • Python NumPy log with a different base — Stack OverFlow
Safa Mulani
Safa Mulani

An enthusiast in every forthcoming wonders!

Articles: 189
How to Use the Python iter() Method?
Previous Post How to Use the Python iter() Method?
Next Post How to Use the Python delattr() Function?
How to Use the Python delattr() Function?

Related Posts

np.linalg.eig: Eigenvalues and Eigenvectors

np.linalg.eig: Eigenvalues and Eigenvectors

  • November 30, 2026
np.sin() in NumPy: Radians, Arrays, and Examples

How to Use np.sin() in NumPy

  • November 30, 2026
np.outer(): Outer Product in NumPy

np.outer(): Outer Product in NumPy

  • November 14, 2026

Partner Websites

  • GoLangDocs
  • LinuxForDevices
  • CodeForGeek

Popular Categories

  • Python Programming
  • Python Modules
  • OOPS in Python
  • Python Programming Examples
  • Strings in Python

Hall of Fame

  • Python Programming
  • OOPS in Python
  • Creating Python Modules
  • Args and Kwargs in Python
  • Python Dictionary (Dict) Tutorial
  • Python List – 15 Things You MUST Know
  • Python Set – Things You MUST Know
  • Python String Functions: Methods by Task

Important Links

  • Home
  • Python Course
  • Start Here
  • Blog
  • About AskPython
Latest posts
  • np.linalg.eig: Eigenvalues and Eigenvectors
  • What Does %s Mean in a Python Format String?
  • How to Use np.sin() in NumPy
  • cv2.imread: Load Images with OpenCV
  • pd.read_json: Read JSON Into a DataFrame

Copyright © 2026 - AskPython. All rights reserved.