Python NumPy Mathematical Functions for Arrays
kodingwindow@kw:~$ python3 ... >>> from numpy import * >>> arr = array([25, 27, 25, 1, 0, -1, 3.14]) >>> sum(arr) 80.14 >>> prod(arr) -0.0 >>> min(arr) -1.0 >>> max(arr) 27.0 >>> argmin(arr) 5 >>> argmax(arr) 1 >>> unique(arr) array([-1. , 0. , 1. , 3.14, 25. , 27. ]) >>> sort(arr) array([-1. , 0. , 1. , 3.14, 25. , 25. , 27. ]) >>> log(arr)<stdin>:1: RuntimeWarning: divide by zero encountered in log <stdin>:1: RuntimeWarning: invalid value encountered in log array([3.21887582, 3.29583687, 3.21887582, 0. , -inf, nan, 1.1442228 ]) >>> abs(arr) array([25. , 27. , 25. , 1. , 0. , 1. , 3.14]) >>> sqrt(arr)<stdin>:1: RuntimeWarning: invalid value encountered in sqrt array([5. , 5.19615242, 5. , 1. , 0. , nan, 1.77200451]) >>> power(arr,0) array([1., 1., 1., 1., 1., 1., 1.]) >>> exp(arr) array([7.20048993e+10, 5.32048241e+11, 7.20048993e+10, 2.71828183e+00, 1.00000000e+00, 3.67879441e-01, 2.31038669e+01]) >>> mean(arr) 11.448571428571428 >>> median(arr) 3.14 >>> var(arr) 153.3387265306122 >>> cov(arr) array(178.89518095) >>> std(arr) 12.38300151540862
>>> arr1 = array([1, -1, 3.14, "Kodingwindow", 'K']) >>> arr2 = array([0, 'W']) >>> concatenate([arr1, arr2]) array(['1', '-1', '3.14', 'Kodingwindow', 'K', '0', 'W'], dtype='<U32')
Python NumPy Trigonometric Functions
>>> from numpy import * >>> arr = array([25, 27, 25, 1, 0, -1, 3.14]) >>> sin(arr) array([-0.13235175, 0.95637593, -0.13235175, 0.84147098, 0. , -0.84147098, 0.00159265]) >>> cos(arr) array([ 0.99120281, -0.29213881, 0.99120281, 0.54030231, 1. , 0.54030231, -0.99999873]) >>> tan(arr) array([-1.33526407e-01, -3.27370380e+00, -1.33526407e-01, 1.55740772e+00, 0.00000000e+00, -1.55740772e+00, -1.59265494e-03]) >>> arcsin(arr) array([ nan, nan, nan, 1.57079633, 0. , -1.57079633, nan]) >>> arccos(arr) array([ nan, nan, nan, 0. , 1.57079633, 3.14159265, nan]) >>> arctan(arr) array([ 1.53081764, 1.53377621, 1.53081764, 0.78539816, 0. , -0.78539816, 1.26248066])
What Next?
Python indexing and slicing on NumPy arrays
How to create arrays using the linspace() and logspace() functions
How to create arrays using the arange(), zeros(), and ones() functions
Advertisement