-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumpy4.py
38 lines (32 loc) · 1.01 KB
/
numpy4.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#The Difference Between Copy and View
#the copy is a new array, and the view is just a view of the original array.
'''
The copy owns the data and any changes made to the copy will not affect original array, and any changes made to the original array will not affect the copy.
The view does not own the data and any changes made to the view will affect the original array, and any changes made to the original array will affect the view.
'''
import numpy as np
arr = np.array([1,2,3,4,5])
x = arr.copy()
arr[0]=42
print(arr)
print("Copy Method",x)
arr = np.array([1, 2, 3, 4, 5])
x = arr.view()
arr[0] = 42
print(arr)
print("View Method", x)
"""
The copy returns None.
The view returns the original array.
"""
arr = np.array([1,2,3,4,5])
x= arr.copy()
y=arr.view()
print(x.base) #None
print(y.base) #1 2 3 4 5
#Get the Shape of an Array
arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
print(arr.shape) #returns (2,4) , 2 rows each with 4 elements
arr = np.array([1,2,3,4],ndmin=5)
print(arr)
print("Shape of array:",arr.shape)