Copy and view of NumPy arrays
Copying and viewing of NumPy arrays are two different ways to create new arrays, and there are important differences between them.
copy
Copy creates a new array containing the same elements of the original array, but the two arrays have independent memory space. This means that any changes made to the copy will not affect the original array and vice versa.
To create a copy, you can use the following methods:
()
: Create a new array containing copies of the same elements as the original array.(arr)
: Convert the array to a new NumPy array.arr[:]
: Create a copy of the entire array using tiles.
Example:
import numpy as np arr = ([1, 2, 3, 4, 5]) # Create a copycopy = () # Modify the copycopy[2] = 100 # Print original array and copyprint(arr) print(copy)
Output:
[ 1 2 3 4 5]
[ 1 2 100 4 5]
view
View is a reference to the original array data and does not have independent memory space. This means that any changes made to the view will be reflected directly in the original array and vice versa.
To create a view, you can use the following methods:
()
: Create a new array that is a view of the original array data.arr[start:end]
: Create a view of the original array using tiles.()
: Change the shape of the array, but not the underlying data.
Example:
import numpy as np arr = ([1, 2, 3, 4, 5]) # Create a viewview = () # Modify the viewview[2] = 100 # Print original array and viewprint(arr) print(view)
Output:
[ 1 2 100 4 5]
[ 1 2 100 4 5]
Check if the array has data
We can useProperties to check whether the array has its data. if
for
None
, then the array has its own data, otherwise it is a view.
Example:
import numpy as np arr = ([1, 2, 3, 4, 5]) copy = () view = () print() # None print() # <ndarray object at 0x00000222588287E0>
practise
Create an array using the following codearr
:
import numpy as np arr = ([10, 20, 30, 40, 50])
And create it using the following methodarr
Copy of:
()
(arr)
arr[:]
After each method, print the original array and copy and verify that they are equal.
Share your code and results in the comments.
Sure, here is the requested Markdown formatted content:
Get the shape of the array
The shape of the NumPy array describes how elements in an array are organized and represented by tuples containing the number of elements in each dimension.
Get array shape
AvailableProperties get the shape of the NumPy array. It returns a tuple where each element represents the length of the corresponding dimension.
Example:
import numpy as np # Create a two-dimensional arrayarr = ([[1, 2, 3], [4, 5, 6]]) # Get array shapeprint()
Output:
(2, 3)
This means that the array contains 2 rows and 3 columns.
The meaning of shape tuple
Each element in the shape tuple represents the length of the corresponding dimension. For example, if the shape is(2, 3, 4)
, then the array has:
2 rows 3 columns 4 values per element
Use ndmin to create an array with a specific shape
We can usendmin
Parameters to create a new array with the specified shape, even if the original data does not have that shape.ndmin
The parameter specifies the minimum number of dimensions to create. If the original data has andmin
Higher dimensions, the shape will be retained. If the number of dimensions is insufficient, a new dimension is added and the element is filled with 1.
Example:
import numpy as np # Create a vector with values 1,2,3,4 using ndmin=5arr = ([1, 2, 3, 4], ndmin=5) print(arr) print()
Output:
[[[[1 2 3 4]]]]
(1, 1, 1, 1, 4)
practise
Create NumPy arrays of the following shapes and print their shapes:
A one-dimensional array of 10 elements. A two-dimensional array with 5 rows and 4 columns. A 3D array containing 2 x 3 x 2.
Share your code and output in the comments.
at last
This is the end of this article about the detailed explanation of NumPy array copying and view. For more related contents of NumPy array copying and view, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!