How to Solve Python AttributeError: ‘numpy.ndarray’ object has no attribute ‘median’

by | Programming, Python, Tips

This error occurs when you try to call the median method on a numpy.ndarray. Although numpy.ndarray has mean, max, min, std, methods, it does not have median as a method. The median method belongs to numpy.

To solve this error, you have to call numpy.median, for example,

median = np.median(arr)

This tutorial will go through the error and how to solve it with code examples.


AttributeError: ‘numpy.ndarray’ object has no attribute ‘median’

AttributeError occurs in a Python program when we try to access an attribute (method or property) that does not exist for a particular object. The median method belongs to the numpy class, not numpy.ndarray and it calculates a specified array’s median.

Note that numpy.ndarray does have other statistical methods like min, max, mean and std, for example:

import numpy as np

arr = np.array([2,4,6,8,10,12,14,16,18,20])

print(arr.min())
print(arr.max())
print(arr.mean())
print(arr.std())
2
20
11.0
5.744562646538029

Also, there are numpy equivalent methods which we can call as follows:

import numpy as np

arr = np.array([2,4,6,8,10,12,14,16,18,20])

print(np.min(arr))
print(np.max(arr))
print(np.mean(arr))
print(np.std(arr))
2
20
11.0
5.744562646538029

However, the median method is only a numpy method.

import numpy as np

arr = np.array([2,4,6,8,10,12,14,16,18,20])

print(np.median(arr))
print(arr.median())
11.0
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [16], in <cell line: 6>()
      3 arr = np.array([2,4,6,8,10,12,14,16,18,20])
      5 print(np.median(arr))
----> 6 print(arr.median())

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

Example

Consider the following example of a NumPy ndarray containing 20 integers.

import numpy as np

arr = np.array([10, 47, 33, 95, 11, 82, 61, 1, 82, 4, 3, 31, 65, 54, 42, 61, 53, 9, 36, 22])
arr
array([10, 47, 33, 95, 11, 82, 61,  1, 82,  4,  3, 31, 65, 54, 42, 61, 53,
        9, 36, 22])

We will attempt to calculate the median of the array as follows:

print(f'Median of array is {arr.median()}')

Let’s run the code to see what happens:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [6], in <cell line: 1>()
----> 1 print(f'Median of array is {arr.median()}')

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

The error occurs because median is not a method of numpy.ndarray. You can find the available methods for ndarray under the numpy documentation.

Solution

We can solve the error by using the numpy.median() method as follows:

median = np.median(arr)

print(f'Median of array is {median}')

Let’s run the code to see the result:

Median of array is 39.0

Summary

Congratulations on reading to the end of this tutorial! If you encounter this error, ensure that you use numpy.median(). If you want to use min, max, mean, std etc., you can use either the numpy.ndarray method or the numpy method.

For further reading on AttributeErrors, go to the article:

How to Solve Python AttributeError: ‘numpy.ndarray’ object has no attribute ‘drop’

Go to the online courses page on Python to learn more about coding in Python for data science and machine learning.

Have fun and happy researching!

Profile Picture
Senior Advisor, Data Science | [email protected] | + posts

Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.

Buy Me a Coffee ✨