NumPy Tutorial · NumPy ufunc

ufunc Logs

Learn all about ufunc Logs in this comprehensive tutorial.

5 min read advanced
  • NumPy provides functions to perform log at the base 2, e and 10.
  • Use the log2() function to perform log at the base 2.
  • Use the log10() function to perform log at the base 10.
  • Use the log() function to perform log at the base e.
  • NumPy does not provide any function to take log at any base, so we can use the frompyfunc() function along with inbuilt function math.

Logs

NumPy provides functions to perform log at the base 2, e and 10.

We will also explore how we can take log for any base by creating a custom ufunc.

All of the log functions will place -inf or inf in the elements if the log can not be computed.

Log at Base 2

Use the log2() function to perform log at the base 2.

python
Note: Note: The arange(1, 10) function returns an array with integers starting from 1 (included) to 10 (not included).

Log at Base 10

Use the log10() function to perform log at the base 10.

python

Natural Log, or Log at Base e

Use the log() function to perform log at the base e.

python

Log at Any Base

NumPy does not provide any function to take log at any base, so we can use the frompyfunc() function along with inbuilt function math.log() with two input parameters and one output parameter:

python

Module quiz

2 questions
1

Which of the following is true about ufunc Logs?

2

What is the most common pitfall when working with ufunc Logs?

Answer all questions to submit.