Operations - Numpy January 6, 2021 [1]: import numpy as np [2]: # Define Two 3x3 Matrices A = np.array([[2, 6, 4], [9, 1, 3], [8, 5, 7]]) B = np.array([[6, 8, 1], [7, 9, 5], [8, 2, 4]]) [3]: print('Mathematical Operations Using Numpy: ') Mathematical Operations Using Numpy: [4]: # Addition A + 1 [4]: array([[ 3, [10, [ 9, 7, 2, 6, 5], 4], 8]]) [5]: A + B + 6 [5]: array([[14, 20, 11], [22, 16, 14], [22, 13, 17]]) [6]: # Substraction A - 2 [6]: array([[ 0, 4, [ 7, -1, [ 6, 3, 2], 1], 5]]) [7]: A - B - 3 [7]: array([[ -7, -5, [ -1, -11, [ -3, 0, 0], -5], 0]]) 1 [8]: # Multiply By a Constant 6 * A [8]: array([[12, 36, 24], [54, 6, 18], [48, 30, 42]]) [9]: # One-to-One Multiplication A * B [9]: array([[12, 48, 4], [63, 9, 15], [64, 10, 28]]) [10]: # Devide By a Constant A / 3 [10]: array([[0.66666667, 2. , 1.33333333], [3. , 0.33333333, 1. ], [2.66666667, 1.66666667, 2.33333333]]) [12]: # One-to-One Division A / B [12]: array([[0.33333333, 0.75 , 4. [1.28571429, 0.11111111, 0.6 [1. , 2.5 , 1.75 ], ], ]]) [13]: # Modulus By a Constant (remainder by division) A % 6 [13]: array([[2, 0, 4], [3, 1, 3], [2, 5, 1]], dtype=int32) [14]: # One-to-One Modulus (remainder by division) A % B [14]: array([[2, 6, 0], [2, 1, 3], [0, 1, 3]], dtype=int32) [16]: # Exponent As Array Eelements 2 ** A [16]: array([[ 4, [512, 64, 2, 16], 8], 2 [256, 32, 128]], dtype=int32) [18]: # One-to-One Exponentiation (Array Eelement Raised to Corresponding Array␣ ,→Eelement) A ** B [18]: array([[ 64, [ 4782969, [16777216, 1679616, 1, 25, 4], 243], 2401]], dtype=int32) [23]: # Mixture of Operations": 2 ** (A + B) * (A + B) / A + 1 [23]: array([[1.02500000e+03, 3.82303333e+04, 4.10000000e+01], [1.16509444e+05, 1.02410000e+04, 6.83666667e+02], [1.31073000e+05, 1.80200000e+02, 3.21928571e+03]]) [24]: # Since We gone through One-to-One Multiplication: # Now is the Time To Take a Look At Mathematical Matrix Multiplication # In This Case We Multiply Two Matrix (No Scalars All Elements Should be␣ ,→Matrices) A.dot(B) [24]: array([[ 86, 78, [ 85, 87, [139, 123, 48], 26], 61]]) [26]: # Another Handy Way to do So is by Using the (At) Operator [email protected] [26]: array([[ 86, 78, [ 85, 87, [139, 123, 48], 26], 61]]) [29]: # Element Comparisons A == B [29]: array([[False, False, False], [False, False, False], [ True, False, False]]) [30]: A != B [30]: array([[ True, [ True, [False, True, True, True, True], True], True]]) 3 [31]: # In case we needed the Boolean Answer: np.array_equal(A, B) [31]: False [32]: np.array_equal(A, A) [32]: True [36]: # Logical Operations: Or & And # Lets Create Two arrays of Bool Values b1 = np.array([0, 1, 0, 1, 1, 1]) b2 = np.array([1, 1, 0, 1, 0, 1]) [38]: np.logical_or(b1, b2) [38]: array([ True, True, False, True, True, True]) True, False, True]) [39]: np.logical_and(b1, b2) [39]: array([False, True, False, [40]: # Mapping Numpy Math-Built-in Functions all over elements np.sin(A) [40]: array([[ 0.90929743, -0.2794155 , -0.7568025 ], [ 0.41211849, 0.84147098, 0.14112001], [ 0.98935825, -0.95892427, 0.6569866 ]]) [41]: np.cos(B) [41]: array([[ 0.96017029, -0.14550003, 0.54030231], [ 0.75390225, -0.91113026, 0.28366219], [-0.14550003, -0.41614684, -0.65364362]]) [42]: np.tan(A + B) [42]: array([[ [ [ -6.79971146, 0.30063224, 0.30063224, 7.24460662, -3.38051501], 0.64836083, -6.79971146], 0.87144798, -225.95084645]]) [56]: np.sqrt(A + B) [56]: array([[2.82842712, 3.74165739, 2.23606798], [4. , 3.16227766, 2.82842712], [4. , 2.64575131, 3.31662479]]) 4 [43]: np.floor(A / 6) [43]: array([[0., 1., 0.], [1., 0., 0.], [1., 0., 1.]]) [44]: np.ceil(9 / B) [44]: array([[2., 2., 9.], [2., 1., 2.], [2., 5., 3.]]) [47]: # Median , Mean, Average, Standard Deviation & Variance a = np.array([1, 2, 6, 9, 7, 2, 8, 2, 6]) print(f'Median of a is: {np.median(a)}') print(f'Mean of a is: {np.mean(a)}') print(f'Average of a is: {np.average(a)}') print(f'Standard Dev of a is: {np.std(a)}') print(f'Variance of a is: {np.var(a)}') Median of Mean of Average of Standard Dev of Variance of a a a a a is: is: is: is: is: 6.0 4.777777777777778 4.777777777777778 2.8588178511708016 8.172839506172838 [48]: # Log & Exp np.log(A) [48]: array([[0.69314718, 1.79175947, 1.38629436], [2.19722458, 0. , 1.09861229], [2.07944154, 1.60943791, 1.94591015]]) [49]: np.exp(B) [49]: array([[4.03428793e+02, 2.98095799e+03, 2.71828183e+00], [1.09663316e+03, 8.10308393e+03, 1.48413159e+02], [2.98095799e+03, 7.38905610e+00, 5.45981500e+01]]) [53]: # Array Transposition(The Flip of the Matrix Over the Main Diagonal) np.transpose(A) [53]: array([[2, 9, 8], [6, 1, 5], [4, 3, 7]]) 5 [54]: # Or the Handy Way A.T [54]: array([[2, 9, 8], [6, 1, 5], [4, 3, 7]]) [55]: B.T [55]: array([[6, 7, 8], [8, 9, 2], [1, 5, 4]]) [ ]: 6