Join Our 5-Week ML/AI Engineer Interview Bootcamp 🚀 led by ML Tech Leads at FAANGs

Back to Questions

148. Feature clipping

easy
GeneralGeneral
senior

Feature clipping is a simple feature-engineering trick to reduce the impact of extreme values by capping them at chosen thresholds. In this task, you’ll clip each numeric feature column using per-feature lower and upper bounds.

Requirements

Implement the function

python

Rules:

  • Clip each value with: xi,j=min(max(xi,j,lowerj),upperj)x'_{i,j} = \min(\max(x_{i,j}, \text{lower}_j), \text{upper}_j)
  • Do not modify the input X in place; return a new clipped 2D array.
  • Use only NumPy and Python built-in libraries (no pandas).
  • Treat each column independently using its corresponding bounds.
  • Keep the output values as floating point numbers.

Example

python

Output:

python
Input Signature
ArgumentType
Xnp.ndarray
lowernp.ndarray
uppernp.ndarray
Output Signature
Return NameType
valuenp.ndarray

Constraints

  • Return new 2D array; do not modify X.

  • Use Python/NumPy only; no pandas.

  • Bounds are per-column: lower[j], upper[j].

Hint 1

Use np.clip(X, lower, upper) to efficiently clip the array.

Hint 2

NumPy broadcasting automatically handles per-column clipping when lower and upper are of shape (n_features,).

Hint 3

This single function call processes the entire matrix at once without loops.

Roles
ML Engineer
AI Engineer
Companies
GeneralGeneral
Levels
senior
entry
Tags
feature-engineering
clipping
2D-lists
array-iteration
49 people are solving this problem
Python LogoPython Editor
Ln 1, Col 1

Input Arguments

Edit values below to test with custom inputs

You need tolog in/sign upto run or submit