26.numpy 2
import numpy as np
## Copy Vs View
arr = np.array([1,2,3,4,5])
myarray = arr.copy()
# myarray[0] = 42
arr[0] = 10
print('arr:', arr)
print('myarray:', myarray)
arr = np.array([1,2,3,4,5])
myarray = arr.view()
myarray[0] = 40
arr[0] = 10
print('arr:', arr)
print('myarray:', myarray)
print(myarray.base)
print(arr.base)
arr = np.array([1,2,3,4,5])
myarray = arr.copy()
print(myarray.base)
## Shape
arr = np.array(10)
print(arr.shape)
arr1 = np.array([1,2,3,4,5])
print(arr1.shape)
arr2 = np.array([[1,2],[3,5]])
print(arr2.shape)
arr3 = np.array([[[1,2,3],[4,5,6]], [[1,2,3],[4,5,6]]])
print(arr3.shape)
arrn = np.array([1,2,3], ndmin=5)
print(arrn)
print(arrn.shape)
## reshape
arr = np.array([[[1,2],[4,5]], [[1,2],[4,5]]])
print(arr)
arr.reshape(2,4)
arr.reshape(3,4)
myarray = arr.reshape(8)
print(myarray)
print(myarray.base)
arr.reshape(2,2,2)
arr.reshape(2,-1)
arr.reshape(-1,2,2) ### (x * 2 * 2 = 8 => x = 2)
arr.reshape(-1)
## iterating arrays
myarray = np.array([1,2,3])
myarray2 = np.array([[1,2,3],[4,5,6]])
myarray3 = np.array([[[1,2,3],[4,5,6]],[[1,2,3],[4,5,6]]])
for x in myarray:
print(x)
for x in myarray2:
print(x)
for x in myarray2:
for y in x:
print(y)
for x in myarray3:
for y in x:
print(y)
for x in myarray3:
for y in x:
for z in y:
print(z)
for x in np.nditer(myarray3):
print(x)
print(myarray2)
for x in np.nditer(myarray2[ 1: , ::2]):
print(x)
for idx, x in np.ndenumerate(myarray2):
print(idx, x)
for idx, x in np.ndenumerate(myarray):
print(idx, x)
print(myarray3)
for x in np.nditer(myarray3[:1,1:,::2]):
print(x)
for idx, x in np.ndenumerate(myarray3):
print(idx, x)