Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

NumPy · Function

np.clip

Clips array elements to an interval by replacing values below the lower bound or above the upper bound with the corresponding boundary. Bounds may be scalar or array-like and can be broadcast across the input.

How it works

np.clip applies lower and upper bounds element by element. Values below the lower bound become that bound, values above the upper bound become the upper bound, and values already inside remain unchanged; bounds and the input can be broadcast.

Syntax

numpy.clip(a, a_min=<no value>, a_max=<no value>, out=None, *, min=<no value>, max=<no value>, **kwargs)

Parameters

NameTypeDescription
aarray_likeInput array containing the values to constrain.
a_minarray_like, scalar, or NoneLower clipping limit; use None to leave the lower side unclipped.
a_maxarray_like, scalar, or NoneUpper clipping limit; use None to leave the upper side unclipped.
outndarray, optionalOptional destination array for the result; it may be a.
minarray_like, scalar, or NoneAlternative lower bound introduced alongside max; mutually exclusive with a_min.
maxarray_like, scalar, or NoneAlternative upper bound introduced alongside min; mutually exclusive with a_max.
kwargsanyAdditional keyword arguments passed to the underlying implementation.

Returns

clipped_array — ndarray containing a with values below the lower limit replaced by that limit and values above the upper limit replaced by that limit.

Key points

  • Use np.clip to constrain array values to specified limits.
  • The bounds define an inclusive interval; values outside it move to the nearest edge.
  • Its behavior matches nested minimum and maximum operations while providing a dedicated, faster operation.
  • Pass out to store results in a supplied destination, including the input for in-place updates.

Example: Clip values to an interval

import numpy as np

values = np.array([-2, 0.5, 3, 8])
clipped = np.clip(values, 0, 5)
print(clipped)  # [0.  0.5 3.  5. ]

Restricts every element to the inclusive interval from 0 through 5.

When to use it

Enforce numeric limits

Preparing features or measurements for a bounded model input.

Sanitize output

Keeping computed values within a valid display or protocol range.

In-place transformation

Updating an existing ndarray without a separate destination.

Interview questions

QWhat does np.clip do?

np.clip(a, a_min, a_max) replaces values below a_min with a_min and values above a_max with a_max. It is equivalent to applying maximum with the lower bound and then minimum with the upper bound, but the documented function is faster.

Follow-up: What happens when the bounds are arrays? The bounds are broadcast against the input, allowing elementwise limits wherever their shapes are broadcast-compatible.

QHow can clipping be one-sided?

The input a is array-like, while a_min and a_max may be scalars, array-like values, or None. None disables clipping at that side, so at most one bound can be omitted this way.

Follow-up: Can both bounds be omitted? No: the function requires either the older positional bounds or the newer min/max keyword pair to define the operation.

QWhat is the purpose of out?

out receives the result and must have a shape that can hold it. It may be the input array, enabling in-place clipping, and the output type is preserved.

Follow-up: Why use out? It can avoid allocating a separate result when an appropriately shaped writable array is available.

  • numpy.minimum
  • numpy.maximum
  • numpy.clip

Source: numpy.clip. Checked 2026-10-15.