Skip to content

Commit bdbcb90

Browse files
authored
[Term Entry] Python NumPy - ndarray: view()
* docs: Add NumPy ndarray.view() term entry (#7820) * minor Updates in view.md ---------
1 parent 9301017 commit bdbcb90

File tree

1 file changed

+78
-0
lines changed
  • content/numpy/concepts/ndarray/terms/view

1 file changed

+78
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
---
2+
Title: '.view()'
3+
Description: "Returns a new view of the array's data without copying the underlying memory."
4+
Subjects:
5+
- 'Computer Science'
6+
- 'Data Science'
7+
Tags:
8+
- 'Array'
9+
- 'Data'
10+
- 'NumPy'
11+
CatalogContent:
12+
- 'learn-python-3'
13+
- 'paths/data-science'
14+
---
15+
16+
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.
17+
18+
Creating a view is much faster and more memory-efficient than creating a copy, especially when working with large arrays.
19+
20+
## Syntax
21+
22+
```pseudo
23+
ndarray.view([dtype][, type])
24+
```
25+
26+
**Parameters:**
27+
28+
- `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.
29+
- `type` (type): The desired type for the resulting object (e.g., `np.matrix`).
30+
31+
**Return value:**
32+
33+
Returns a new `ndarray` object that shares the data of the original array.
34+
35+
## Example
36+
37+
In this example, modifying the data in the view (`view_array`) also changes the original array (`original_array`) because both share the same memory:
38+
39+
```py
40+
import numpy as np
41+
42+
# Create the original array
43+
original_array = np.array([1, 2, 3, 4, 5])
44+
45+
# Create a view of the original array
46+
view_array = original_array.view()
47+
48+
print(f"Original Array before modification: {original_array}")
49+
print(f"View Array before modification: {view_array}")
50+
51+
# Modify a single element in the view
52+
view_array[0] = 99
53+
54+
print(f"\nOriginal Array after modifying the view: {original_array}")
55+
```
56+
57+
The output of this code is:
58+
59+
```shell
60+
Original Array before modification: [1 2 3 4 5]
61+
View Array before modification: [1 2 3 4 5]
62+
63+
Original Array after modifying the view: [99 2 3 4 5]
64+
```
65+
66+
## Codebyte Example
67+
68+
In this example, changing the original array also updates its view, since both reference the same data in memory:
69+
70+
```codebyte/python
71+
import numpy as np
72+
original = np.array([10, 20, 30])
73+
data_view = original.view()
74+
# Modify the original array's second element
75+
original[1] = 50
76+
print(f"Original array: {original}")
77+
print(f"View array: {data_view}")
78+
```

0 commit comments

Comments
 (0)