Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AttributeError: 'numpy.ndarray' object has no attribute 'median'

Tags:

I can perform a number of statistics on a numpy array but "median" returns an attribute error. When I do a "dir(np)" I do see the median method listed.

(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'
>>> 
like image 952
Steve Caldara Avatar asked Oct 18 '16 15:10

Steve Caldara


1 Answers

Although numpy.ndarray has a mean, max, std etc. method, it does not have a median method. For a list of all methods available for an ndarray, see the numpy documentation for ndarray.

It is available as a function that takes the array as an argument:

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

As you will see in the documentation for ndarray.mean, ndarray.mean and np.mean are "equivalent functions," so this is just a matter of semantics.

like image 61
brianpck Avatar answered Sep 22 '22 20:09

brianpck