From 7241af6b30a7133569aeae5bef81f6e099f6f1e5 Mon Sep 17 00:00:00 2001 From: ParthShikhare19 Date: Fri, 24 Oct 2025 17:16:57 +0530 Subject: [PATCH 1/3] added view() in md file --- .../numpy/concepts/ndarray/terms/view/view.md | 99 +++++++++++++++++++ 1 file changed, 99 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..9fada9fa865 --- /dev/null +++ b/content/numpy/concepts/ndarray/terms/view/view.md @@ -0,0 +1,99 @@ +--- +Title: '.view()' +Description: 'Return a new view of the array (a shallow view that shares the original array\'s data buffer).' +Subjects: + - 'Data Science' +Tags: + - 'NumPy' + - 'ndarray' + - 'methods' +CatalogContent: + - 'learn-python-3' +--- + +The **`.view()`** method returns a new array object that looks at the same data as the original array. The returned object is a *view* (a shallow reference) — it shares the same underlying memory buffer as the original `ndarray`. Because the two arrays share memory, modifying the contents of one will generally affect the other. + +## Syntax + +```python +ndarray.view([dtype=None][, type=None]) +``` + +Parameters: + +- `dtype` (optional): A data-type to force the returned view to use. If provided, the underlying data will be reinterpreted with the specified dtype — this can change how the bytes are interpreted and may change the shape required to represent the data. +- `type` (optional): If present, the returned object will be a subclass of `ndarray` (for example, `numpy.matrix`) instead of a plain `ndarray`. + +Return value: A new array object (an `ndarray` or an instance of the specified `type`) that shares the same data buffer as the original unless the interpretation prevents sharing. + +Important notes: + +- `.view()` creates a shallow view — it does not copy array data. Use `.copy()` when you need an independent copy. +- Reinterpreting dtype with `.view(dtype=...)` does not change the bytes — it only changes how they are interpreted. + +## Example + +Basic example showing that the view shares data with the original array: + +```python +import numpy as np + +# create an array and a view +arr = np.arange(6).reshape(2, 3) +v = arr.view() + +print('Original arr:') +print(arr) + +# modify the view +v[0, 0] = 99 + +print('\nModified view v:') +print(v) + +print('\nOriginal arr after modifying v (shares memory):') +print(arr) +``` + +This demonstrates that modifying `v` also modifies `arr`, because `v` is a view into the same buffer. + +## Codebyte + +```codebyte/python +import numpy as np + +# 1) Shared-data view example +arr = np.arange(6, dtype=np.int64).reshape(2, 3) +v = arr.view() +print('arr before:', arr, sep='\n') + +v[0, 0] = -1 +print('\nview v after modification:', v, sep='\n') +print('\narr after view modification (same buffer):', arr, sep='\n') + +# 2) Dtype reinterpretation example (byte-wise view) +a = np.array([1, 256], dtype=np.int16) +print('\noriginal a:', a, a.dtype) + +# view underlying bytes as unsigned 8-bit integers +b = a.view(np.uint8) +print('byte-wise view b:', b, b.dtype) + +# 3) Creating an ndarray subclass via the `type` argument (rare) +mat = arr.view(type=np.matrix) +print('\nmatrix view (type=np.matrix):', mat, type(mat)) + +# If you want an independent copy, use .copy() +c = arr.copy() +c[0, 0] = 555 +print('\narr after making a copy and modifying the copy (arr unchanged):', arr, sep='\n') +print('\ncopy c:', c, sep='\n') +``` + +The codebyte shows three common uses of `.view()`: + +- creating a shallow view that shares memory (fast, no copy), +- reinterpreting the raw bytes using a different `dtype`, and +- creating a different array *type* by passing the `type` argument. + +Refer to the NumPy docs for more details and edge cases: https://numpy.org/doc/stable/reference/generated/numpy.ndarray.view.html From 36bde7037e52e6073d3357144a437a69b64d7848 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Sat, 25 Oct 2025 14:16:48 +0530 Subject: [PATCH 2/3] Revise .view() method documentation in NumPy Updated the description and examples for the .view() method in NumPy documentation. Improved clarity and corrected formatting issues. --- .../numpy/concepts/ndarray/terms/view/view.md | 85 +++++++++++-------- 1 file changed, 50 insertions(+), 35 deletions(-) diff --git a/content/numpy/concepts/ndarray/terms/view/view.md b/content/numpy/concepts/ndarray/terms/view/view.md index 9fada9fa865..e958c3f490d 100644 --- a/content/numpy/concepts/ndarray/terms/view/view.md +++ b/content/numpy/concepts/ndarray/terms/view/view.md @@ -1,41 +1,45 @@ --- Title: '.view()' -Description: 'Return a new view of the array (a shallow view that shares the original array\'s data buffer).' +Description: 'Return a new view of the array (a shallow view) that shares the original array\'s data buffer.' Subjects: + - 'Computer Science' - 'Data Science' Tags: + - 'Arrays' + - 'Methods' - 'NumPy' - - 'ndarray' - - 'methods' + - 'Objects' CatalogContent: - 'learn-python-3' + - 'paths/computer-science' --- -The **`.view()`** method returns a new array object that looks at the same data as the original array. The returned object is a *view* (a shallow reference) — it shares the same underlying memory buffer as the original `ndarray`. Because the two arrays share memory, modifying the contents of one will generally affect the other. +The **`.view()`** method returns a new array object that looks at the same data as the original array. The returned object is a _view_ ((shallow reference) that shares the same memory buffer. Modifying the contents of the view generally affects the original array. + +The method also allows reinterpreting the data with a different `dtype` or creating a subclass of `ndarray` using the `type` parameter. If a completely independent copy is needed, `.copy()` should be used instead. ## Syntax -```python +```pseudo ndarray.view([dtype=None][, type=None]) ``` -Parameters: +**Parameters:** -- `dtype` (optional): A data-type to force the returned view to use. If provided, the underlying data will be reinterpreted with the specified dtype — this can change how the bytes are interpreted and may change the shape required to represent the data. -- `type` (optional): If present, the returned object will be a subclass of `ndarray` (for example, `numpy.matrix`) instead of a plain `ndarray`. +- `dtype` (optional): Data type to force the returned view to use. Reinterprets the underlying bytes with the specified dtype; this may affect shape requirements. +- `type` (optional): If provided, the returned object will be a subclass of `ndarray` (e.g., `numpy.matrix`) instead of a plain `ndarray`. -Return value: A new array object (an `ndarray` or an instance of the specified `type`) that shares the same data buffer as the original unless the interpretation prevents sharing. +**Return value:** -Important notes: +Returns a new array object (an `ndarray` or an instance of the specified `type`) that shares the same data buffer as the original unless the interpretation prevents sharing. -- `.view()` creates a shallow view — it does not copy array data. Use `.copy()` when you need an independent copy. -- Reinterpreting dtype with `.view(dtype=...)` does not change the bytes — it only changes how they are interpreted. +> **Note:** `.view()` creates a shallow view — it does not copy array data. Use `.copy()` to create an independent array. ## Example -Basic example showing that the view shares data with the original array: +In this example, a view of a NumPy array is created that shares the original array’s memory, so modifications to the view also affect the original array: -```python +```py import numpy as np # create an array and a view @@ -55,45 +59,56 @@ print('\nOriginal arr after modifying v (shares memory):') print(arr) ``` +The output of this code is: + +```shell +Original arr: +[[0 1 2] + [3 4 5]] + +Modified view v: +[[99 1 2] + [ 3 4 5]] + +Original arr after modifying v (shares memory): +[[99 1 2] + [ 3 4 5]] +``` + This demonstrates that modifying `v` also modifies `arr`, because `v` is a view into the same buffer. -## Codebyte +## Codebyte Example + +In this example, the array’s underlying bytes are reinterpreted with a different data type using `.view(dtype=...)` without copying the data: ```codebyte/python import numpy as np -# 1) Shared-data view example +# 1) Shallow view sharing memory arr = np.arange(6, dtype=np.int64).reshape(2, 3) v = arr.view() -print('arr before:', arr, sep='\n') - v[0, 0] = -1 -print('\nview v after modification:', v, sep='\n') -print('\narr after view modification (same buffer):', arr, sep='\n') +print('arr after modifying view v:', arr, sep='\n') -# 2) Dtype reinterpretation example (byte-wise view) +# 2) Reinterpreting bytes with a different dtype a = np.array([1, 256], dtype=np.int16) -print('\noriginal a:', a, a.dtype) - -# view underlying bytes as unsigned 8-bit integers b = a.view(np.uint8) -print('byte-wise view b:', b, b.dtype) +print('\nOriginal a:', a, a.dtype) +print('Byte-wise view b:', b, b.dtype) -# 3) Creating an ndarray subclass via the `type` argument (rare) +# 3) Creating an ndarray subclass using type mat = arr.view(type=np.matrix) -print('\nmatrix view (type=np.matrix):', mat, type(mat)) +print('\nMatrix view (type=np.matrix):', mat, type(mat)) -# If you want an independent copy, use .copy() +# 4) Independent copy c = arr.copy() c[0, 0] = 555 -print('\narr after making a copy and modifying the copy (arr unchanged):', arr, sep='\n') -print('\ncopy c:', c, sep='\n') +print('\nOriginal arr after modifying copy c:', arr, sep='\n') +print('Copy c:', c, sep='\n') ``` The codebyte shows three common uses of `.view()`: -- creating a shallow view that shares memory (fast, no copy), -- reinterpreting the raw bytes using a different `dtype`, and -- creating a different array *type* by passing the `type` argument. - -Refer to the NumPy docs for more details and edge cases: https://numpy.org/doc/stable/reference/generated/numpy.ndarray.view.html +- Creating a shallow view that shares memory (fast, no copy), +- Reinterpreting the raw bytes using a different `dtype`, and +- Creating a different array _type_ by passing the `type` argument. From 4acd47c27f14df1ae91b4e895a8308e5621f81a1 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Sat, 25 Oct 2025 14:18:17 +0530 Subject: [PATCH 3/3] Update description of .view() method Corrected description grammar and wording for clarity. --- content/numpy/concepts/ndarray/terms/view/view.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/numpy/concepts/ndarray/terms/view/view.md b/content/numpy/concepts/ndarray/terms/view/view.md index e958c3f490d..e8d94b4f351 100644 --- a/content/numpy/concepts/ndarray/terms/view/view.md +++ b/content/numpy/concepts/ndarray/terms/view/view.md @@ -1,6 +1,6 @@ --- Title: '.view()' -Description: 'Return a new view of the array (a shallow view) that shares the original array\'s data buffer.' +Description: 'Returns a new view of the array (shallow reference) that shares the original array’s data buffer.' Subjects: - 'Computer Science' - 'Data Science'