Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have an array of arrays and I'm trying to find the lowest non-zero value among them all.

minima = []
for array in K: #where K is my array of arrays (all floats)
    if 0.0 in array:
        array.remove(0.0)
    minima.append(min(array))

print min(minima)

This yields

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

I thought array.remove() was the way to remove an element. What am I doing wrong?

like image 957
berkelem Avatar asked May 12 '15 17:05

berkelem


People also ask

What is attributeerror ‘NumPy’ object has no attribute ‘ append’?

What is AttributeError: ‘numpy.ndarray’ object has no attribute ‘append’? In Python, it is common to use the append () method to add an element to the end of the array like we do in the list.

Why does NumPy array has no append?

The AttributeError: ‘numpy.ndarray’ object has no attribute ‘append’ occurs if you attempt to append an element into NumPy array using the regular append () method like we do in the list. The numpy.ndarray does not have an append method, and hence it throws AttributeError.

How do I see what features a NumPy array has kept?

Numpy arrays have no attribute named columns If you want to see what features SelectFromModel kept, you need to substitute X_train (which is a numpy.array) with X which is a pandas.DataFrame. This will return a list of the columns kept by the feature selector.

Why is index () not working on my NumPy array?

This error occurs when you attempt to use the index () function on a NumPy array, which does not have an index attribute available to use. The following example shows how to address this error in practice.


1 Answers

I think I've figured it out. The .remove() method is a list method, not an ndarray method. So by using array.tolist() I can then apply the .remove() method and get the required result.

like image 67
berkelem Avatar answered Sep 17 '22 18:09

berkelem