Broadcasting: ============= refers to the ability of performing operations on arrays with different shapes Ex: + 1-d + 2-d ==> while the operation, the lower dimension array can automatically expand according to the higher dimension. Rules for Broadcasting: ====================== 1) If arrays are with different dimensions: the shape of smaller dimension is must be padded with '1' on the left side until this match with higher dimension array. [[1 1 1 1],[1 1 1 1],[1 2 3 4]] + [[1 2 3 4],[5 6 7 8],[9 10 11 12]] 2) The size of each dimension must either be the same or one of the dimension must be same. 3) Broadcasting can be applied from last dimension to first dimension. # WAP IN NUMPY TO ADD A SCALAR TO AN ARRAY. """ [1 2 3] + 10 [1,2,3] + [1,1,10] """ import numpy a = numpy.array([1,2,3]) b = numpy.array([[1,2,3],[4,5,6],[7,8,9]]) result = a + 10 res1 = b + 100 print(result) print(res1) ============================== import numpy as np a = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]]) b = np.array([10,20,30,40]) # [[1,1,1,1],[1,1,1,1],[10,20,30,40]] result = a + b print("The Sum of two differently shaped arrays = ",result) ============================== # Broadcasting with multi-dimensional arrays import numpy a = numpy.ones((2,3,4)) b = numpy.arange(4) print("The Given array = ",a) print("The Given array = ",b) result = a + b print(result) ================================================= Arithmetic Operations: ====================== Array Addition: =============== import numpy a = numpy.array([10,20,30,40,50]) b = numpy.array([50,40,30,20,10]) sum = a + b print("The Sum = ",sum) =============================== import numpy as np a = np.array([[1,2,3,4,5],[6,7,8,9,10]]) b = np.array([[10,9,8,7,6],[5,4,3,2,1]]) sum = a+b print("The Sum = ",sum) ============================= Array Subtraction: ================== import numpy as np a = np.arange(1,25).reshape(2,3,4) b = np.arange(24).reshape(2,3,4) print("a = ",a) print("b = ",b) difference = a-b print(difference) ============================== Array Multiplication: ===================== import numpy a = numpy.array([1,2,3,4]) b = numpy.array([5,6,7,8]) product = a * b print("The Product = ",product) ====================================== Array Division: =============== import numpy as np a = np.arange(5) b = np.arange(1,6) print("a = ",a) print("b = ",b) print(a/b) print(a//b) print(a%b) ============================ Array Power Operation: ====================== Syntax: a ** b import numpy a = numpy.arange(5) b = numpy.arange(1,6) print("a = ",a) print("b = ",b) print(a ** b) ================================================ # Sorting import numpy as np a = np.array([[100,12,301],[99,88,101]]) print("The Array before the sorting is = ",a) # sort() s_a1 = np.sort(a) # forward sorting print("Original Array = ",a) print("The Sorted Array = ",s_a1) print(np.sort(a,axis = 1))