In this guide, we will discuss how to fix this error by converting the numpy array to a List and using the remove() method to delete the element. After that, we will convert the List back to a numpy array. Also, we can utilize the numpy.delete() function to delete single or multiple elements from the numpy array across the specified axis.
Reproducing the Error
First, we will use the remove() method on the numpy array and see the error in detail.
Create a numpy array, Industry, with five types (strings) and try to remove ‘Biotechnology’ from it.
# Create numpy array - Industry with 5 strings
Industry = numpy.array(['Biotechnology','Chemicals','Communications','Construction','Consulting'])
print(Industry,"\n")
# Try to remove 'Biotechnology' from the Industry
Industry.remove('Biotechnology')
Output
You can see that an error is encountered.
Solution 1: Use remove() by converting to List
The remove() method takes a single element as a parameter to be removed from the List.
Syntax
Let’s see the syntax of how to convert a numpy array to List and use the remove() method. Use the numpy.asarray() to convert the List back to the numpy array.
- Convert to List: List = array.tolist()
- Remove the element: List.remove(element)
- Convert back to numpy array: array= numpy.asarray(List)
Example
Using the same numpy array, remove ‘Biotechnology’ by converting this numpy array (Industry) to a List.
# Create numpy array - Industry with 5 strings
Industry = numpy.array(['Biotechnology','Chemicals','Communications','Construction','Consulting'])
print(Industry,"\n")
# Remove 'Biotechnology' from the Industry
# by converting into List.
List= Industry.tolist()
List.remove('Biotechnology')
# Convert the List back to numpy array
Industry = numpy.asarray(List)
print(Industry)
Output
You can see that ‘Biotechnology’ is removed from the List, and the List is converted back to an array again using the numpy.asarray() method.
Solution 2: Use numpy.delete()
The numpy.delete() function deletes single or multiple elements from the numpy array along the specified axis based on the index positions. If the axis is not specified, the element is applied to the flattened numpy array.
Syntax
Let’s see the syntax and parameters passed to this function.
- array is the input array.
- obj takes single or multiple element index positions to specify which elements are deleted.
- axis – Row elements are deleted if axis is set to 0, while column elements will be deleted if axis is set to 1. If the axis is not specified, the element is applied to the flattened numpy array.
Example 1: Remove Single Element
Use the same numpy array and remove ‘Biotechnology’.
# Create numpy array - Industry with 5 strings
Industry = numpy.array(['Biotechnology','Chemicals','Communications','Construction','Consulting'])
print(Industry,"\n")
# Remove 'Biotechnology' from the Industry
Industry=numpy.delete(Industry, 0)
print(Industry)
Output
You can see that ‘Biotechnology’ is removed from the numpy array.
Example 2: Remove Multiple Elements
Remove ‘Chemicals’ (Index = 1), ‘Communications'(Index = 2), ‘Construction'(Index = 3) from the Industry.
# Create numpy array - Industry with 5 strings
Industry = numpy.array(['Biotechnology','Chemicals','Communications','Construction','Consulting'])
print(Industry,"\n")
# Remove 'Chemicals','Communications','Construction' from the Industry
Industry=numpy.delete(Industry, [1,2,3])
print(Industry)
Output
‘Biotechnology’ and ‘Consulting’ are the two strings that remain in the array after removing the above three elements.
Example 3: axis = 0
Create a numpy array, Industry, with two rows and two columns – [[‘Biotechnology’, ‘Chemicals’], [‘Communications’, ‘Construction’]].
- Remove the second row by specifying the index as 1 and axis as 0.
- Remove all rows by specifying the indices as [0,1] and axis as 0.
Industry = numpy.array([['Biotechnology','Chemicals'],['Communications','Construction']])
print(Industry,"\n")
# Remove second row
Industry2=numpy.delete(Industry, 1,axis=0)
print(Industry2,"\n")
# Remove all rows
Industry3=numpy.delete(Industry, [0,1],axis=0)
print(Industry3)
Output
Example 4: axis = 1
Create a numpy array with two rows and two columns.
- Remove the first column by specifying the index as 0 and axis as 1.
- Remove all columns by specifying the indices as [0,1] and axis as 1
Industry = numpy.array([['Biotechnology','Chemicals'],['Communications','Construction']])
print(Industry,"\n")
# Remove first column
Industry2=numpy.delete(Industry, 0,axis=1)
print(Industry2,"\n")
# Remove all columns
Industry3=numpy.delete(Industry, [0,1],axis=1)
print(Industry3)
Output
Conclusion
To avoid the error AttributeError: ‘numpy.ndarray’ object has no attribute ‘remove,’ refrain from using the remove() method directly on the numpy array. First, it has to be converted to a List object using the toList() function. Another solution to fix this error is to utilize the numpy.delete() function to delete specific elements from the numpy array over the specified axis.