简体   繁体   中英

Taking the log of each element in a numpy.ndarray?

I currently struggling with taking the log of each element of an numpy.ndarray.

According to the documentation should one be able to just .log() ..

But i cannot make it work?

>>> import numpy
>>> x = numpy.random.rand(3,3)
>>> x
array([[ 0.42631996,  0.13211157,  0.09500156],
       [ 0.5699495 ,  0.39852007,  0.55909279],
       [ 0.12059745,  0.40645911,  0.68107768]])
>>> x.log
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'numpy.ndarray' object has no attribute 'log'
>>> 

numpy.log() isn't a method of the numpy.ndarray , it's a function in the numpy module, so you have to call it as such:

In [1]: import numpy

In [2]: x = numpy.random.rand(3,3)

In [3]: x
Out[3]:
array([[ 0.05047638,  0.40156621,  0.29631731],
       [ 0.21764918,  0.49813348,  0.48520004],
       [ 0.59433129,  0.53859086,  0.66388153]])

In [4]: numpy.log(x)
Out[4]:
array([[-2.98624972, -0.91238286, -1.21632442],
       [-1.52487076, -0.69688721, -0.72319401],
       [-0.52031839, -0.61879907, -0.40965157]])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM