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

by | Programming, Python, Tips

If you attempt to call the remove() method on a NumPy array, you will raise the error AttributeError: ‘numpy.ndarray’ object has no attribute ‘remove’. The remove() method belongs to the List data type. This error typically occurs when trying to remove values from an array. You can solve this error by converting the array to a list using the ndarray method tolist() or the built-in Python list() method.

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


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

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 remove method belongs to the List data type, not numpy.ndarray and removes a specified item from a list.

Example

Consider the following example of a numpy array containing 20 random integers between 0 and 100.

import numpy as np

arr = np.random.randint(0, 100, (20))

arr
array([22, 27, 97,  9, 50, 16,  0, 82, 79, 60, 13, 67, 39,  5, 92, 33, 97,
       64, 55, 10])

We will attempt to remove the numbers in the array that are lower than 50. We will use a for loop to iterate over the elements and an if-statement to check each element.

for x in arr:
    if x < 50:
        arr.remove(x)

Let’s run the code to see what happens:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [16], in <cell line: 1>()
      1 for x in np.nditer(arr):
      2     if x < 50:
----> 3         arr.remove(x)

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

The error occurs because we tried to call remove on the array. The remove method belongs to the List data type.

Solution #1: Convert Ndarray to List

The first way we can solve this error is by converting the array to a list. We can use either tolist(), which is a numpy.ndarray method or list(), which is a built-in Python method. Let’s look at the implementation of both:

Convert numpy.ndarray to list Using tolist()

lst = arr.tolist()
lst
[22, 27, 97, 9, 50, 16, 0, 82, 79, 60, 13, 67, 39, 5, 92, 33, 97, 64, 55, 10]

Convert numpy.ndarray to list Using list()

lst = list(arr)
lst
[22, 27, 97, 9, 50, 16, 0, 82, 79, 60, 13, 67, 39, 5, 92, 33, 97, 64, 55, 10]

Now that we have a list, we can use list comprehension to remove the elements lower than 50. List comprehension provides a concise way to create a new list based on the values of an existing list. Let’s look at the revised code:

lst = [i for i in lst if i > 50]

Let’s run the code to see the result:

[97, 82, 79, 60, 67, 92, 97, 64, 55]

We successfully removed the integers with values lower than 50. We can convert the list back to a numpy.ndarray using the numpy.array method, as follows:

new_arr = np.array(lst)
new_arr
array([97, 82, 79, 60, 67, 92, 97, 64, 55])

Solution #2: Use numpy.delete()

The second way to solve this error is to use numpy.delete(). First, we need to get the indices of the values that satisfy the condition of being less than 50. We can find the indices by using the numpy.where() function as follows:

indices = np.where(arr<50)

indices
(array([ 0,  1,  3,  5,  6, 10, 12, 13, 15, 19]),)

The indices variable is an array of indices for numbers smaller than 50 in the array. Next, we can use numpy.delete. The function will delete each of the corresponding elements from the array, and we will assign the array to the variable new_arr. Let’s look at the revised code:

new_arr = np.delete(arr, indices)

new_arr

Let’s run the code to see the result:

array([97, 50, 82, 79, 60, 67, 92, 97, 64, 55])

We successfully removed the numbers from the array that were smaller than 50. Also, note that we did not have to convert back to an array as we used numpy methods, not list methods, to solve the error.

Summary

Congratulations on reading to the end of this tutorial! If you encounter this error, you can get the indices of the values you want to remove using numpy.where then use numpy.delete to remove those values. Alternatively, you could convert the array to a list and use the remove method.

For further reading on AttributeErrors with numpy.ndarray, go to the articles:

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!

Research Scientist at Moogsoft | + posts

Suf is a research scientist at Moogsoft, specializing in Natural Language Processing and Complex Networks. Previously he was a Postdoctoral Research Fellow in Data Science working on adaptations of cutting-edge physics analysis techniques to data-intensive problems in industry. In another life, he was an experimental particle physicist working on the ATLAS Experiment of the Large Hadron Collider. His passion is to share his experience as an academic moving into industry while continuing to pursue research. Find out more about the creator of the Research Scientist Pod here and sign up to the mailing list here!

Follow the Research Scientist Pod on Social media!