Movatterモバイル変換


[0]ホーム

URL:


Menu
×
Sign In
+1 Get Certified For Teachers Spaces Plus Get Certified For Teachers Spaces Plus
   ❮     
     ❯   

NumPy Logs


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 thelog2() function to perform log at the base 2.

Example

Find log at base 2 of all elements of following array:

import numpy as np

arr = np.arange(1, 10)

print(np.log2(arr))
Try it Yourself »

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


Log at Base 10

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

Example

Find log at base 10 of all elements of following array:

import numpy as np

arr = np.arange(1, 10)

print(np.log10(arr))
Try it Yourself »

Natural Log, or Log at Base e

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

Example

Find log at base e of all elements of following array:

import numpy as np

arr = np.arange(1, 10)

print(np.log(arr))
Try it Yourself »

Log at Any Base

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

Example

from math import log
import numpy as np

nplog = np.frompyfunc(log, 2, 1)

print(nplog(100, 15))
Try it Yourself »


 
Track your progress - it's free!
 

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness
of all content. While using W3Schools, you agree to have read and accepted ourterms of use,cookie and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved.W3Schools is Powered by W3.CSS.


[8]ページ先頭

©2009-2025 Movatter.jp