From 5069426ebd4e800dd0b7c6ead7e105d136a70c11 Mon Sep 17 00:00:00 2001 From: John Moore <5482498+johnmoore84@users.noreply.github.com> Date: Thu, 30 Oct 2025 19:08:44 -0700 Subject: [PATCH 1/3] [Term Entry] Python NumPy - ndarray: clip() --- .../numpy/concepts/ndarray/terms/clip/clip.md | 103 ++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 content/numpy/concepts/ndarray/terms/clip/clip.md diff --git a/content/numpy/concepts/ndarray/terms/clip/clip.md b/content/numpy/concepts/ndarray/terms/clip/clip.md new file mode 100644 index 00000000000..422ed42b3c4 --- /dev/null +++ b/content/numpy/concepts/ndarray/terms/clip/clip.md @@ -0,0 +1,103 @@ +--- +Title: '.clip()' +Description: 'Limits ndarray values to a specified range.' +Subjects: + - 'Computer Science' + - 'Data Science' +Tags: + - 'Arrays' + - 'Math' + - 'Methods' + - 'NumPy' +CatalogContent: + - 'learn-python-3' + - 'paths/computer-science' +--- + +Numpy's **`.clip()`** is used to limit the values in an array to a specified range by replacing values outside the range with the minimum or maximum specified values. + +## Syntax + +```pseudo +ndarray.clip(min=None, max=None, out=None) +``` + +**Parameters:** + +- `min`: The minimum value to clip array elements to. All values below this will be set to `min`. +- `max`: The maximum value to clip array elements to. All values above this will be set to `max`. +- `out`: Output array for storing the result. Must have the same shape as the input array. + +**Return value:** + +Returns an array in which all values are clipped to the specified range. If provided, the result is stored in the output array. + +## Example 1: Clipping an Array Using `.clip()` + +In this example, `.clip()` is used without the `out` parameter to restrict all values of an array to a given range: + +```py +import numpy as np + +# Create an array +np_array = np.array([0, 1, 1, 2, 3, 5, 8, 13, 21]) + +# Clip values between 3 and 9 +clipped_array = np_array.clip(min=3, max=9) + +# Print clipped array +print("Clipped Array: ", clipped_array) +``` + +The output of this code is: + +```shell +Clipped Array: [3 3 3 3 3 5 8 9 9] +``` + +## Example 2: Element-Wise Clipping Using `.clip()` + +In this example, `.clip()` is used to clip each element of an array by using an array for `min` and `max`: + +```py +import numpy as np + +# Create a 2D array +np_array = np.array([[[20, -1, 12], [2, -3, 50]]]) + +# Create an output array +output_array = np.empty_like(np_array) + +# Create element-wise min and max arrays +min_vals = ([[[-1, 4, 7], [10, -13, 16]]]) +max_vals = ([[[2, 5, 11], [13, 17, 19]]]) + +clipped_array = np_array.clip(min_vals, max_vals, output_array) + +# Print the output array +print("Clipped Array: \n", output_array) +``` + +The output of this code is: + +```shell +Clipped Array: + [[[ 2 4 11] + [10 -3 19]]] +``` + +## Codebyte Example + +In this example, `.clip()` is provided with an integer for `min` and an array for `max`: + +```codebyte/python +import numpy as np + +# Create an array of 10 integers +np_array = np.array([4, 3, 7, -23, 5, 6, 4, 324, -94, 2]) +print("Array: ", np_array) + +# Provide an integer for min and an array of 10 for max +clipped_array = np_array.clip(-4, [0, 1, 1, 2, 4, 7, 13, 24, 44, 81]) +print("Clipped Array: ", clipped_array) +``` From caf7aceeb76550fb069ed2e16cf49851d64e3d2b Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Fri, 31 Oct 2025 22:15:08 +0530 Subject: [PATCH 2/3] minor changes --- content/numpy/concepts/ndarray/terms/clip/clip.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/numpy/concepts/ndarray/terms/clip/clip.md b/content/numpy/concepts/ndarray/terms/clip/clip.md index 422ed42b3c4..445518c6a19 100644 --- a/content/numpy/concepts/ndarray/terms/clip/clip.md +++ b/content/numpy/concepts/ndarray/terms/clip/clip.md @@ -14,7 +14,7 @@ CatalogContent: - 'paths/computer-science' --- -Numpy's **`.clip()`** is used to limit the values in an array to a specified range by replacing values outside the range with the minimum or maximum specified values. +Numpy's **`.clip()`** is used to limit the values in an [array](https://www.codecademy.com/resources/docs/numpy/ndarray) to a specified range by replacing values outside the range with the minimum or maximum specified values. ## Syntax From 0b773e0084e42bda732efe8de9210c45c21f6cc4 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Sat, 1 Nov 2025 17:01:16 +0530 Subject: [PATCH 3/3] minor content fixes --- .../numpy/concepts/ndarray/terms/clip/clip.md | 21 +++++++------------ 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/content/numpy/concepts/ndarray/terms/clip/clip.md b/content/numpy/concepts/ndarray/terms/clip/clip.md index 445518c6a19..b91c0c6d750 100644 --- a/content/numpy/concepts/ndarray/terms/clip/clip.md +++ b/content/numpy/concepts/ndarray/terms/clip/clip.md @@ -1,6 +1,6 @@ --- Title: '.clip()' -Description: 'Limits ndarray values to a specified range.' +Description: 'Limits the values in an array to a specified range.' Subjects: - 'Computer Science' - 'Data Science' @@ -14,7 +14,7 @@ CatalogContent: - 'paths/computer-science' --- -Numpy's **`.clip()`** is used to limit the values in an [array](https://www.codecademy.com/resources/docs/numpy/ndarray) to a specified range by replacing values outside the range with the minimum or maximum specified values. +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. ## Syntax @@ -30,7 +30,7 @@ ndarray.clip(min=None, max=None, out=None) **Return value:** -Returns an array in which all values are clipped to the specified range. If provided, the result is stored in the output array. +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. ## Example 1: Clipping an Array Using `.clip()` @@ -57,25 +57,20 @@ Clipped Array: [3 3 3 3 3 5 8 9 9] ## Example 2: Element-Wise Clipping Using `.clip()` -In this example, `.clip()` is used to clip each element of an array by using an array for `min` and `max`: +This example demonstrates using arrays for `min` and `max` to clip values element-wise: ```py import numpy as np -# Create a 2D array np_array = np.array([[[20, -1, 12], [2, -3, 50]]]) - -# Create an output array output_array = np.empty_like(np_array) -# Create element-wise min and max arrays -min_vals = ([[[-1, 4, 7], [10, -13, 16]]]) -max_vals = ([[[2, 5, 11], [13, 17, 19]]]) +min_vals = np.array([[[-1, 4, 7], [10, -13, 16]]]) +max_vals = np.array([[[2, 5, 11], [13, 17, 19]]]) -clipped_array = np_array.clip(min_vals, max_vals, output_array) +np_array.clip(min_vals, max_vals, out=output_array) -# Print the output array -print("Clipped Array: \n", output_array) +print("Clipped Array:\n", output_array) ``` The output of this code is: