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

Back to Questions

63. Removing constant features

easy
GeneralGeneral
senior

Remove constant (zero-variance) features during feature engineering so your model doesn’t waste capacity on columns that never change. You’ll scan a 2D dataset and drop any feature whose values are identical across all samples.

Constant-feature detection can be defined as:

var(xj)=1n∑i=1n(xij−μj)2,drop feature j if var(xj)=0\text{var}(x_j) = \frac{1}{n}\sum_{i=1}^{n}(x_{ij} - \mu_j)^2,\quad \text{drop feature } j \text{ if } \text{var}(x_j)=0

Requirements

Implement the function

python

Rules:

  • A feature (column) is constant if all its values are exactly the same across all rows.
  • Return the filtered dataset as a NumPy array (same row order), plus kept_indices.
  • Do not use prebuilt feature-selection utilities (e.g., from sklearn).
  • Use NumPy vectorized operations.
  • Keep the scope simple: compute constant-ness directly from the input (no streaming, no file I/O).

Example

python

Output:

python
Input Signature
ArgumentType
Xnp.ndarray
Output Signature
Return NameType
valuetuple

Constraints

  • Input/output are NumPy arrays (indices can be list/array).

  • Use NumPy only; no sklearn utilities.

  • Check exact equality; no tolerance needed.

Hint 1

Operate column-wise via axis=0.

Hint 2

A column is constant if all values equal its first-row value: np.all(X == X[0], axis=0).

Hint 3

Use the boolean mask to build kept_indices, then slice: X_filtered = X[:, kept_indices]; return (X_filtered, kept_indices).

Roles
ML Engineer
AI Engineer
Companies
GeneralGeneral
Levels
senior
entry
Tags
numpy
feature-selection
data-preprocessing
zero-variance
25 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