|
| 1 | +--- |
| 2 | +Title: '.clip()' |
| 3 | +Description: 'Limits the values in an array to a specified range.' |
| 4 | +Subjects: |
| 5 | + - 'Computer Science' |
| 6 | + - 'Data Science' |
| 7 | +Tags: |
| 8 | + - 'Arrays' |
| 9 | + - 'Math' |
| 10 | + - 'Methods' |
| 11 | + - 'NumPy' |
| 12 | +CatalogContent: |
| 13 | + - 'learn-python-3' |
| 14 | + - 'paths/computer-science' |
| 15 | +--- |
| 16 | + |
| 17 | +Numpy's **`.clip()`** method limits the values in an [array](https://www.codecademy.com/resources/docs/numpy/ndarray) to a specified range by replacing values below a minimum or above a maximum with those boundary values. |
| 18 | + |
| 19 | +## Syntax |
| 20 | + |
| 21 | +```pseudo |
| 22 | +ndarray.clip(min=None, max=None, out=None) |
| 23 | +``` |
| 24 | + |
| 25 | +**Parameters:** |
| 26 | + |
| 27 | +- `min`: The minimum value to clip array elements to. All values below this will be set to `min`. |
| 28 | +- `max`: The maximum value to clip array elements to. All values above this will be set to `max`. |
| 29 | +- `out`: Output array for storing the result. Must have the same shape as the input array. |
| 30 | + |
| 31 | +**Return value:** |
| 32 | + |
| 33 | +Returns an array in which all values are clipped to the specified range. If `out` is provided, the result is stored in it and a reference to `out` is returned. |
| 34 | + |
| 35 | +## Example 1: Clipping an Array Using `.clip()` |
| 36 | + |
| 37 | +In this example, `.clip()` is used without the `out` parameter to restrict all values of an array to a given range: |
| 38 | + |
| 39 | +```py |
| 40 | +import numpy as np |
| 41 | + |
| 42 | +# Create an array |
| 43 | +np_array = np.array([0, 1, 1, 2, 3, 5, 8, 13, 21]) |
| 44 | + |
| 45 | +# Clip values between 3 and 9 |
| 46 | +clipped_array = np_array.clip(min=3, max=9) |
| 47 | + |
| 48 | +# Print clipped array |
| 49 | +print("Clipped Array: ", clipped_array) |
| 50 | +``` |
| 51 | + |
| 52 | +The output of this code is: |
| 53 | + |
| 54 | +```shell |
| 55 | +Clipped Array: [3 3 3 3 3 5 8 9 9] |
| 56 | +``` |
| 57 | + |
| 58 | +## Example 2: Element-Wise Clipping Using `.clip()` |
| 59 | + |
| 60 | +This example demonstrates using arrays for `min` and `max` to clip values element-wise: |
| 61 | + |
| 62 | +```py |
| 63 | +import numpy as np |
| 64 | + |
| 65 | +np_array = np.array([[[20, -1, 12], [2, -3, 50]]]) |
| 66 | +output_array = np.empty_like(np_array) |
| 67 | + |
| 68 | +min_vals = np.array([[[-1, 4, 7], [10, -13, 16]]]) |
| 69 | +max_vals = np.array([[[2, 5, 11], [13, 17, 19]]]) |
| 70 | + |
| 71 | +np_array.clip(min_vals, max_vals, out=output_array) |
| 72 | + |
| 73 | +print("Clipped Array:\n", output_array) |
| 74 | +``` |
| 75 | + |
| 76 | +The output of this code is: |
| 77 | + |
| 78 | +```shell |
| 79 | +Clipped Array: |
| 80 | + [[[ 2 4 11] |
| 81 | + [10 -3 19]]] |
| 82 | +``` |
| 83 | + |
| 84 | +## Codebyte Example |
| 85 | + |
| 86 | +In this example, `.clip()` is provided with an integer for `min` and an array for `max`: |
| 87 | + |
| 88 | +```codebyte/python |
| 89 | +import numpy as np |
| 90 | +
|
| 91 | +# Create an array of 10 integers |
| 92 | +np_array = np.array([4, 3, 7, -23, 5, 6, 4, 324, -94, 2]) |
| 93 | +print("Array: ", np_array) |
| 94 | +
|
| 95 | +# Provide an integer for min and an array of 10 for max |
| 96 | +clipped_array = np_array.clip(-4, [0, 1, 1, 2, 4, 7, 13, 24, 44, 81]) |
| 97 | +print("Clipped Array: ", clipped_array) |
| 98 | +``` |
0 commit comments