search
Search
Unlock 100+ guides
search toc
close
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
What does this mean?
Why is this true?
Give me some examples!
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
Doc Search
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Shrink
Navigate to

# NumPy | clip method

schedule Aug 12, 2023
Last updated
local_offer
PythonNumPy
Tags
mode_heat
Master the mathematics behind data science with 100+ top-tier guides
Start your free 7-days trial now!

Numpy's `clip(~)` method is used to ensure that the values of the input array are between a particular range.

# Parameters

1. `a` | `array_like`

The input array.

2. `a_min` | `scalar` or `array_like` or `None`

The lower bound to clip. Values that are smaller than `a_min` will be replaced by `a_min`. You can ignore the lower bound by setting `None`.

3. `a_max` | `scalar` or `array_like` or `None`

The upper bound to clip. Values that are larger than `a_max` will be replaced by `a_max`. You can ignore the upper bound by setting `None`.

# Return value

A Numpy array with the values of the input array clipped as per your parameters.

# Examples

## Specifying both lower and upper bound

``` x = np.array([1,2,3,4,5])np.clip(x, 2, 4) array([2, 2, 3, 4, 4]) ```

Notice how the value `1` was clipped up to `2`, while the value `5` is clipped down to `4`.

## Not specifying a lower bound

Set `None` for the second parameter:

``` x = np.array([1,2,3,4,5])np.clip(x, None, 4) array([1, 2, 3, 4, 4]) ```

## Not specifying an upper bound

Set `None` for the third parameter:

``` x = np.array([1,2,3,4,5])np.clip(x, 2, None) array([2, 2, 3, 4, 5]) ```

## Clipping a 2D array

Clipping a multi-dimensional array is slightly tricky. Consider the following example:

``` x = np.array([[1,2],[3,4],[5,6]])np.clip(x, [2,3], [4,5]) array([[2, 3],       [3, 4],       [4, 5]]) ```

Here, the first row `[1,2]` is clipped by the lower bound `[2,3]` - the clipping is done element-wise and so since `1<2` and `2<3`, both values are clipped up to `[2,3]`.

Edited by 0 others
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
0
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!