How to filter a numpy array based on two or more conditions?

This recipe helps you filter a numpy array based on two or more conditions

Recipe Objective

How to filter a numpy array based on two or more conditions?

Creating a new array from the existing array whereas taking out some elements from that existing array and then creating a new one is called as filtering. In case of filtering the elements whose value at an index is "True" that are going to be ontained in the filtered array otherwise if the values at an index is "False" then it will be excluded from that filtered array.

Step 1 - Import library

import numpy as np

Step 2 - Take a Sample array

Sample_array = np.array([55,60,65,70,75,80,85,90])

Step 3 - Create filter array

filter_array = [] ##take empty list for values in Sample_array: if values > 65: filter_array.append(True) ##append values which are greater than 65 else: filter_array.append(False) ##exclude values which are less than 65 new_filtered_array = Sample_array[filter_array] ## Apply it on the Sample array that we have taken

Step 4 - Print the results

print("The filtered array is:", new_filtered_array)

The filtered array is: [70 75 80 85 90]

Here we can see the array has been filtered, as we have pass a condition where if the values are than "65" append that values and exclude the values which are less than "65".

What Users are saying..

profile image

Abhinav Agarwal

Graduate Student at Northwestern University
linkedin profile url

I come from Northwestern University, which is ranked 9th in the US. Although the high-quality academics at school taught me all the basics I needed, obtaining practical experience was a challenge.... Read More

Relevant Projects

Build CNN for Image Colorization using Deep Transfer Learning
Image Processing Project -Train a model for colorization to make grayscale images colorful using convolutional autoencoders.

Build Multi Class Text Classification Models with RNN and LSTM
In this Deep Learning Project, you will use the customer complaints data about consumer financial products to build multi-class text classification models using RNN and LSTM.

Time Series Classification Project for Elevator Failure Prediction
In this Time Series Project, you will predict the failure of elevators using IoT sensor data as a time series classification machine learning problem.

Deploy Transformer-BART Model on Paperspace Cloud
In this MLOps Project you will learn how to deploy a Tranaformer BART Model for Abstractive Text Summarization on Paperspace Private Cloud

Recommender System Machine Learning Project for Beginners-3
Content Based Recommender System Project - Building a Content-Based Product Recommender App with Streamlit

Build a Collaborative Filtering Recommender System in Python
Use the Amazon Reviews/Ratings dataset of 2 Million records to build a recommender system using memory-based collaborative filtering in Python.

Personalized Medicine: Redefining Cancer Treatment
In this Personalized Medicine Machine Learning Project you will learn to classify genetic mutations on the basis of medical literature into 9 classes.

MLOps Project for a Mask R-CNN on GCP using uWSGI Flask
MLOps on GCP - Solved end-to-end MLOps Project to deploy a Mask RCNN Model for Image Segmentation as a Web Application using uWSGI Flask, Docker, and TensorFlow.

Loan Eligibility Prediction Project using Machine learning on GCP
Loan Eligibility Prediction Project - Use SQL and Python to build a predictive model on GCP to determine whether an application requesting loan is eligible or not.

Build a Multi Touch Attribution Machine Learning Model in Python
Identifying the ROI on marketing campaigns is an essential KPI for any business. In this ML project, you will learn to build a Multi Touch Attribution Model in Python to identify the ROI of various marketing efforts and their impact on conversions or sales..