What does np.clip do, and when would you reach for it?
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
What does `np.clip` do, and when would you reach for it?
Describe its behavior and arguments, and give practical cases from data science or machine learning work where it is the right tool.
```hint Think about the edges of a range
Picture operations that break or misbehave at the edge of a valid range, such as taking a log or storing a pixel value, and decide what should happen to values that fall outside it.
```
### What a Strong Answer Covers
- What the function does to values below, inside and above the bounds
- The arguments, clipping from one side with `None`, and per-element bounds through broadcasting
- Practical uses, such as guarding a log or a division, keeping pixel or probability values in range, and capping outliers
- Pitfalls such as NaN values, integer overflow that happens before the clip, and reversed bounds
### Follow-up Questions
- How does capping a feature at its 1st and 99th percentiles differ from dropping those rows, and what can go wrong at inference time?
- Why do log-loss implementations clip predicted probabilities, and roughly what value would you clip to?
- What does gradient clipping by value do, and how does it differ from clipping by the gradient's norm?
- What does `np.clip` return for a NaN entry, and how would you deal with missing values in the same step?
Overview: Explain what NumPy's np.clip function does to values outside a range and when it is the right tool in data science and machine learning code. Tests knowledge of its arguments, one-sided and broadcast bounds, practical uses such as guarding a log, and common pitfalls.