Saturday , December 21 2024
Numpy Mathematic Functions

Numpy Mathematic Functions

6 – Math Functions

NumPy Math Functions

In [2]:
import numpy as np
In [3]:
arr = np.random.randint(10,99,[3,3])
arr
Out[3]:
array([[45, 53, 78],
       [81, 88, 25],
       [71, 55, 59]])

Element-wise addition, subtraction, multiplication and division

In [4]:
print(arr + 10)
print(arr - 10)
print(arr * 10)
print(arr / 10)
[[55 63 88]
 [91 98 35]
 [81 65 69]]
[[35 43 68]
 [71 78 15]
 [61 45 49]]
[[450 530 780]
 [810 880 250]
 [710 550 590]]
[[4.5 5.3 7.8]
 [8.1 8.8 2.5]
 [7.1 5.5 5.9]]
In [5]:
# the above operations can be performed using numpy built-in functions
# which can save memory as the output can be stored in the original array rather than assigning new memory
arr1 = np.array([1,2,3])
np.add(arr1, [8,9,10], out=arr1)
print(arr1)
np.subtract(arr1, [8,9,10], out=arr1)
print(arr1)
np.multiply(arr1, [1,2,3], out=arr1)
print(arr1)
[ 9 11 13]
[1 2 3]
[1 4 9]

Element-wise exponentiation

In [6]:
print(np.exp(arr))
[[3.49342711e+19 1.04137594e+23 7.49841700e+33]
 [1.50609731e+35 1.65163625e+38 7.20048993e+10]
 [6.83767123e+30 7.69478527e+23 4.20121040e+25]]

Element-wise logorithm

In [7]:
# natural log
print(np.log(arr))      
[[3.80666249 3.97029191 4.35670883]
 [4.39444915 4.47733681 3.21887582]
 [4.26267988 4.00733319 4.07753744]]
In [8]:
# base 2
print(np.log2(arr))     
[[5.4918531  5.72792045 6.28540222]
 [6.33985    6.45943162 4.64385619]
 [6.14974712 5.78135971 5.88264305]]
In [9]:
# base 10
print(np.log10(arr))    
[[1.65321251 1.72427587 1.8920946 ]
 [1.90848502 1.94448267 1.39794001]
 [1.85125835 1.74036269 1.77085201]]

Element-wise square root

In [10]:
print(np.sqrt(arr))
[[6.70820393 7.28010989 8.83176087]
 [9.         9.38083152 5.        ]
 [8.42614977 7.41619849 7.68114575]]

Element-wise sine and cosine

In [11]:
print(np.sin(arr))
[[ 0.85090352  0.39592515  0.51397846]
 [-0.62988799  0.0353983  -0.13235175]
 [ 0.95105465 -0.99975517  0.63673801]]
In [12]:
print(np.cos(arr))
[[ 0.52532199 -0.91828279 -0.85780309]
 [ 0.77668598  0.99937328  0.99120281]
 [-0.30902273  0.02212676 -0.77108022]]

Sum along a specified axis

In [13]:
arr
Out[13]:
array([[45, 53, 78],
       [81, 88, 25],
       [71, 55, 59]])
In [14]:
# sum along the row
print(np.sum(arr, axis=0))    
[197 196 162]
In [15]:
# sum along the column
print(np.sum(arr, axis=1))    
[176 194 185]

Compute the min and max along a specified axis

In [16]:
arr
Out[16]:
array([[45, 53, 78],
       [81, 88, 25],
       [71, 55, 59]])
In [17]:
# calculate min along the row
print(np.min(arr, axis=0))
[45 53 25]
In [18]:
# calculate max along the column
print(np.max(arr, axis=1))    
[78 88 71]
In [19]:
# if axis not specified, calculate the max/min value of all elements
print(np.max(arr))
print(np.min(arr))
88
25

Compute the indices of the min and max along a specified axis

In [20]:
arr
Out[20]:
array([[45, 53, 78],
       [81, 88, 25],
       [71, 55, 59]])
In [21]:
# along the row
print(np.argmin(arr, axis=0))
print(np.argmax(arr, axis=0))
[0 0 1]
[1 1 0]
In [22]:
# along the column
print(np.argmin(arr, axis=1))
print(np.argmax(arr, axis=1))
[0 2 1]
[2 1 0]
In [23]:
# if axis not specified, return the index of the flattened array
print(np.argmin(arr))
print(np.argmax(arr))
5
4

Compute element-wise min and max of two arrays

In [24]:
arr1 = np.array([1, 3, 5, 7, 9])
arr2 = np.array([0, 4, 3, 8, 7])
print(np.maximum(arr1, arr2))
print(np.minimum(arr1, arr2))
[1 4 5 8 9]
[0 3 3 7 7]

Split fractional and integral parts of a floating-point array

In [25]:
arr1 = np.random.rand(10) * 10
re, intg = np.modf(arr1)
print('fractional: ', re)
print('integral: ', intg)
fractional:  [0.61238088 0.49277372 0.7488407  0.51805791 0.24094172 0.57935203
 0.97274589 0.34999626 0.85337645 0.4998429 ]
integral:  [4. 6. 3. 9. 5. 7. 8. 3. 2. 2.]

Compute the mean

In [26]:
arr
Out[26]:
array([[45, 53, 78],
       [81, 88, 25],
       [71, 55, 59]])
In [27]:
# compute the overall mean
print(np.mean(arr))
61.666666666666664
In [28]:
# compute the mean along the row
print(np.mean(arr, axis=0))   
[65.66666667 65.33333333 54.        ]
In [29]:
# compute the mean along the column
print(np.mean(arr, axis=1)) 
[58.66666667 64.66666667 61.66666667]

Compute the median

In [30]:
arr
Out[30]:
array([[45, 53, 78],
       [81, 88, 25],
       [71, 55, 59]])
In [31]:
# compute the overall median
print(np.median(arr))
59.0
In [32]:
# compute the median along the row
print(np.median(arr, axis=0)) 
[71. 55. 59.]
In [33]:
# compute the median along the column
print(np.median(arr, axis=1))
[53. 81. 59.]

Compute the percentile

In [34]:
arr1 = np.random.rand(100)
# compute 5, 65, and 95 percentiles of the array
print(np.percentile(arr1, [5, 65, 95]))
[0.04720134 0.59197879 0.93064323]

Compute the standard deviation & variance

In [35]:
arr
Out[35]:
array([[45, 53, 78],
       [81, 88, 25],
       [71, 55, 59]])
In [36]:
# compute the overall standard deviation
print(np.std(arr))
18.76758434701233
In [37]:
# compute the standard deviation along the row
print(np.std(arr, axis=0))
[15.17307557 16.04853749 21.92411154]
In [38]:
# compute the standard deviation along the column
print(np.std(arr, axis=1))
[14.05544576 28.19377394  6.79869268]
In [39]:
# compute the overall variance
print(np.var(arr))
352.22222222222223
In [40]:
# compute the variance along the row
print(np.var(arr, axis=0))
[230.22222222 257.55555556 480.66666667]
In [41]:
# compute the variance along the column
print(np.var(arr, axis=1))
[197.55555556 794.88888889  46.22222222]

Compute the covariance & correlation

In [42]:
arr = np.random.rand(5,8)
arr
Out[42]:
array([[0.95214752, 0.29493415, 0.05878789, 0.39374683, 0.71688838,
        0.05481   , 0.00623518, 0.07657365],
       [0.44869699, 0.38991553, 0.15028163, 0.25317292, 0.99921478,
        0.86781744, 0.28919635, 0.43225943],
       [0.24550552, 0.7726166 , 0.27018533, 0.68800599, 0.23574331,
        0.32689282, 0.65105567, 0.86009933],
       [0.23499775, 0.75088904, 0.84001736, 0.14851715, 0.52967944,
        0.66669622, 0.90208167, 0.0071426 ],
       [0.4463815 , 0.2661077 , 0.91024507, 0.76804913, 0.63738655,
        0.95655654, 0.4639961 , 0.96585732]])
In [43]:
print(np.cov(arr))
[[ 0.12277939  0.03236792 -0.0411153  -0.04672846 -0.04043407]
 [ 0.03236792  0.08970289 -0.03339385 -0.00281817  0.00876793]
 [-0.0411153  -0.03339385  0.06846022 -0.0231893  -0.00798352]
 [-0.04672846 -0.00281817 -0.0231893   0.11496709 -0.02531996]
 [-0.04043407  0.00876793 -0.00798352 -0.02531996  0.07046295]]
In [44]:
print(np.corrcoef(arr[:,0], arr[:,1]))
[[ 1.         -0.73159923]
 [-0.73159923  1.        ]]

Compute cumulative sum & product

In [45]:
# calculate the cumulative sums along the row
print(np.cumsum(arr, axis=0))    
[[0.95214752 0.29493415 0.05878789 0.39374683 0.71688838 0.05481
  0.00623518 0.07657365]
 [1.40084451 0.68484968 0.20906952 0.64691975 1.71610316 0.92262744
  0.29543152 0.50883308]
 [1.64635003 1.45746628 0.47925485 1.33492574 1.95184647 1.24952026
  0.94648719 1.36893241]
 [1.88134778 2.20835533 1.3192722  1.48344289 2.48152591 1.91621647
  1.84856886 1.37607501]
 [2.32772928 2.47446303 2.22951728 2.25149203 3.11891246 2.87277302
  2.31256496 2.34193234]]
In [46]:
# calculate the cumulative sums along the column
print(np.cumsum(arr, axis=1))    
[[0.95214752 1.24708168 1.30586957 1.6996164  2.41650477 2.47131477
  2.47754995 2.5541236 ]
 [0.44869699 0.83861252 0.98889414 1.24206707 2.24128185 3.10909929
  3.39829564 3.83055507]
 [0.24550552 1.01812212 1.28830745 1.97631344 2.21205675 2.53894956
  3.19000523 4.05010456]
 [0.23499775 0.98588679 1.82590415 1.9744213  2.50410074 3.17079696
  4.07287863 4.08002123]
 [0.4463815  0.7124892  1.62273427 2.39078341 3.02816996 3.9847265
  4.4487226  5.41457992]]
In [47]:
# calculate the cumulative product along the row
print(np.cumprod(arr, axis=0))   
[[9.52147523e-01 2.94934153e-01 5.87878910e-02 3.93746829e-01
  7.16888379e-01 5.48099993e-02 6.23517692e-03 7.65736508e-02]
 [4.27225725e-01 1.14999407e-01 8.83473992e-03 9.96860357e-02
  7.16325465e-01 4.75650733e-02 1.80319039e-03 3.30996827e-02]
 [1.04886274e-01 8.88504503e-02 2.38701711e-03 6.85845898e-02
  1.68868933e-01 1.55486808e-02 1.17397732e-03 2.84690148e-02]
 [2.46480383e-02 6.67168298e-02 2.00513580e-03 1.01859878e-02
  8.94464026e-02 1.03662466e-02 1.05902342e-03 2.03342908e-04]
 [1.10024283e-02 1.77538621e-02 1.82516499e-03 7.82333915e-03
  5.70119342e-02 9.91590108e-03 4.91382736e-04 1.96400237e-04]]
In [48]:
# calculate the cumulative product along the column
print(np.cumprod(arr, axis=1))  
[[9.52147523e-01 2.80820823e-01 1.65088639e-02 6.50031283e-03
  4.65999872e-03 2.55414527e-04 1.59255476e-06 1.21947732e-07]
 [4.48696987e-01 1.74953924e-01 2.62923604e-02 6.65651373e-03
  6.65128691e-03 5.77210279e-03 1.66927104e-03 7.21558150e-04]
 [2.45505521e-01 1.89681640e-01 5.12491965e-02 3.52597542e-02
  8.31225104e-03 2.71721515e-03 1.76905832e-03 1.52156587e-03]
 [2.34997748e-01 1.76457235e-01 1.48227140e-01 2.20142724e-02
  1.16605075e-02 7.77401628e-03 7.01279758e-03 5.00896384e-05]
 [4.46381498e-01 1.18785554e-01 1.08123965e-01 8.30445178e-02
  5.29314589e-02 5.06319334e-02 2.34930196e-02 2.26909050e-02]]

Element-wise comparison

In [49]:
arr1 = np.array([1,2,3,4,5])
arr2 = np.array([5,4,3,2,1])
In [50]:
# return an array of bools
print(arr1 == arr2)    
print(arr1 < 3)
[False False  True False False]
[ True  True False False False]

About Machine Learning

Check Also

Groupby in Pandas - Data Science Tutorials

Groupby in Pandas – Data Science Tutorials

14- Groupby Groupby in Pandas¶Pandas groupby is used for grouping the data according to the …

Leave a Reply

Your email address will not be published. Required fields are marked *