Back to Practice
#0036
Select Positive Values
EasyNumPy10 min10 XP
Problem
Given a NumPy array, return a new array containing only values greater than zero, preserving their original order.
Why This Matters
Boolean indexing is one of the first NumPy ideas that feels different from normal Python loops. It lets you filter whole arrays at once.
Function Signature
def positive_values(x):
Examples
Example 1
Inputx = np.array([-2, 5, 0, 3, -1])
Outputarray([5, 3])
Only 5 and 3 are greater than zero. The zero is not positive.
Constraints
- Input is a one-dimensional numeric NumPy array.
- Return a NumPy array, not a Python list.
- Do not sort the values.
CodePython
Visible browser tests run here when available.
Testcases2 visible / 4 hidden categories
Mixed signs
Inputnp.array([-2, 5, 0, 3, -1])
Expectednp.array([5, 3])
The boolean mask is [False, True, False, True, False].
No positives
Inputnp.array([-4, 0, -1])
Expectedempty array
No value satisfies x > 0.
Hidden Test Categories
All values positiveFloating point valuesSingle-element arrayLarge array