From ce8159bb8cc7650efb91d1ab27245633a8966624 Mon Sep 17 00:00:00 2001 From: cmaxreilly Date: Tue, 26 Nov 2024 23:44:21 -0700 Subject: [PATCH 01/14] Adding/fixing C printf format specifiers. --- content/c/concepts/basic-output/terms/printf/printf.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/content/c/concepts/basic-output/terms/printf/printf.md b/content/c/concepts/basic-output/terms/printf/printf.md index 6ce6614c586..3f20c2419cb 100644 --- a/content/c/concepts/basic-output/terms/printf/printf.md +++ b/content/c/concepts/basic-output/terms/printf/printf.md @@ -49,10 +49,11 @@ As seen above, the string given contains a `%` character followed by a letter to | Format Specifier(s) | Type(s) | | ------------------- | --------------- | | `%c` | char | -| `%f` or `%g` | float or double | +| `%s` | string | | `%d` or `%i` | int | +| `%f` or `%g` | float | +| `%lf` | double | | `%p` | pointer | -| `%s` | string | ## Decimal Precision From 160855488cc79e7c050efe64da72bc88935ab99d Mon Sep 17 00:00:00 2001 From: cmaxreilly Date: Mon, 2 Dec 2024 11:16:59 -0700 Subject: [PATCH 02/14] Added files for square function in numpy module. Added metadata. --- .../concepts/math-methods/terms/square/square.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 content/numpy/concepts/math-methods/terms/square/square.md diff --git a/content/numpy/concepts/math-methods/terms/square/square.md b/content/numpy/concepts/math-methods/terms/square/square.md new file mode 100644 index 00000000000..ff08ba1c9eb --- /dev/null +++ b/content/numpy/concepts/math-methods/terms/square/square.md @@ -0,0 +1,14 @@ +--- +Title: '.square()' +Description: 'Calculates the square of each element in an array.' +Subjects: + - 'Computer Science' + - 'Data Science' +Tags: + - 'Arrays' + - 'Functions' + - 'NumPy' +CatalogContent: + - 'learn-python-3' + - 'paths/computer-science' +--- From 972816a7926e8ad0545e0063b3b1bc27b734031d Mon Sep 17 00:00:00 2001 From: cmaxreilly Date: Mon, 2 Dec 2024 19:12:09 -0700 Subject: [PATCH 03/14] First draft of .square information page. --- .../math-methods/terms/square/square.md | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/content/numpy/concepts/math-methods/terms/square/square.md b/content/numpy/concepts/math-methods/terms/square/square.md index ff08ba1c9eb..2f92177e33c 100644 --- a/content/numpy/concepts/math-methods/terms/square/square.md +++ b/content/numpy/concepts/math-methods/terms/square/square.md @@ -4,6 +4,7 @@ Description: 'Calculates the square of each element in an array.' Subjects: - 'Computer Science' - 'Data Science' + - 'Discrete Math' Tags: - 'Arrays' - 'Functions' @@ -12,3 +13,62 @@ CatalogContent: - 'learn-python-3' - 'paths/computer-science' --- + + +In NumPy, the **`.square()`** method is used to calculate the square of a number or the square of +the elements of an array. It is employed in mathematical computations common in machine learning, +data analysis, engineering, and graphics. + +## General Syntax +```python +numpy.square(array, out = None, where = True, dtype = None) +``` +- `array`: Input array. Can be a number, an array, or a multidimensional array. Required. +- `out`: An optional array where the result will be stored. Optional. +- `where`: Used for conditional replacements of the elements in the output array. Must be a numpy + array. Optional. +- `dtype`: The datatype of the output array. Optional. + +## Modifying the output array +The output array cannot simply be a python array, since python arrays are lists of pointers to +objects. Numpy makes use of arrays composed of contiguous blocks of memory (like in C or Fortran) +for the purposes of optimization. Therefore, arrays supplied for this argument must be initialized +with the numpy.array function like so... + +```python +output_array = numpy.array([0, 0, 0, 0, 0]) +``` +You can now call your array as the `out` parameter in your .square() function. +```python +array = [1, 2, 3, 4, 5] +numpy.square(array, out = output_array) +print(output_array) +# Outputs [1, 4, 9, 16, 25] +``` + +## Operating conditionally +With the use of the "where" parameter, the function will execute conditionally. For instance... + +```python +import numpy as np +array = [1, 2, 3, 4, 5] +conditions = np.array([False, True, True, False, True]) +np.square(array, where=conditions) +# Outputs 'array([1, 4, 9, 4, 25])' +``` + +The "where" parameter takes a boolean value (True, False, 1, 0, etc.) or a matrix of boolean values, +and squares values at matrix indexes that correspond with true values and vice versa. If the "where" +parameter is set equal to a single boolean value, the entire input array is either squared (if it is +'1' or 'True') or not suqared (if it is '0' or 'False). + +## Changing types +Sometimes it is important to increase or decrease the size of the datatype of the output array. This +can be done by setting the 'dtype' parameter to an np datatype. Like so... + +```python +import numpy as np +array = [1, 2, 3, 4, 5] +np.square(array, dtype=np.float32) +# Outputs array([ 1., 4., 9., 16., 25.], dtype=float32) +``` From c73161a61de378e939d7b53deb3db8b45f59658c Mon Sep 17 00:00:00 2001 From: Max Reilly <104339073+cmaxreilly@users.noreply.github.com> Date: Tue, 3 Dec 2024 13:11:50 -0700 Subject: [PATCH 04/14] Revert printf.md --- content/c/concepts/basic-output/terms/printf/printf.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/content/c/concepts/basic-output/terms/printf/printf.md b/content/c/concepts/basic-output/terms/printf/printf.md index 3f20c2419cb..6ce6614c586 100644 --- a/content/c/concepts/basic-output/terms/printf/printf.md +++ b/content/c/concepts/basic-output/terms/printf/printf.md @@ -49,11 +49,10 @@ As seen above, the string given contains a `%` character followed by a letter to | Format Specifier(s) | Type(s) | | ------------------- | --------------- | | `%c` | char | -| `%s` | string | +| `%f` or `%g` | float or double | | `%d` or `%i` | int | -| `%f` or `%g` | float | -| `%lf` | double | | `%p` | pointer | +| `%s` | string | ## Decimal Precision From c3fe5d23b03d5944c778840205be75c0e05c2ce1 Mon Sep 17 00:00:00 2001 From: Max Reilly <104339073+cmaxreilly@users.noreply.github.com> Date: Tue, 3 Dec 2024 13:12:45 -0700 Subject: [PATCH 05/14] Update content/numpy/concepts/math-methods/terms/square/square.md Changed line lengths to match. Co-authored-by: Daksha Deep --- content/numpy/concepts/math-methods/terms/square/square.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/content/numpy/concepts/math-methods/terms/square/square.md b/content/numpy/concepts/math-methods/terms/square/square.md index 2f92177e33c..f25da0484d9 100644 --- a/content/numpy/concepts/math-methods/terms/square/square.md +++ b/content/numpy/concepts/math-methods/terms/square/square.md @@ -15,9 +15,7 @@ CatalogContent: --- -In NumPy, the **`.square()`** method is used to calculate the square of a number or the square of -the elements of an array. It is employed in mathematical computations common in machine learning, -data analysis, engineering, and graphics. +In NumPy, the **`.square()`** method computes the square of a number or the square of the elements in an array. It is commonly used in mathematical calculations in machine learning, data analysis, engineering, and graphics. ## General Syntax ```python From 14d4086599676c4f7abc372f21cef9af89670c7e Mon Sep 17 00:00:00 2001 From: Max Reilly <104339073+cmaxreilly@users.noreply.github.com> Date: Tue, 3 Dec 2024 13:13:19 -0700 Subject: [PATCH 06/14] Fixed Header /square/square.md Co-authored-by: Daksha Deep --- content/numpy/concepts/math-methods/terms/square/square.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/numpy/concepts/math-methods/terms/square/square.md b/content/numpy/concepts/math-methods/terms/square/square.md index f25da0484d9..536ccede6cc 100644 --- a/content/numpy/concepts/math-methods/terms/square/square.md +++ b/content/numpy/concepts/math-methods/terms/square/square.md @@ -17,7 +17,7 @@ CatalogContent: In NumPy, the **`.square()`** method computes the square of a number or the square of the elements in an array. It is commonly used in mathematical calculations in machine learning, data analysis, engineering, and graphics. -## General Syntax +## Syntax ```python numpy.square(array, out = None, where = True, dtype = None) ``` From f907960885877edbd800da0930514c4ca59c9b77 Mon Sep 17 00:00:00 2001 From: Max Reilly <104339073+cmaxreilly@users.noreply.github.com> Date: Tue, 3 Dec 2024 13:19:26 -0700 Subject: [PATCH 07/14] Added links and inline syntax blocks to /square/square.md Co-authored-by: Daksha Deep --- content/numpy/concepts/math-methods/terms/square/square.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/content/numpy/concepts/math-methods/terms/square/square.md b/content/numpy/concepts/math-methods/terms/square/square.md index 536ccede6cc..d3f5d45c42d 100644 --- a/content/numpy/concepts/math-methods/terms/square/square.md +++ b/content/numpy/concepts/math-methods/terms/square/square.md @@ -28,10 +28,11 @@ numpy.square(array, out = None, where = True, dtype = None) - `dtype`: The datatype of the output array. Optional. ## Modifying the output array -The output array cannot simply be a python array, since python arrays are lists of pointers to -objects. Numpy makes use of arrays composed of contiguous blocks of memory (like in C or Fortran) + +The output array cannot simply be a Python array since Python arrays are [lists](https://www.codecademy.com/resources/docs/python/built-in-functions/list) of pointers to +objects. Numpy makes use of arrays composed of contiguous blocks of memory (like in C) for the purposes of optimization. Therefore, arrays supplied for this argument must be initialized -with the numpy.array function like so... +with the `numpy.array` function like so... ```python output_array = numpy.array([0, 0, 0, 0, 0]) From 1599599bb137f534f858b85e48828170821765df Mon Sep 17 00:00:00 2001 From: Max Reilly <104339073+cmaxreilly@users.noreply.github.com> Date: Tue, 3 Dec 2024 13:20:20 -0700 Subject: [PATCH 08/14] Substituted colon for elipsis /square/square.md Co-authored-by: Daksha Deep --- content/numpy/concepts/math-methods/terms/square/square.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/numpy/concepts/math-methods/terms/square/square.md b/content/numpy/concepts/math-methods/terms/square/square.md index d3f5d45c42d..32af0d60391 100644 --- a/content/numpy/concepts/math-methods/terms/square/square.md +++ b/content/numpy/concepts/math-methods/terms/square/square.md @@ -46,7 +46,7 @@ print(output_array) ``` ## Operating conditionally -With the use of the "where" parameter, the function will execute conditionally. For instance... +With the use of the "where" parameter, the function will execute conditionally. For instance: ```python import numpy as np From 9d2120c0bd83e1e219784395d22dc5fdc27771e4 Mon Sep 17 00:00:00 2001 From: Max Reilly <104339073+cmaxreilly@users.noreply.github.com> Date: Tue, 3 Dec 2024 13:21:25 -0700 Subject: [PATCH 09/14] Fixed line lengths and colons /square/square.md Co-authored-by: Daksha Deep --- content/numpy/concepts/math-methods/terms/square/square.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/content/numpy/concepts/math-methods/terms/square/square.md b/content/numpy/concepts/math-methods/terms/square/square.md index 32af0d60391..a79557f2da7 100644 --- a/content/numpy/concepts/math-methods/terms/square/square.md +++ b/content/numpy/concepts/math-methods/terms/square/square.md @@ -62,8 +62,7 @@ parameter is set equal to a single boolean value, the entire input array is eith '1' or 'True') or not suqared (if it is '0' or 'False). ## Changing types -Sometimes it is important to increase or decrease the size of the datatype of the output array. This -can be done by setting the 'dtype' parameter to an np datatype. Like so... +Sometimes, it is important to increase or decrease the size of the datatype of the output array. This can be done by setting the 'dtype' parameter to an np datatype, like: ```python import numpy as np From 4ffb192e69c62184215ebd6aa584cba04c0cad0f Mon Sep 17 00:00:00 2001 From: Max Reilly <104339073+cmaxreilly@users.noreply.github.com> Date: Tue, 3 Dec 2024 20:43:33 +0000 Subject: [PATCH 10/14] Fixed formatting errors in square.md --- .../math-methods/terms/square/square.md | 26 ++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/content/numpy/concepts/math-methods/terms/square/square.md b/content/numpy/concepts/math-methods/terms/square/square.md index a79557f2da7..ef1c60d24d8 100644 --- a/content/numpy/concepts/math-methods/terms/square/square.md +++ b/content/numpy/concepts/math-methods/terms/square/square.md @@ -14,13 +14,14 @@ CatalogContent: - 'paths/computer-science' --- - In NumPy, the **`.square()`** method computes the square of a number or the square of the elements in an array. It is commonly used in mathematical calculations in machine learning, data analysis, engineering, and graphics. ## Syntax + ```python numpy.square(array, out = None, where = True, dtype = None) ``` + - `array`: Input array. Can be a number, an array, or a multidimensional array. Required. - `out`: An optional array where the result will be stored. Optional. - `where`: Used for conditional replacements of the elements in the output array. Must be a numpy @@ -37,15 +38,21 @@ with the `numpy.array` function like so... ```python output_array = numpy.array([0, 0, 0, 0, 0]) ``` -You can now call your array as the `out` parameter in your .square() function. + +This array can now be called as the `out` parameter in your .square() function. + ```python array = [1, 2, 3, 4, 5] numpy.square(array, out = output_array) print(output_array) -# Outputs [1, 4, 9, 16, 25] +``` + +```shell +[1, 4, 9, 16, 25] ``` ## Operating conditionally + With the use of the "where" parameter, the function will execute conditionally. For instance: ```python @@ -53,20 +60,27 @@ import numpy as np array = [1, 2, 3, 4, 5] conditions = np.array([False, True, True, False, True]) np.square(array, where=conditions) -# Outputs 'array([1, 4, 9, 4, 25])' +``` + +```shell +array([1, 4, 9, 4, 25]) ``` The "where" parameter takes a boolean value (True, False, 1, 0, etc.) or a matrix of boolean values, and squares values at matrix indexes that correspond with true values and vice versa. If the "where" parameter is set equal to a single boolean value, the entire input array is either squared (if it is -'1' or 'True') or not suqared (if it is '0' or 'False). +'1' or 'True') or not squared (if it is '0' or 'False). ## Changing types + Sometimes, it is important to increase or decrease the size of the datatype of the output array. This can be done by setting the 'dtype' parameter to an np datatype, like: ```python import numpy as np array = [1, 2, 3, 4, 5] np.square(array, dtype=np.float32) -# Outputs array([ 1., 4., 9., 16., 25.], dtype=float32) +``` + +```shell +array([ 1., 4., 9., 16., 25.], dtype=float32) ``` From 2b990df3abb7f2d38839ec9d802b53f60391a53a Mon Sep 17 00:00:00 2001 From: Max Reilly <104339073+cmaxreilly@users.noreply.github.com> Date: Tue, 17 Dec 2024 18:42:56 +0000 Subject: [PATCH 11/14] Fixed syntax labels. --- .../concepts/math-methods/terms/square/square.md | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/content/numpy/concepts/math-methods/terms/square/square.md b/content/numpy/concepts/math-methods/terms/square/square.md index ef1c60d24d8..35ac7af1195 100644 --- a/content/numpy/concepts/math-methods/terms/square/square.md +++ b/content/numpy/concepts/math-methods/terms/square/square.md @@ -18,7 +18,7 @@ In NumPy, the **`.square()`** method computes the square of a number or the squa ## Syntax -```python +```pseudo numpy.square(array, out = None, where = True, dtype = None) ``` @@ -35,18 +35,20 @@ objects. Numpy makes use of arrays composed of contiguous blocks of memory (like for the purposes of optimization. Therefore, arrays supplied for this argument must be initialized with the `numpy.array` function like so... -```python +```pseudo output_array = numpy.array([0, 0, 0, 0, 0]) ``` This array can now be called as the `out` parameter in your .square() function. -```python +```pseudo array = [1, 2, 3, 4, 5] numpy.square(array, out = output_array) print(output_array) ``` +Output + ```shell [1, 4, 9, 16, 25] ``` @@ -55,13 +57,15 @@ print(output_array) With the use of the "where" parameter, the function will execute conditionally. For instance: -```python +```pseudo import numpy as np array = [1, 2, 3, 4, 5] conditions = np.array([False, True, True, False, True]) np.square(array, where=conditions) ``` +Output + ```shell array([1, 4, 9, 4, 25]) ``` @@ -75,12 +79,14 @@ parameter is set equal to a single boolean value, the entire input array is eith Sometimes, it is important to increase or decrease the size of the datatype of the output array. This can be done by setting the 'dtype' parameter to an np datatype, like: -```python +```pseudo import numpy as np array = [1, 2, 3, 4, 5] np.square(array, dtype=np.float32) ``` +Output + ```shell array([ 1., 4., 9., 16., 25.], dtype=float32) ``` From 1a05b77a124c3f78f0b1d321b6ce00cb4bf88d34 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Thu, 19 Dec 2024 16:44:18 +0530 Subject: [PATCH 12/14] minor changes --- .../math-methods/terms/square/square.md | 29 ++++++++----------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/content/numpy/concepts/math-methods/terms/square/square.md b/content/numpy/concepts/math-methods/terms/square/square.md index 35ac7af1195..9b5d102ae0e 100644 --- a/content/numpy/concepts/math-methods/terms/square/square.md +++ b/content/numpy/concepts/math-methods/terms/square/square.md @@ -22,26 +22,24 @@ In NumPy, the **`.square()`** method computes the square of a number or the squa numpy.square(array, out = None, where = True, dtype = None) ``` -- `array`: Input array. Can be a number, an array, or a multidimensional array. Required. -- `out`: An optional array where the result will be stored. Optional. -- `where`: Used for conditional replacements of the elements in the output array. Must be a numpy - array. Optional. +- `array`: Input array, it can be a number, an array, or a multidimensional array. Required. +- `out`: An optional array storing the result. Optional. +- `where`: (Optional) Used for conditional replacements of the elements in the output array. It must be a numpy + array. - `dtype`: The datatype of the output array. Optional. ## Modifying the output array The output array cannot simply be a Python array since Python arrays are [lists](https://www.codecademy.com/resources/docs/python/built-in-functions/list) of pointers to -objects. Numpy makes use of arrays composed of contiguous blocks of memory (like in C) -for the purposes of optimization. Therefore, arrays supplied for this argument must be initialized -with the `numpy.array` function like so... +objects. Numpy makes use of arrays composed of contiguous blocks of memory (like in C) for the purposes of optimization. Therefore, arrays supplied for this argument must be initialized with the `numpy.array` function like so... -```pseudo +```py output_array = numpy.array([0, 0, 0, 0, 0]) ``` -This array can now be called as the `out` parameter in your .square() function. +This array can now be called the `out` parameter in your .square() function. -```pseudo +```py array = [1, 2, 3, 4, 5] numpy.square(array, out = output_array) print(output_array) @@ -55,9 +53,9 @@ Output ## Operating conditionally -With the use of the "where" parameter, the function will execute conditionally. For instance: +Using the "where" parameter, the function will execute conditionally. For instance: -```pseudo +```py import numpy as np array = [1, 2, 3, 4, 5] conditions = np.array([False, True, True, False, True]) @@ -70,16 +68,13 @@ Output array([1, 4, 9, 4, 25]) ``` -The "where" parameter takes a boolean value (True, False, 1, 0, etc.) or a matrix of boolean values, -and squares values at matrix indexes that correspond with true values and vice versa. If the "where" -parameter is set equal to a single boolean value, the entire input array is either squared (if it is -'1' or 'True') or not squared (if it is '0' or 'False). +The "where" parameter takes a boolean value (True, False, 1, 0, etc.) or a matrix of boolean values and squares values at matrix indexes that correspond with true values and vice versa. If the "where" parameter is set equal to a single boolean value, the entire input array is either squared (if it is '1' or 'True') or not squared (if it is '0' or 'False). ## Changing types Sometimes, it is important to increase or decrease the size of the datatype of the output array. This can be done by setting the 'dtype' parameter to an np datatype, like: -```pseudo +```py import numpy as np array = [1, 2, 3, 4, 5] np.square(array, dtype=np.float32) From f4d2bce25b1eeb4672c07a6f07f08b4ae86ed4d0 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Thu, 19 Dec 2024 16:44:33 +0530 Subject: [PATCH 13/14] Update square.md --- content/numpy/concepts/math-methods/terms/square/square.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/numpy/concepts/math-methods/terms/square/square.md b/content/numpy/concepts/math-methods/terms/square/square.md index 9b5d102ae0e..a2a02d5e5be 100644 --- a/content/numpy/concepts/math-methods/terms/square/square.md +++ b/content/numpy/concepts/math-methods/terms/square/square.md @@ -14,7 +14,7 @@ CatalogContent: - 'paths/computer-science' --- -In NumPy, the **`.square()`** method computes the square of a number or the square of the elements in an array. It is commonly used in mathematical calculations in machine learning, data analysis, engineering, and graphics. +In NumPy, the **`.square()`** method computes the square of a number or the square of the elements in an array. It is commonly used in mathematical calculations, machine learning, data analysis, engineering, and graphics. ## Syntax From d2bf5cc882d57bc6e31f8cc7edeeaf3310d54520 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Fri, 20 Dec 2024 19:58:34 +0530 Subject: [PATCH 14/14] Update square.md fixed --- .../math-methods/terms/square/square.md | 88 ++++++++++++++----- 1 file changed, 64 insertions(+), 24 deletions(-) diff --git a/content/numpy/concepts/math-methods/terms/square/square.md b/content/numpy/concepts/math-methods/terms/square/square.md index a2a02d5e5be..75e6f655f02 100644 --- a/content/numpy/concepts/math-methods/terms/square/square.md +++ b/content/numpy/concepts/math-methods/terms/square/square.md @@ -19,69 +19,109 @@ In NumPy, the **`.square()`** method computes the square of a number or the squa ## Syntax ```pseudo -numpy.square(array, out = None, where = True, dtype = None) +numpy.square(x, out = None, where = True, dtype = None) ``` -- `array`: Input array, it can be a number, an array, or a multidimensional array. Required. -- `out`: An optional array storing the result. Optional. -- `where`: (Optional) Used for conditional replacements of the elements in the output array. It must be a numpy - array. -- `dtype`: The datatype of the output array. Optional. +- `x`: The input data, which can be a number, an array, or a multidimensional array. +- `out` (Optional): A location where the result is stored. If provided, it must have the same shape as the expected output. +- `where` (Optional): A boolean array specifying which elements to compute. The result is only computed for elements where `where` is `True`. +- `dtype` (Optional): The desired data type for the output array. If not specified, it defaults to the data type of x. -## Modifying the output array +## Examples -The output array cannot simply be a Python array since Python arrays are [lists](https://www.codecademy.com/resources/docs/python/built-in-functions/list) of pointers to -objects. Numpy makes use of arrays composed of contiguous blocks of memory (like in C) for the purposes of optimization. Therefore, arrays supplied for this argument must be initialized with the `numpy.array` function like so... +### Modifying the output array + +The output array for NumPy operations cannot be a Python [list](https://www.codecademy.com/resources/docs/python/built-in-functions/list) because lists are not optimized for numerical computations. NumPy arrays are composed of contiguous blocks of memory, which enhances performance. Therefore, the array passed for the out parameter must be a NumPy array initialized with the `numpy.array` function: ```py -output_array = numpy.array([0, 0, 0, 0, 0]) +import numpy as np + +output_array = np.array([0, 0, 0, 0, 0]) ``` -This array can now be called the `out` parameter in your .square() function. +This array can then be used as the `out` parameter in the `numpy.square()` function: ```py +import numpy as np + +output_array = np.array([0, 0, 0, 0, 0]) + array = [1, 2, 3, 4, 5] -numpy.square(array, out = output_array) +np.square(array, out = output_array) print(output_array) ``` -Output +This generates the output as follows: ```shell [1, 4, 9, 16, 25] ``` -## Operating conditionally +### Operating conditionally -Using the "where" parameter, the function will execute conditionally. For instance: +Using the `where` parameter, the function will execute conditionally. The `where` parameter specifies where to apply the operation, based on a condition. If the condition is `True` at a particular index, the corresponding element in the array will be squared. If the condition is `False`, the element will remain unchanged. For instance: ```py import numpy as np -array = [1, 2, 3, 4, 5] + +array = np.array([1, 2, 3, 4, 5]) conditions = np.array([False, True, True, False, True]) -np.square(array, where=conditions) + +result = np.square(array, where=conditions) +print(result) ``` -Output +Output: ```shell array([1, 4, 9, 4, 25]) ``` -The "where" parameter takes a boolean value (True, False, 1, 0, etc.) or a matrix of boolean values and squares values at matrix indexes that correspond with true values and vice versa. If the "where" parameter is set equal to a single boolean value, the entire input array is either squared (if it is '1' or 'True') or not squared (if it is '0' or 'False). +The `where` parameter takes a boolean array or condition. It determines where the squaring operation will take place: -## Changing types +- True at an index: The element at that index will be squared. +- False at an index: The element at that index will remain unchanged. -Sometimes, it is important to increase or decrease the size of the datatype of the output array. This can be done by setting the 'dtype' parameter to an np datatype, like: +If the `where` parameter is set to a single boolean value (either `True` or `False`), the entire array is either squared (if `True`) or left unchanged (if `False`). + +### Changing types + +Sometimes, it is important to increase or decrease the size of the datatype of the output array. This can be done by setting the `dtype` parameter to an np datatype, like: ```py import numpy as np -array = [1, 2, 3, 4, 5] -np.square(array, dtype=np.float32) +array = np.array([1, 2, 3, 4, 5]) # Ensuring it's a numpy array +result = np.square(array, dtype=np.float32) + +# Print the result +print(result) ``` -Output +Output generated will be as follows: ```shell array([ 1., 4., 9., 16., 25.], dtype=float32) ``` + +## Codebyte Example + +Run the following example to understand how the `.square()` method works: + +```codebyte/python +import numpy as np + +# Create a NumPy array +array = np.array([1, 2, 3, 4, 5]) + +# Create an output array initialized with zeros +output_array = np.zeros_like(array) + +# Set the condition for the 'where' parameter (square values where condition is True) +conditions = np.array([False, True, True, False, True]) + +# Use numpy.square() with all parameters +result = np.square(array, out=output_array, where=conditions) + +# Print the result +print("Squared values with conditions:", result) +```