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

Jingwei Li

Graduate Research assistance at Stony Brook University
linkedin profile url

ProjectPro is an awesome platform that helps me learn much hands-on industrial experience with a step-by-step walkthrough of projects. There are two primary paths to learn: Data Science and Big Data.... Read More

Relevant Projects

Build Portfolio Optimization Machine Learning Models in R
Machine Learning Project for Financial Risk Modelling and Portfolio Optimization with R- Build a machine learning model in R to develop a strategy for building a portfolio for maximized returns.

Multi-Class Text Classification with Deep Learning using BERT
In this deep learning project, you will implement one of the most popular state of the art Transformer models, BERT for Multi-Class Text Classification

Deep Learning Project for Beginners with Source Code Part 1
Learn to implement deep neural networks in Python .

Multilabel Classification Project for Predicting Shipment Modes
Multilabel Classification Project to build a machine learning model that predicts the appropriate mode of transport for each shipment, using a transport dataset with 2000 unique products. The project explores and compares four different approaches to multilabel classification, including naive independent models, classifier chains, natively multilabel models, and multilabel to multiclass approaches.

Build Regression (Linear,Ridge,Lasso) Models in NumPy Python
In this machine learning regression project, you will learn to build NumPy Regression Models (Linear Regression, Ridge Regression, Lasso Regression) from Scratch.

Walmart Sales Forecasting Data Science Project
Data Science Project in R-Predict the sales for each department using historical markdown data from the Walmart dataset containing data of 45 Walmart stores.

Langchain Project for Customer Support App in Python
In this LLM Project, you will learn how to enhance customer support interactions through Large Language Models (LLMs), enabling intelligent, context-aware responses. This Langchain project aims to seamlessly integrate LLM technology with databases, PDF knowledge bases, and audio processing agents to create a comprehensive customer support application.

Build an End-to-End AWS SageMaker Classification Model
MLOps on AWS SageMaker -Learn to Build an End-to-End Classification Model on SageMaker to predict a patient’s cause of death.

Build Piecewise and Spline Regression Models in Python
In this Regression Project, you will learn how to build a piecewise and spline regression model from scratch in Python to predict the points scored by a sports team.

Word2Vec and FastText Word Embedding with Gensim in Python
In this NLP Project, you will learn how to use the popular topic modelling library Gensim for implementing two state-of-the-art word embedding methods Word2Vec and FastText models.