For instance if you want to remove the third column from an array of shape (2, 3) :
import numpy as np
a = np.ones((2, 3))
b = np.delete(a, 2, axis=1)
Note that delete does not work in-place, so a is unmodified. If you want to keep working on a do :
a = np.delete(a, 2, axis=1)
This will assign the new array to the same variable.
Answer from Nicolas Barbey on Stack OverflowProblems with numpy.delete()
debugging - Numpy: np.delete is not removing values in the array - Stack Overflow
python - Numpy.delete() function not properly deleting element at index - Stack Overflow
problems with numpy.delete
Hey lads n gals
I am working on an assignment, where we are doing a grading system for students on the -3 to 12 scale...
here is the code so far:
import numpy as np
from RoundGrade import roundGrade
def computeFinalGrade(grades):
if -3 in grades:
return -3
if len(grades)==1:
return grades[0]
if len(grades)>=2:
grademin=grades.delete(min(grades))
average=np.mean(grademin)
return roundGrade(average)
grades=np.array([6,9,2,5,2])
print(computeFinalGrade(grades))
however this gives me the error messege:
'numpy.ndarray' object has no attribute 'delete'
i have tried converting the array to a list, but it doesn't help
thanks <3
It's because delete function, returns a copy of changed array.
You need to do this:
import numpy as np
m = np.array([[1], [1], [3]])
m = np.delete(m, 2)
print(m)
In according to numpy documentation: Return a new array with sub-arrays along an axis deleted.
Referring numpy documentation for np.delete :-
Returns out - ndarray A copy of arr with the elements specified by obj removed. Note that delete does not occur in-place. If axis is None, out is a flattened array.
Correct code is
import numpy as np
m = np.array([[1], [1], [3]])
m = np.delete(m, 2,0)
print(m)
Output:
[[1] [1]]
I am trying to graph 2 sets of data, radio loud and radio quiet quasars, with radio loud quasars being blue and radio quiet quasars being red. Numpy delete has worked to delete all the indices I've intended except when I try sorting radio loud quasar data from radio quiet. Segment of my code giving me issues:
radians = 0.0174532925
dist = 130658597000000000000000
values = radioflux[0:166583]
searchval = 0
searchvalquietloud = 1
indices = np.where(values == searchval)[0]
radiocut = np.delete(radioflux[0:166583],indices)
quiet = np.where(values <= searchvalquietloud)[0]
radioloud = np.delete(radiocut[0:12464],quiet)
loud = np.where(values > searchvalquietloud)[0]
radioquiet = np.delete(radiocut[0:12464],loud)
distance = np.multiply(redshift,dist)
angle = np.multiply(ra,radians)
distancecut = np.delete(distance,indices)
anglecut = np.delete(angle,indices)
distancequiet = np.delete(distancecut,radioloud)
distanceloud = np.delete(distancecut,radioquiet)
anglequiet = np.delete(anglecut,radioloud)
angleloud = np.delete(anglecut,radioquiet)
The number of items in each array is:
Radio Flux: 166583
Radio Cut: 12464
Radio Loud: 239
Radio Quiet: 12225
Distance Cut: 12464
Angle Cut: 12464
Distance Loud: 12120
Distance Quiet: 12424
Angle Loud: 12120
Angle Quiet: 12424
Everything is how it should be except the last four. They should be:
Distance Loud: 239
Distance Quiet: 12225
Angle Loud: 239
Angle Quiet: 12225
What is wrong with my code?
Thanks for all your help!
To me, it appears that you are simply trying to delete an index that is out of the array; hence the lack of change ...
From your code len(scale) gives only 17 .
For the record as the doc indicates, numpy.delete(arr,obj) will try to delete the element returned by arr[obj] for a 1-D array so :
- numpy.delete(arr,0)
- numpy.delete(arr,[0])
- numpy.delete(arr,0.0)
- numpy.delete(arr,[0.0])
will all delete arr[0] which is the zero-th element of that 1-D array.
first, delete does not operate in place:
In [849]: a=np.arange(10)
In [850]: np.delete(a,[1])
Out[850]: array([0, 2, 3, 4, 5, 6, 7, 8, 9]) # returned array
In [851]: a
Out[851]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) # not change in a
If I do an out of bounds delete with a scalar, I get an error:
In [853]: a1=np.delete(a,11)
...
IndexError: index 11 is out of bounds for axis 0 with size 10
But if the delete is a list, it appears the bounds check does not operate (is there a parameter for that?)
In [854]: a1=np.delete(a,[11])
In [855]: a1
Out[855]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
a1 is a copy though; changing one of its values does not affect a.
In [858]: a1[-1]=100
In [859]: a1
Out[859]: array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 100])
In [860]: a
Out[860]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
np.delete is a complicated Python function, written to be quite general. It can be studied. It is not a fundamental function. It is not doing anything you can't do just as well with basic array operations like masking, indexing and/or selective copying. And it isn't going to be faster.
================
You can study np.delete. My memory is that it often does the following:
delete via mask for scalar:
In [863]: mask=np.ones(a.shape,dtype=bool)
In [864]: mask[1]=False
In [865]: a[mask]
Out[865]: array([0, 2, 3, 4, 5, 6, 7, 8, 9])
for list:
In [866]: mask=np.ones(a.shape,dtype=bool)
In [867]: mask[[1,3,5]]=False
In [868]: a[mask]
Out[868]: array([0, 2, 4, 6, 7, 8, 9])
==================
Recreation of your script with added displays:
In [874]: scale=[1,2]
In [875]: for h in range(500,3001,500):scale.append(h)
In [876]: len(scale)
Out[876]: 8
In [877]: scale+=[4000,5000]
In [878]: for h in range(7000,17001,2000):scale.append(h)
In [879]: len(scale)
Out[879]: 16
In [880]: scale.append(1000)
In [881]: Scale=np.array(scale)
In [882]: Scale.shape
Out[882]: (17,)
In [883]: np.delete(Scale,[1000])
Out[883]:
array([ 1, 2, 500, 1000, 1500, 2000, 2500, 3000, 4000,
5000, 7000, 9000, 11000, 13000, 15000, 17000, 1000])
So there are only 17 items in the array, not a 1000. And as I illustrated with a delete list, it does not raise an error if it is out of bounds. Hence the delete result is a copy of the input.
By the way I create that same array with an array concatenate
In [886]: np.r_[1, 2, 500:3001:500, [4000,5000], 7000:17001:2000, 1000]
Out[886]:
array([ 1, 2, 500, 1000, 1500, 2000, 2500, 3000, 4000,
5000, 7000, 9000, 11000, 13000, 15000, 17000, 1000])
Even sticking with the list I can avoid the loops with extend:
In [887]: scale=[1,2]
In [888]: scale.extend(range(500,3001,500))
In [889]: scale.extend([4000,5000])
In [890]: scale.extend(range(7000,17001,2000))
In [891]: scale.append(1000)