From fa6b2e1e2fa64ba4e6ff40ddf8c5a8d061c7f5c6 Mon Sep 17 00:00:00 2001 From: Riya Dey Date: Tue, 28 Oct 2025 10:00:13 +0530 Subject: [PATCH 1/3] docs: Add term entry for NumPy ndarray.copy() --- .../numpy/concepts/ndarray/terms/copy/copy.md | 92 +++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 content/numpy/concepts/ndarray/terms/copy/copy.md diff --git a/content/numpy/concepts/ndarray/terms/copy/copy.md b/content/numpy/concepts/ndarray/terms/copy/copy.md new file mode 100644 index 00000000000..9130eed8ad7 --- /dev/null +++ b/content/numpy/concepts/ndarray/terms/copy/copy.md @@ -0,0 +1,92 @@ +--- +Title: 'NumPy ndarray - copy()' +Description: 'Creates a new, independent copy of a NumPy ndarray.' +Subjects: # Note: Verify/Update these based on Codecademy/docs/documentation/subjects.md + - 'NumPy' + - 'Python' + - 'Data Science' +Tags: # Note: Verify/Update these based on Codecademy/docs/documentation/tags.md + - 'Arrays' + - 'Methods' + - 'Copying' +CatalogContent: # Optional: Add relevant Codecademy course/path slugs if known +# - 'learn-numpy' +# - 'paths/data-science' +--- + +Creates a new, independent copy of a NumPy array (`ndarray`). Unlike simple assignment (which creates a *view* sharing the same underlying data), the **`copy()`** method ensures that modifications to the new array do not affect the original array, and vice versa. This is essential when you need to manipulate an array while preserving the original data. + +## Syntax + +```py +new_array = original_array.copy(order='C') +``` +**Parameters** +- `order` : {'C', 'F', 'A', 'K'}, optional Controls the memory layout of the copy. + + - `'C'` (default): C-style (row-major) order. + + - `'F'`: Fortran-style (column-major) order. + + - `'A'`: Any order (Fortran if original_array is Fortran contiguous, C otherwise). + + - `'K'`: Keep the memory layout of original_array. + +**Return Value** +- Returns a new `ndarray` object which is a copy of the original array. + +## Example +The following example demonstrates the difference between assigning an array (creating a view) and using copy() to create an independent copy. + +```py +import numpy as np + +# Original array +original = np.array([10, 20, 30]) +print(f"Original array: {original}") + +# Assignment creates a view (deep copy, shares data) +view = original +view[0] = 111 # Modify the view +print(f"Original after modifying view: {original}") +# Output shows original is changed! +# Reset original for clarity +original = np.array([10, 20, 30]) + +# copy() creates an independent copy +copied_array = original.copy() +copied_array[0] = 999 +# Modify the copy +print(f"Original after modifying copy: {original}") +# Output shows original is unchanged! +print(f"Copied array: {copied_array}") +``` +Output of the above example: + +```shell +Original array: [10 20 30] +Original after modifying view: [111 20 30] +Original after modifying copy: [10 20 30] +Copied array: [999 20 30] +``` + +## Codebyte Example: Copying a 2D array using copy() +This example shows copy() in action with a 2D array. It modifies the copy and leaves the original untouched, as a Deep Copy. + +```codebyte/python +import numpy as np + +# Create the original 2D array +original_arr = np.arange(4).reshape((2, 2)) +print(f"Original array:\n{original_arr}") + +# Create an independent copy +copied_arr = original_arr.copy() + +# Modify an element in the copy +copied_arr[0, 1] = 50 +print(f"\nCopied array after modification:\n{copied_arr}") + +# Verify the original array is unaffected +print(f"\nOriginal array (still unchanged):\n{original_arr}") +``` \ No newline at end of file From 07f33811c77a99489d5d169c76dd09b393c5388b Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Tue, 28 Oct 2025 11:58:46 +0530 Subject: [PATCH 2/3] Revise copy() documentation for clarity and accuracy Updated metadata and improved clarity in the description and examples for the copy() method in NumPy. --- .../numpy/concepts/ndarray/terms/copy/copy.md | 66 +++++++++---------- 1 file changed, 32 insertions(+), 34 deletions(-) diff --git a/content/numpy/concepts/ndarray/terms/copy/copy.md b/content/numpy/concepts/ndarray/terms/copy/copy.md index 9130eed8ad7..96a3e69e5d5 100644 --- a/content/numpy/concepts/ndarray/terms/copy/copy.md +++ b/content/numpy/concepts/ndarray/terms/copy/copy.md @@ -1,43 +1,41 @@ --- -Title: 'NumPy ndarray - copy()' -Description: 'Creates a new, independent copy of a NumPy ndarray.' -Subjects: # Note: Verify/Update these based on Codecademy/docs/documentation/subjects.md - - 'NumPy' - - 'Python' +Title: 'copy()' +Description: 'Creates and returns a new, independent copy of a NumPy ndarray.' +Subjects: + - 'Computer Science' - 'Data Science' -Tags: # Note: Verify/Update these based on Codecademy/docs/documentation/tags.md +Tags: - 'Arrays' - 'Methods' - - 'Copying' -CatalogContent: # Optional: Add relevant Codecademy course/path slugs if known -# - 'learn-numpy' -# - 'paths/data-science' + - 'NumPy' +CatalogContent: + - 'learn-python-3' + - 'paths/computer-science' --- -Creates a new, independent copy of a NumPy array (`ndarray`). Unlike simple assignment (which creates a *view* sharing the same underlying data), the **`copy()`** method ensures that modifications to the new array do not affect the original array, and vice versa. This is essential when you need to manipulate an array while preserving the original data. +The **`copy()`** method in NumPy creates a new, independent copy of an array (`ndarray`). Unlike simple assignment, which creates a view that shares the same underlying data, it ensures that changes to the new array do not affect the original, and vice versa. This is useful when you need to modify an array while preserving the original data. ## Syntax -```py -new_array = original_array.copy(order='C') +```pseudo +ndarray.copy(order='C') ``` -**Parameters** -- `order` : {'C', 'F', 'A', 'K'}, optional Controls the memory layout of the copy. - - - `'C'` (default): C-style (row-major) order. - - `'F'`: Fortran-style (column-major) order. +**Parameters** - - `'A'`: Any order (Fortran if original_array is Fortran contiguous, C otherwise). +- `order` *(optional, str)*: Controls the memory layout of the copy. + - `'C'` (default): C-style (row-major) order. + - `'F'`: Fortran-style (column-major) order. + - `'A'`: Preserves the array’s order — Fortran if the original is Fortran-contiguous, otherwise C. + - `'K'`: Keeps the order as closely as possible to the original. - - `'K'`: Keep the memory layout of original_array. +**Return value:** -**Return Value** -- Returns a new `ndarray` object which is a copy of the original array. +Returns a new `ndarray` object that is an independent copy of the original array. ## Example -The following example demonstrates the difference between assigning an array (creating a view) and using copy() to create an independent copy. +The following example demonstrates the difference between assignment (which shares data) and using `copy()` (which creates an independent copy): ```py import numpy as np @@ -45,22 +43,21 @@ import numpy as np original = np.array([10, 20, 30]) print(f"Original array: {original}") -# Assignment creates a view (deep copy, shares data) +# Assignment creates a reference (shares data) view = original -view[0] = 111 # Modify the view -print(f"Original after modifying view: {original}") -# Output shows original is changed! +view[0] = 111 +print(f"Original after modifying view: {original}") + # Reset original for clarity original = np.array([10, 20, 30]) # copy() creates an independent copy copied_array = original.copy() -copied_array[0] = 999 -# Modify the copy -print(f"Original after modifying copy: {original}") -# Output shows original is unchanged! +copied_array[0] = 999 +print(f"Original after modifying copy: {original}") print(f"Copied array: {copied_array}") ``` + Output of the above example: ```shell @@ -70,8 +67,9 @@ Original after modifying copy: [10 20 30] Copied array: [999 20 30] ``` -## Codebyte Example: Copying a 2D array using copy() -This example shows copy() in action with a 2D array. It modifies the copy and leaves the original untouched, as a Deep Copy. +## Codebyte Example: Copying a 2D array using `copy()` + +This example shows how `copy()` works with a 2D array. It modifies the copy while leaving the original unchanged: ```codebyte/python import numpy as np @@ -89,4 +87,4 @@ print(f"\nCopied array after modification:\n{copied_arr}") # Verify the original array is unaffected print(f"\nOriginal array (still unchanged):\n{original_arr}") -``` \ No newline at end of file +``` From 80c591843d4d3af321e1510ba6487f16f0425cd6 Mon Sep 17 00:00:00 2001 From: Sriparno Roy Date: Fri, 31 Oct 2025 11:52:49 +0530 Subject: [PATCH 3/3] Minor changes --- .../numpy/concepts/ndarray/terms/copy/copy.md | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/content/numpy/concepts/ndarray/terms/copy/copy.md b/content/numpy/concepts/ndarray/terms/copy/copy.md index 96a3e69e5d5..b271d0068cd 100644 --- a/content/numpy/concepts/ndarray/terms/copy/copy.md +++ b/content/numpy/concepts/ndarray/terms/copy/copy.md @@ -1,9 +1,9 @@ --- -Title: 'copy()' +Title: '.copy()' Description: 'Creates and returns a new, independent copy of a NumPy ndarray.' Subjects: - 'Computer Science' - - 'Data Science' + - 'Data Science' Tags: - 'Arrays' - 'Methods' @@ -13,7 +13,7 @@ CatalogContent: - 'paths/computer-science' --- -The **`copy()`** method in NumPy creates a new, independent copy of an array (`ndarray`). Unlike simple assignment, which creates a view that shares the same underlying data, it ensures that changes to the new array do not affect the original, and vice versa. This is useful when you need to modify an array while preserving the original data. +The **`.copy()`** method in NumPy creates a new, independent copy of an array (`ndarray`). Unlike simple assignment, which creates a view that shares the same underlying data, it ensures that changes to the new array do not affect the original, and vice versa. This is useful when you need to modify an array while preserving the original data. ## Syntax @@ -21,9 +21,9 @@ The **`copy()`** method in NumPy creates a new, independent copy of an array (`n ndarray.copy(order='C') ``` -**Parameters** +**Parameters:** -- `order` *(optional, str)*: Controls the memory layout of the copy. +- `order` (optional, `str`): Controls the memory layout of the copy. - `'C'` (default): C-style (row-major) order. - `'F'`: Fortran-style (column-major) order. - `'A'`: Preserves the array’s order — Fortran if the original is Fortran-contiguous, otherwise C. @@ -33,9 +33,10 @@ ndarray.copy(order='C') Returns a new `ndarray` object that is an independent copy of the original array. -## Example +## Example: Basic Usage of `.copy()` + +The following example demonstrates the difference between assignment (which shares data) and using `.copy()` (which creates an independent copy): -The following example demonstrates the difference between assignment (which shares data) and using `copy()` (which creates an independent copy): ```py import numpy as np @@ -67,9 +68,9 @@ Original after modifying copy: [10 20 30] Copied array: [999 20 30] ``` -## Codebyte Example: Copying a 2D array using `copy()` +## Codebyte Example: Copying a 2D Array Using `.copy()` -This example shows how `copy()` works with a 2D array. It modifies the copy while leaving the original unchanged: +This example shows how `.copy()` works with a 2D array. It modifies the copy while leaving the original unchanged: ```codebyte/python import numpy as np