How can I remove some specific elements from a numpy array? Say I have
import numpy as np
a = np.array([1,2,3,4,5,6,7,8,9])
I then want to remove 3,4,7 from a. All I know is the index of the values (index=[2,3,6]).
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Use
numpy.delete(), which returns a new array with sub-arrays along an axis deleted.For your specific question:
Note that
numpy.delete()returns a new array since array scalars are immutable, similar to strings in Python, so each time a change is made to it, a new object is created. I.e., to quote thedelete()docs:If the code I post has output, it is the result of running the code.