import numpy as np np.random.seed(0) # Creating a one dimensional array. one = np.random.randint(10, size = 6) print(one) # Creating a two dimensional array. two = np.random.randint(10, size = (2, 5)) print(two) # Creating a three dimensional array. three = np.random.randint(10, size = (3, 4, 5)) print(three) # Using the attributes to determine the dimension of the arrays. print("one ==>", one.ndim ) print("two ==>", two.ndim ) print("three ==>", three.ndim ) print("one ==>", one.shape) print("two ==>", two.shape) print("three ==>", three.shape) # Using the attribute to determine the size of the arrays. print("one ==>", one.size) print("two ==>", two.size) print("three ==>", three.size) # Using the attribute to determine the datatype of the arrays. print("one ==>", one.dtype) print("two ==>", two.dtype) print("three ==>", three.dtype) # Using the attribute to determine the item size of the arrays. print("one ==>", one.itemsize) print("two ==>", two.itemsize) print("three ==>", three.itemsize) print("one ==>", one.nbytes) print("two ==>", two.nbytes) print("three ==>", three.nbytes) # Array Indexing: Accessing Single Elements oneValue = one[2] print(oneValue) # Accessing the elements in a multi dimensional array print(two) twoValue = two[0, 4] print(twoValue) # Changing the values of the array print(one) one[3] = 6 print(one) one[4] = 4.56 # It will be silently truncated print(one) # Array Slicing: Accessing Subarrays # The syntax of the slicing is ==> x[start:stop:step] ==> default value is: # [0: size of the dimension: 1] a = np.random.randint(10, size = (10)) print(a) print("------------------------") print(a[4: 9: 2]) print("------------------------") print(a[1::2]) print("------------------------") print(a[::2]) print("------------------------") print(a[:9:]) print("------------------------") # This is the best example for reversing an array print(a[::-1]) print("------------------------") # I tries on strings and it worked !!! a = "University of Regina is awesome !!!" print(a) print("------------------------") print(a[::-1]) print(two) print("------------------------") twoValue = two[:2, :3] print(twoValue) print("------------------------") twoValue = two[:2, ::3] print(twoValue) print("------------------------") twoValue = two[::-1, ::-1] print(twoValue) # Accessing array rows and columns. print(two) print("------------------------") print(two[:, 0]) # first coloumn print("------------------------") print(two[0, :]) # first row print("------------------------") print(two[0]) # Entire row # Modifing the array without using copy() function. print(two) print("------------------------") twoValue = two[:2, :2] print(twoValue) print("------------------------") twoValue[0, 0] = 25 print(twoValue) print("------------------------") print(two) # Modifing the array with using copy() function. print(two) print("------------------------") twoValue = two[:2, :2].copy() print(twoValue) print("------------------------") twoValue[0, 0] = 45 print(twoValue) print("------------------------") print(two) # Reshaping of Arrays using reshape() in a more advanced way grid = np.arange(1, 10).reshape(3, 3) print(grid) # Reshaping the arrays using newaxis print(one) print("------------------------") print(one.reshape(3, 2)) print("------------------------") print(one[ np.newaxis, :]) print("------------------------") print(one[:, np.newaxis]) a = np.array([1, 2, 3]) b = np.array([4, 5, 6]) c = np.concatenate([a, b]) print(c) print("------------------------") d = np.array([7, 8, 9]) e = np.concatenate([c, d]) print(e) print("------------------------") print(two) print("------------------------") f = np.concatenate([two, two]) print(f) print("------------------------") # concatenate along the second axis (zero-indexed) g = np.concatenate([two, two], axis = 1) print(g) print("------------------------") a = np.array([1, 2, 3]) print(a) print("------------------------") b = np.array([[4, 5, 6], [7, 8, 9]]) print(b) print("------------------------") print(np.vstack([a, b])) print("------------------------") # horizontally stack the arrays y = np.array([[99], [99]]) print(np.hstack([b, y])) print("------------------------") # Splitting of arrays a = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]) print(a) print("------------------------") b, c, d = np.split(a, [2, 5]) print(b, c, d) grid = np.arange(16).reshape(4, 4) print(grid) upper, lower = np.vsplit(grid, [2]) print(upper) print("------------------------") print(lower) left, right = np.hsplit(grid, [2]) print(left) print("------------------------") print(right)