Numpy where function multiple conditions
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
np.where is one of the most useful NumPy tools for vectorized conditional logic, but multiple conditions are also where many beginners get tripped up. The syntax looks close to normal Python conditionals, yet the rules are different because NumPy operates element by element across arrays. Once you understand the operators and grouping rules, multi-condition expressions become both fast and readable.
Combine comparisons with elementwise operators
Each comparison on a NumPy array produces another array of booleans. To combine those boolean arrays, use elementwise operators such as & for logical and, | for logical or, and ~ for logical not. Each comparison should be wrapped in parentheses.
The output keeps the same shape as the input array. Elements that satisfy the mask keep their original value, while the rest are replaced with -1.
Do not use and or or
This is the most common mistake. Python's and and or are for single truth values, not entire arrays. NumPy arrays do not have one obvious true or false value, so Python raises an error when you try to use those operators directly.
The parentheses matter because & has different precedence from comparison operators. Without them, you can end up combining raw arrays and numbers in a way that produces wrong results or confusing exceptions.
Use named masks to keep logic readable
As conditions grow, readability becomes more important than squeezing everything into one line. Giving masks meaningful names makes the business rule easier to review and test.
This pattern is easier to maintain than repeating the comparisons in a deeply nested expression. It also makes debugging simpler because you can print each mask independently.
Choose between np.where, filtering, and np.select
np.where is ideal when you want an output array with the same shape as the input. If you want only the matching elements, boolean indexing is usually a better fit.
For more than two branches, np.select is often clearer than stacking multiple np.where calls.
When the logic has three or more branches, np.select usually reads closer to the intent.
Handle NaN values explicitly
Comparisons involving NaN do not behave like normal numbers. For example, np.nan > 4 is false, which means missing values can quietly fall into your fallback branch unless you check for them intentionally.
If missing values are meaningful in your pipeline, define an explicit rule for them instead of letting them ride along accidentally.
Common Pitfalls
- Using Python
andororinstead of NumPy's elementwise&and|. - Forgetting parentheses around each comparison in a combined condition.
- Nesting too many
np.wherecalls whennp.selectwould be clearer. - Ignoring how
NaNbehaves during comparisons. - Using
np.wherewhen boolean indexing is the simpler tool for the job.
Summary
- Multi-condition
np.whereworks by combining boolean arrays with&,|, and~. - Always wrap each comparison in parentheses.
- Use named masks when the rule is complex enough to deserve explanation.
- Prefer boolean indexing for filtering and
np.selectfor many branches. - Treat
NaNexplicitly so missing values do not silently distort your logic.
Related reading
- On-line iterator algorithms for estimating statistical median, mode, skewness, kurtosis?
- One hot coding in Train Validation and Test set Production data
- Online algorithm for calculating absolute deviation
- Options for deploying R models in production
- Nvidia Cudatoolkit vs Conda Cudatoolkit
- Object of custom type as dictionary key
- Ordinal classification packages and algorithms
- out of sample definition
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.