How to filter a numpy array based on two or more conditions?
MACHINE LEARNING RECIPES DATA CLEANING PYTHON DATA MUNGING PANDAS CHEATSHEET     ALL TAGS

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

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

0

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 - Creat 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".

Relevant Projects

Loan Eligibility Prediction in Python using H2O.ai
In this loan prediction project you will build predictive models in Python using H2O.ai to predict if an applicant is able to repay the loan or not.

Learn to prepare data for your next machine learning project
Text data requires special preparation before you can start using it for any machine learning project.In this ML project, you will learn about applying Machine Learning models to create classifiers and learn how to make sense of textual data.

Perform Time series modelling using Facebook Prophet
In this project, we are going to talk about Time Series Forecasting to predict the electricity requirement for a particular house using Prophet.

Forecast Inventory demand using historical sales data in R
In this machine learning project, you will develop a machine learning model to accurately forecast inventory demand based on historical sales data.

Predict Credit Default | Give Me Some Credit Kaggle
In this data science project, you will predict borrowers chance of defaulting on credit loans by building a credit score prediction model.

Data Science Project - Instacart Market Basket Analysis
Data Science Project - Build a recommendation engine which will predict the products to be purchased by an Instacart consumer again.

Machine Learning project for Retail Price Optimization
In this machine learning pricing project, we implement a retail price optimization algorithm using regression trees. This is one of the first steps to building a dynamic pricing model.

Customer Churn Prediction Analysis using Ensemble Techniques
In this machine learning churn project, we implement a churn prediction model in python using ensemble techniques.

Machine Learning Project to Forecast Rossmann Store Sales
In this machine learning project you will work on creating a robust prediction model of Rossmann's daily sales using store, promotion, and competitor data.

Data Science Project in Python on BigMart Sales Prediction
The goal of this data science project is to build a predictive model and find out the sales of each product at a given Big Mart store.