numpy.select() function - Python Last Updated : 07 Apr, 2025 Comments Improve Suggest changes Like Article Like Report The numpy.select() function is used to construct an array by selecting elements from a list of choices based on multiple conditions. It is particularly useful when dealing with conditional replacements or transformations in NumPy arrays. Example: Python import numpy as np arr = np.array([10, 20, 30, 40]) conditions = [arr < 20, arr > 30] choices = [100, 200] result = np.select(conditions, choices, default=0) print(result) Syntax:numpy.select(condlist, choicelist, default=0)Parameters:condlist : list of bool ndarrays A list of boolean NumPy arrays that determine from which array in choicelist the output elements are selected. If multiple conditions are True, the first one encountered is used.choicelist : list of ndarrays A list of arrays from which the output elements are chosen. It must have the same length as condlist.default : scalar, optional (default=0) The value inserted in the output array when none of the conditions are met.Return Value:ndarray : An array with elements chosen from choicelist based on the conditions in condlist.Code Implementation 1. Basic Usage of numpy.select() Here:If arr < 3, the corresponding element is taken from arr.If arr > 4, the corresponding element is taken from arr**3.Otherwise, the default value 0 is used. Python import numpy as np arr = np.arange(8) condlist = [arr < 3, arr > 4] choicelist = [arr, arr**3] result = np.select(condlist, choicelist) print(result) Output :[ 0 1 2 0 0 125 216 343]2. Using a Different Default ValueHere:Values where arr < 4 are taken from arr.Values where arr > 6 are taken from arr**2.All other values are replaced with -1 (instead of 0). Python arr = np.arange(8) condlist = [arr < 4, arr > 6] choicelist = [arr, arr**2] # Custom default value (e.g., -1) result = np.select(condlist, choicelist, default=-1) print(result) Output: 0 1 2 3 -1 -1 -1 49]3. Handling Multiple ConditionsHere :If arr is even (arr % 2 == 0), it is multiplied by 10.If arr is divisible by 3 (arr % 3 == 0), it is negated.If neither condition is met, the default value 100 is used. Python arr = np.arange(10) condlist = [arr % 2 == 0, arr % 3 == 0] choicelist = [arr * 10, arr * -1] result = np.select(condlist, choicelist, default=100) print(result) Output: [ 0 100 20 -3 40 100 60 100 80 -9]Why Use numpy.select()?More flexible than numpy.where() when dealing with multiple conditions.Helps avoid complex nested if-else conditions in array transformations.Efficient and concise for applying different transformations to an array based on conditions.Comparison with numpy.where()Featurenumpy.select()numpy.where()Multiple ConditionsYesNo (only two conditions: True/False)Custom Default ValueYesNoSimplicityBetter for multiple rulesBetter for simple if-elseThe numpy.select() function is a powerful tool for conditional selection and transformation of array elements. It is especially useful when handling multiple conditions efficiently in a structured way. Mastering its usage will help simplify complex array operations in Python. Comment More infoAdvertise with us Next Article numpy.select() function - Python sanjoy_62 Follow Improve Article Tags : Machine Learning Numpy Python-numpy Python numpy-arrayManipulation python +1 More Practice Tags : Machine Learningpython Similar Reads Python Tutorial | Learn Python Programming Language Python Tutorial â Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly.Python is:A high-level language, used in web development, data science, automatio 10 min read Machine Learning Tutorial Machine learning is a branch of Artificial Intelligence that focuses on developing models and algorithms that let computers learn from data without being explicitly programmed for every task. In simple words, ML teaches the systems to think and understand like humans by learning from the data.It can 5 min read Linear Regression in Machine learning Linear regression is a type of supervised machine-learning algorithm that learns from the labelled datasets and maps the data points with most optimized linear functions which can be used for prediction on new datasets. It assumes that there is a linear relationship between the input and output, mea 15+ min read Support Vector Machine (SVM) Algorithm Support Vector Machine (SVM) is a supervised machine learning algorithm used for classification and regression tasks. It tries to find the best boundary known as hyperplane that separates different classes in the data. It is useful when you want to do binary classification like spam vs. not spam or 9 min read Logistic Regression in Machine Learning Logistic Regression is a supervised machine learning algorithm used for classification problems. Unlike linear regression which predicts continuous values it predicts the probability that an input belongs to a specific class. It is used for binary classification where the output can be one of two po 11 min read K means Clustering â Introduction K-Means Clustering is an Unsupervised Machine Learning algorithm which groups unlabeled dataset into different clusters. It is used to organize data into groups based on their similarity. Understanding K-means ClusteringFor example online store uses K-Means to group customers based on purchase frequ 4 min read K-Nearest Neighbor(KNN) Algorithm K-Nearest Neighbors (KNN) is a supervised machine learning algorithm generally used for classification but can also be used for regression tasks. It works by finding the "k" closest data points (neighbors) to a given input and makesa predictions based on the majority class (for classification) or th 8 min read Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and 9 min read 100+ Machine Learning Projects with Source Code [2025] This article provides over 100 Machine Learning projects and ideas to provide hands-on experience for both beginners and professionals. Whether you're a student enhancing your resume or a professional advancing your career these projects offer practical insights into the world of Machine Learning an 5 min read Introduction to Convolution Neural Network Convolutional Neural Network (CNN) is an advanced version of artificial neural networks (ANNs), primarily designed to extract features from grid-like matrix datasets. This is particularly useful for visual datasets such as images or videos, where data patterns play a crucial role. CNNs are widely us 8 min read Like