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

Ste*_*ara 12 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'
>>> 
Run Code Online (Sandbox Code Playgroud)

bri*_*pck 12

虽然numpy.ndarraymean,max,std等方法,它没有一个median方法.有关可用的所有方法的列表ndarray,请参阅numpy文档ndarray.

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

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

正如您将在文档中ndarray.mean看到,ndarray.mean并且np.mean是"等效函数",所以这只是语义问题.

  • 为什么他们选择不将其添加为方法? (5认同)
  • 人们应该在 github 上添加一个问题,以使中值()作为一种方法可用,就像其他函数(mean、std、max...)一样。没有理由排除中位数。 (2认同)