Predictive Hacks

How to Remove Elements from a Numpy Array based on a Value

let’s say that we have a numpy array and we want to remove all the elements which are equal to a specific value. Below, we will remove all the elements which are equal to 5.

import numpy as np

myarr = np.array([1,2,3,4,5,1,2,3,4,5])
myarr
array([1, 2, 3, 4, 5, 1, 2, 3, 4, 5])
# remove all the elements which are equal to 5
myarr = myarr[myarr!=5]
myarr
array([1, 2, 3, 4, 1, 2, 3, 4])

As we can see, we removed the 5 element from the array.

Share This Post

Share on facebook
Share on linkedin
Share on twitter
Share on email

Subscribe To Our Newsletter

Get updates and learn from the best

More To Explore

Python

Image Captioning with HuggingFace

Image captioning with AI is a fascinating application of artificial intelligence (AI) that involves generating textual descriptions for images automatically.

Python

Intro to Chatbots with HuggingFace

In this tutorial, we will show you how to use the Transformers library from HuggingFace to build chatbot pipelines. Let’s