From 33ae992b8fa267f4519c852a1e080c85ffe5ccc8 Mon Sep 17 00:00:00 2001 From: Rupa-Rd Date: Tue, 28 Oct 2025 23:16:49 +0530 Subject: [PATCH 1/5] ndarry term .round() is added --- .../concepts/ndarray/terms/round/round.md | 102 ++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 content/numpy/concepts/ndarray/terms/round/round.md diff --git a/content/numpy/concepts/ndarray/terms/round/round.md b/content/numpy/concepts/ndarray/terms/round/round.md new file mode 100644 index 00000000000..da87404f0b2 --- /dev/null +++ b/content/numpy/concepts/ndarray/terms/round/round.md @@ -0,0 +1,102 @@ +--- +Title: 'round' +Description: 'Converts the numpy array elements into the specific number of decimal places.' +Subjects: + - 'Code Foundations' + - 'Computer Science' + - 'Data Science' +Tags: + - 'NumPy' + - 'Methods' + - 'Arrays' + - 'Elements' +CatalogContent: + - 'learn-python-3' + - 'paths/data-science' + - 'paths/computer-science' +--- + + +The `.round()` method in NumPy serves to convert array elements to the nearest whole number or a specified number of decimal places. When rounding to the nearest whole number, values with a decimal component of 0.5 or greater are rounded up to the next larger whole number. + +## Syntax + +### 1. Round off to the nearest whole number +```pseudo +numpy.round(ndarray) +``` + +#### Parameters +- `ndarray` : Refers to the numpy array + +#### Return value +The function returns a numpy array with float datatype and maintains the same length as input array. + +### 2. Round off to `n` number of decimal places +```pseudo +numpy.round(ndarray, decimals=n) +``` + +#### Parameters +- `ndarray` : Refers to the numpy array +- `decimals` : Refers to the number of decimal places + +#### Return value +The function returns a numpy array with float datatype and maintains the same length as input array. + +## Example 1: Rounding off the numpy arrays into the nearest whole number + +In this example, we round off all the elements of numpy arrays into their nearest whole number. + +```py +import numpy as np + +ndarray = np.array([1.34566, 4.55, 7.788, 2.09, 9.45]) +nearest_whole_number = np.round(ndarray) + +print(nearest_whole_number) +``` + +This produces the following output: + +```shell +[1. 5. 8. 2. 9.] +``` + +## Example 2: Rounding off the numpy arrays into the n decimal places + +In this example, we round off all the elements of numpy arrays into 2 decimal places. + +```py +import numpy as np + +ndarray = np.array([1.34566, 4.55, 7.788, 2.09, 9.45]) +two_decimal_places = np.round(ndarray, decimals=2) + +print(two_decimal_places) +``` + +This produces the following output: + +```shell +[1.35 4.55 7.79 2.09 9.45] +``` + +## Codebyte Example + +Run the following codebyte example to understand the usage of the `.round()` method: + +```codebyte/python +import numpy as np + +ndarray = np.array([1.34566, 4.55, 7.788, 2.09, 9.45]) + +# Round off to the nearest whole number +nearest_whole_number = np.round(ndarray) +print(nearest_whole_number) + +# Round off to 2 decimal places +two_decimal_places = np.round(ndarray, decimals=2) +print(two_decimal_places) + +``` \ No newline at end of file From 005eb07c895abced0de66dcdcd5e2142d8aba854 Mon Sep 17 00:00:00 2001 From: Rupa-Rd Date: Tue, 28 Oct 2025 23:35:50 +0530 Subject: [PATCH 2/5] Formatted `round.md` using yarn format --- .../concepts/ndarray/terms/round/round.md | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/content/numpy/concepts/ndarray/terms/round/round.md b/content/numpy/concepts/ndarray/terms/round/round.md index da87404f0b2..7822b296a8b 100644 --- a/content/numpy/concepts/ndarray/terms/round/round.md +++ b/content/numpy/concepts/ndarray/terms/round/round.md @@ -1,47 +1,52 @@ --- -Title: 'round' +Title: 'round' Description: 'Converts the numpy array elements into the specific number of decimal places.' -Subjects: +Subjects: - 'Code Foundations' - 'Computer Science' - 'Data Science' -Tags: +Tags: - 'NumPy' - 'Methods' - 'Arrays' - 'Elements' -CatalogContent: +CatalogContent: - 'learn-python-3' - 'paths/data-science' - 'paths/computer-science' --- - The `.round()` method in NumPy serves to convert array elements to the nearest whole number or a specified number of decimal places. When rounding to the nearest whole number, values with a decimal component of 0.5 or greater are rounded up to the next larger whole number. ## Syntax ### 1. Round off to the nearest whole number + ```pseudo numpy.round(ndarray) ``` #### Parameters + - `ndarray` : Refers to the numpy array #### Return value + The function returns a numpy array with float datatype and maintains the same length as input array. ### 2. Round off to `n` number of decimal places + ```pseudo numpy.round(ndarray, decimals=n) ``` #### Parameters + - `ndarray` : Refers to the numpy array -- `decimals` : Refers to the number of decimal places +- `decimals` : Refers to the number of decimal places #### Return value + The function returns a numpy array with float datatype and maintains the same length as input array. ## Example 1: Rounding off the numpy arrays into the nearest whole number @@ -99,4 +104,4 @@ print(nearest_whole_number) two_decimal_places = np.round(ndarray, decimals=2) print(two_decimal_places) -``` \ No newline at end of file +``` From b13a16cc326eb2e37a5456d14bb141568ac55fb7 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Wed, 29 Oct 2025 12:11:49 +0530 Subject: [PATCH 3/5] Revise round() method documentation in NumPy Updated the documentation for the round() method in NumPy to clarify its functionality and parameters. --- .../concepts/ndarray/terms/round/round.md | 57 +++++++------------ 1 file changed, 19 insertions(+), 38 deletions(-) diff --git a/content/numpy/concepts/ndarray/terms/round/round.md b/content/numpy/concepts/ndarray/terms/round/round.md index 7822b296a8b..8493524f25e 100644 --- a/content/numpy/concepts/ndarray/terms/round/round.md +++ b/content/numpy/concepts/ndarray/terms/round/round.md @@ -1,65 +1,47 @@ --- -Title: 'round' -Description: 'Converts the numpy array elements into the specific number of decimal places.' +Title: 'round()' +Description: 'Returns a new array with each element rounded to the specified number of decimals.' Subjects: - 'Code Foundations' - 'Computer Science' - - 'Data Science' Tags: - - 'NumPy' - - 'Methods' - 'Arrays' - 'Elements' + - 'Methods' + - 'NumPy' CatalogContent: - 'learn-python-3' - 'paths/data-science' - - 'paths/computer-science' --- -The `.round()` method in NumPy serves to convert array elements to the nearest whole number or a specified number of decimal places. When rounding to the nearest whole number, values with a decimal component of 0.5 or greater are rounded up to the next larger whole number. +The **`.round()`** method in NumPy rounds each element of an array to the nearest integer or to a specified number of decimal places. Values exactly halfway between two numbers are rounded to the nearest even value (also known as banker’s rounding). ## Syntax -### 1. Round off to the nearest whole number - ```pseudo -numpy.round(ndarray) +ndarray.round(decimals=0, out=None) ``` -#### Parameters +**Parameters:** -- `ndarray` : Refers to the numpy array +- `decimals` (int, optional): Number of decimal places to round to. Default is 0. Can be negative to round to the left of the decimal point. +- `out` (ndarray, optional): An alternative output array where results are stored. Must be the same shape as the input. -#### Return value +**Return value:** -The function returns a numpy array with float datatype and maintains the same length as input array. - -### 2. Round off to `n` number of decimal places - -```pseudo -numpy.round(ndarray, decimals=n) -``` +Returns an array with elements rounded to the specified number of decimals. If `out` is provided, the result is stored in that array, and a reference to it is returned. -#### Parameters +## Example 1: Round to the nearest whole number -- `ndarray` : Refers to the numpy array -- `decimals` : Refers to the number of decimal places - -#### Return value - -The function returns a numpy array with float datatype and maintains the same length as input array. - -## Example 1: Rounding off the numpy arrays into the nearest whole number - -In this example, we round off all the elements of numpy arrays into their nearest whole number. +This example rounds all elements in a NumPy array to the nearest integer: ```py import numpy as np -ndarray = np.array([1.34566, 4.55, 7.788, 2.09, 9.45]) -nearest_whole_number = np.round(ndarray) +arr = np.array([1.34566, 4.55, 7.788, 2.09, 9.45]) +rounded_arr = arr.round() -print(nearest_whole_number) +print(rounded_arr) ``` This produces the following output: @@ -68,9 +50,9 @@ This produces the following output: [1. 5. 8. 2. 9.] ``` -## Example 2: Rounding off the numpy arrays into the n decimal places +## Example 2: Round to specific decimal places -In this example, we round off all the elements of numpy arrays into 2 decimal places. +This example rounds all elements in a NumPy array to two decimal places: ```py import numpy as np @@ -89,7 +71,7 @@ This produces the following output: ## Codebyte Example -Run the following codebyte example to understand the usage of the `.round()` method: +This example shows how to round NumPy array elements to various decimal places using `.round()`: ```codebyte/python import numpy as np @@ -103,5 +85,4 @@ print(nearest_whole_number) # Round off to 2 decimal places two_decimal_places = np.round(ndarray, decimals=2) print(two_decimal_places) - ``` From 00bf3355d7dc3c6e8d71ab5a629bc57fbe83ce3a Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Wed, 29 Oct 2025 12:13:15 +0530 Subject: [PATCH 4/5] Enhance .round() method description with ndarray link Updated the description of the .round() method to include a link to the ndarray documentation. --- content/numpy/concepts/ndarray/terms/round/round.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/numpy/concepts/ndarray/terms/round/round.md b/content/numpy/concepts/ndarray/terms/round/round.md index 8493524f25e..68c9ef06c89 100644 --- a/content/numpy/concepts/ndarray/terms/round/round.md +++ b/content/numpy/concepts/ndarray/terms/round/round.md @@ -14,7 +14,7 @@ CatalogContent: - 'paths/data-science' --- -The **`.round()`** method in NumPy rounds each element of an array to the nearest integer or to a specified number of decimal places. Values exactly halfway between two numbers are rounded to the nearest even value (also known as banker’s rounding). +The **`.round()`** method in NumPy rounds each element of an [array](https://www.codecademy.com/resources/docs/numpy/ndarray) to the nearest integer or to a specified number of decimal places. Values exactly halfway between two numbers are rounded to the nearest even value (also known as banker’s rounding). ## Syntax From 86fa5da155cdc5cb1b79b65abd0669228c55569d Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Wed, 29 Oct 2025 12:13:36 +0530 Subject: [PATCH 5/5] Fix formatting for round() method documentation --- content/numpy/concepts/ndarray/terms/round/round.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/content/numpy/concepts/ndarray/terms/round/round.md b/content/numpy/concepts/ndarray/terms/round/round.md index 68c9ef06c89..a5c0964c6de 100644 --- a/content/numpy/concepts/ndarray/terms/round/round.md +++ b/content/numpy/concepts/ndarray/terms/round/round.md @@ -14,7 +14,7 @@ CatalogContent: - 'paths/data-science' --- -The **`.round()`** method in NumPy rounds each element of an [array](https://www.codecademy.com/resources/docs/numpy/ndarray) to the nearest integer or to a specified number of decimal places. Values exactly halfway between two numbers are rounded to the nearest even value (also known as banker’s rounding). +The **`round()`** method in NumPy rounds each element of an [array](https://www.codecademy.com/resources/docs/numpy/ndarray) to the nearest integer or to a specified number of decimal places. Values exactly halfway between two numbers are rounded to the nearest even value (also known as banker’s rounding). ## Syntax @@ -71,7 +71,7 @@ This produces the following output: ## Codebyte Example -This example shows how to round NumPy array elements to various decimal places using `.round()`: +This example shows how to round NumPy array elements to various decimal places using `round()`: ```codebyte/python import numpy as np