python - 属性错误 : 'numpy.ndarray' object has no attribute 'median'

标签 python numpy

我可以对 numpy 数组执行大量统计,但“中位数”返回属性错误。当我执行“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'
>>> 

最佳答案

虽然numpy.ndarraymean, max, std等方法,但它没有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 是“等价函数”,所以这只是语义问题。

关于python - 属性错误 : 'numpy.ndarray' object has no attribute 'median' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40112487/

相关文章:

python - Anacondas 安装搞砸了 IPython pandas 导入?

java - Jython 将 java File 对象数组从 python 发送到 java 类

python - 如何使用正则表达式来匹配文件中其他小写单词不连续重复的大写单词

python - 如何检查 pandas 数据框中的值范围?

python - Numpy:给定矩阵的非零索引如何将元素提取到子矩阵中

python - 取消引用 numpy 数组或列表的 void 指针

python - 使用 py2exe 时使用 Numpy 创建一个 tcl 文件夹

python - 如何按日期对值进行排序并按列删除重复项?

python - 使用导入的模块来引用嵌套模块

python - 通过边缘校正波浪图像