AttributeError:' numpy.ndarray'对象没有属性' median'

时间:2016-10-18 15:37:22

标签: python numpy

我可以对numpy数组执行大量统计,但是" median"返回属性错误。当我做一个" dir(np)"我确实看到列出的中位数方法。

(newpy2) 7831c1c083a2:src scaldara$ python
Python 2.7.12 |Continuum Analytics, Inc.| (default, Jul  2 2016,   17:43:17) 
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)] on     darwin
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org

>>> import numpy as np
>>> print(np.version.version)
1.11.2
>>> a = np.array([1,2,3,4,5,6,7,8,9,10])
>>> print(a)
[ 1  2  3  4  5  6  7  8  9 10]
>>> print(a.min())
1
>>> print(a.max())
10
>>> print(a.mean())
5.5
>>> print(a.std())
2.87228132327
>>> print(a.median())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'numpy.ndarray' object has no attribute 'median'
>>> 

1 个答案:

答案 0 :(得分:10)

虽然numpy.ndarraymeanmaxstd等方法,但它没有median方法。有关ndarray可用的所有方法的列表,请参阅numpy documentation for ndarray

它可以作为一个函数使用数组作为参数:

>>> import numpy as np
>>> a = np.array([1,2,3,4,5,6,7,8,9,10])
>>> np.median(a)
5.5

正如您将在the documentation for ndarray.mean中看到的那样,ndarray.meannp.mean是“等效函数”,所以这只是一个语义问题。