From b070baedfddc43ff811802e26a24ce69fd63a339 Mon Sep 17 00:00:00 2001 From: Rishabh Ranjan Singh Date: Thu, 6 Nov 2025 12:27:26 +0530 Subject: [PATCH 1/2] docs: Add NumPy ndarray.view() term entry (#7820) --- .../numpy/concepts/ndarray/terms/view/view.md | 85 +++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 content/numpy/concepts/ndarray/terms/view/view.md diff --git a/content/numpy/concepts/ndarray/terms/view/view.md b/content/numpy/concepts/ndarray/terms/view/view.md new file mode 100644 index 00000000000..8de07eb6291 --- /dev/null +++ b/content/numpy/concepts/ndarray/terms/view/view.md @@ -0,0 +1,85 @@ +--- +Title: '.view()' +Description: "Creates a new view of an array's data without copying the underlying memory." +Subjects: + - 'Data Science' + - 'Computer Science' +Tags: + - 'NumPy' + - 'Array' + - 'Data View' +CatalogContent: + - 'learn-python-3' + - 'paths/data-science' +--- + +The **`.view()`** method in NumPy creates a new array object that **views the data of the original array**. This means that both the new view and the original array share the exact same underlying memory block. + +Because the data is shared, any modification made to the data in the view will directly affect the data in the original array, and vice-versa. The only thing that changes is the array's metadata (e.g., its data type or shape). + +This is crucial for efficiency, as creating a view is much faster than creating a full copy of a large array. + +## Syntax + +The method is called directly on a NumPy array object. + +```pseudo +array.view(dtype=None, type=None) +``` + +## Parameters + +* `dtype` (data-type): The desired data type for the new array view. Changing the `dtype` does not change the underlying data bytes, only how they are interpreted. +* `type` (type): The desired type for the resulting object (e.g., `np.matrix`). + +## Return Value + +Returns a new `ndarray` object that shares the data of the original array. + +## Example + +This example demonstrates how modifying the data in the new array view (`view_array`) directly changes the data in the original array (`original_array`), because they share memory. + +```python +import numpy as np + +# Create the original array +original_array = np.array([1, 2, 3, 4, 5]) + +# Create a view of the original array +view_array = original_array.view() + +print(f"Original Array before modification: {original_array}") +print(f"View Array before modification: {view_array}") + +# Modify a single element in the view +view_array[0] = 99 + +print(f"\nOriginal Array after modifying the view: {original_array}") +``` + +**Output:** + +```shell +Original Array before modification: [1 2 3 4 5] +View Array before modification: [1 2 3 4 5] + +Original Array after modifying the view: [99 2 3 4 5] +``` + +## Codebyte + +Use the Codebyte below to confirm that if you change a value in the original array, the view also changes. + +```python +import numpy as np + +original = np.array([10, 20, 30]) +data_view = original.view() + +# Modify the original array's second element +original[1] = 50 + +print(f"Original array: {original}") +print(f"View array: {data_view}") +``` \ No newline at end of file From 5327019d797ed4d26b944f3bc9521dacbd48c806 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Fri, 7 Nov 2025 11:30:32 +0530 Subject: [PATCH 2/2] minor Updates in view.md --- .../numpy/concepts/ndarray/terms/view/view.md | 45 ++++++++----------- 1 file changed, 19 insertions(+), 26 deletions(-) diff --git a/content/numpy/concepts/ndarray/terms/view/view.md b/content/numpy/concepts/ndarray/terms/view/view.md index 8de07eb6291..f18d769edc9 100644 --- a/content/numpy/concepts/ndarray/terms/view/view.md +++ b/content/numpy/concepts/ndarray/terms/view/view.md @@ -1,46 +1,42 @@ --- Title: '.view()' -Description: "Creates a new view of an array's data without copying the underlying memory." +Description: "Returns a new view of the array's data without copying the underlying memory." Subjects: - - 'Data Science' - 'Computer Science' + - 'Data Science' Tags: - - 'NumPy' - 'Array' - - 'Data View' + - 'Data' + - 'NumPy' CatalogContent: - 'learn-python-3' - 'paths/data-science' --- -The **`.view()`** method in NumPy creates a new array object that **views the data of the original array**. This means that both the new view and the original array share the exact same underlying memory block. - -Because the data is shared, any modification made to the data in the view will directly affect the data in the original array, and vice-versa. The only thing that changes is the array's metadata (e.g., its data type or shape). +The **`.view()`** method in NumPy returns a new array object that views the same data as the original array. Both arrays share the same underlying memory block, meaning that any modification to the data in one will directly affect the other. Only the array’s metadata (such as data type or shape) may differ. -This is crucial for efficiency, as creating a view is much faster than creating a full copy of a large array. +Creating a view is much faster and more memory-efficient than creating a copy, especially when working with large arrays. ## Syntax -The method is called directly on a NumPy array object. - ```pseudo -array.view(dtype=None, type=None) +ndarray.view([dtype][, type]) ``` -## Parameters +**Parameters:** -* `dtype` (data-type): The desired data type for the new array view. Changing the `dtype` does not change the underlying data bytes, only how they are interpreted. -* `type` (type): The desired type for the resulting object (e.g., `np.matrix`). +- `dtype` (data-type): The desired data type for the new array view. Changing the `dtype` changes how the data bytes are interpreted, not the data itself. +- `type` (type): The desired type for the resulting object (e.g., `np.matrix`). -## Return Value +**Return value:** Returns a new `ndarray` object that shares the data of the original array. ## Example -This example demonstrates how modifying the data in the new array view (`view_array`) directly changes the data in the original array (`original_array`), because they share memory. +In this example, modifying the data in the view (`view_array`) also changes the original array (`original_array`) because both share the same memory: -```python +```py import numpy as np # Create the original array @@ -58,7 +54,7 @@ view_array[0] = 99 print(f"\nOriginal Array after modifying the view: {original_array}") ``` -**Output:** +The output of this code is: ```shell Original Array before modification: [1 2 3 4 5] @@ -67,19 +63,16 @@ View Array before modification: [1 2 3 4 5] Original Array after modifying the view: [99 2 3 4 5] ``` -## Codebyte +## Codebyte Example -Use the Codebyte below to confirm that if you change a value in the original array, the view also changes. +In this example, changing the original array also updates its view, since both reference the same data in memory: -```python +```codebyte/python import numpy as np - original = np.array([10, 20, 30]) data_view = original.view() - # Modify the original array's second element -original[1] = 50 - +original[1] = 50 print(f"Original array: {original}") print(f"View array: {data_view}") -``` \ No newline at end of file +```