gpt-4.1-2025-04-14· OpenAI
LMArena Human Preference 140K
how to filter 1d numpy array values based on condition
- User
how to filter 1d numpy array values based on condition
- Assistant
To filter a 1D NumPy array based on a condition, you can use boolean indexing. Here’s a step-by-step example:
Example
Suppose you have the following array:
import numpy as np arr = np.array([1, 2, 3, 4, 5, 6])1. Filtering values greater than 3:
filtered = arr[arr > 3] print(filtered)Output:
[4 5 6]2. Filtering even values:
filtered = arr[arr % 2 == 0] print(filtered)Output:
[2 4 6]3. Filtering values between 2 and 5 (inclusive):
filtered = arr[(arr >= 2) & (arr <= 5)] print(filtered)
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.