在numpy.ndarray中记录每个元素的日志?

时间:2017-01-13 20:37:50

标签: python arrays numpy

我目前正在努力获取numpy.ndarray的每个元素的日志。

根据文档,应该只能.log() ..

但我不能让它发挥作用?

>>> 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'
>>> 

1 个答案:

答案 0 :(得分:7)

numpy.log()不是numpy.ndarray的一种方法,它是numpy模块中的一个功能,所以你必须这样称呼它:

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]])