NumPy Math Functions¶
In [2]:
import numpy as np
In [3]:
arr = np.random.randint(10,99,[3,3])
arr
Out[3]:
Element-wise addition, subtraction, multiplication and division¶
In [4]:
print(arr + 10)
print(arr - 10)
print(arr * 10)
print(arr / 10)
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)
Element-wise exponentiation¶
In [6]:
print(np.exp(arr))
Element-wise logorithm¶
In [7]:
# natural log
print(np.log(arr))
In [8]:
# base 2
print(np.log2(arr))
In [9]:
# base 10
print(np.log10(arr))
Element-wise square root¶
In [10]:
print(np.sqrt(arr))
Element-wise sine and cosine¶
In [11]:
print(np.sin(arr))
In [12]:
print(np.cos(arr))
Sum along a specified axis¶
In [13]:
arr
Out[13]:
In [14]:
# sum along the row
print(np.sum(arr, axis=0))
In [15]:
# sum along the column
print(np.sum(arr, axis=1))
Compute the min and max along a specified axis¶
In [16]:
arr
Out[16]:
In [17]:
# calculate min along the row
print(np.min(arr, axis=0))
In [18]:
# calculate max along the column
print(np.max(arr, axis=1))
In [19]:
# if axis not specified, calculate the max/min value of all elements
print(np.max(arr))
print(np.min(arr))
Compute the indices of the min and max along a specified axis¶
In [20]:
arr
Out[20]:
In [21]:
# along the row
print(np.argmin(arr, axis=0))
print(np.argmax(arr, axis=0))
In [22]:
# along the column
print(np.argmin(arr, axis=1))
print(np.argmax(arr, axis=1))
In [23]:
# if axis not specified, return the index of the flattened array
print(np.argmin(arr))
print(np.argmax(arr))
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))
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)
Compute the mean¶
In [26]:
arr
Out[26]:
In [27]:
# compute the overall mean
print(np.mean(arr))
In [28]:
# compute the mean along the row
print(np.mean(arr, axis=0))
In [29]:
# compute the mean along the column
print(np.mean(arr, axis=1))
Compute the median¶
In [30]:
arr
Out[30]:
In [31]:
# compute the overall median
print(np.median(arr))
In [32]:
# compute the median along the row
print(np.median(arr, axis=0))
In [33]:
# compute the median along the column
print(np.median(arr, axis=1))
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]))
Compute the standard deviation & variance¶
In [35]:
arr
Out[35]:
In [36]:
# compute the overall standard deviation
print(np.std(arr))
In [37]:
# compute the standard deviation along the row
print(np.std(arr, axis=0))
In [38]:
# compute the standard deviation along the column
print(np.std(arr, axis=1))
In [39]:
# compute the overall variance
print(np.var(arr))
In [40]:
# compute the variance along the row
print(np.var(arr, axis=0))
In [41]:
# compute the variance along the column
print(np.var(arr, axis=1))
Compute the covariance & correlation¶
In [42]:
arr = np.random.rand(5,8)
arr
Out[42]:
In [43]:
print(np.cov(arr))
In [44]:
print(np.corrcoef(arr[:,0], arr[:,1]))
Compute cumulative sum & product¶
In [45]:
# calculate the cumulative sums along the row
print(np.cumsum(arr, axis=0))
In [46]:
# calculate the cumulative sums along the column
print(np.cumsum(arr, axis=1))
In [47]:
# calculate the cumulative product along the row
print(np.cumprod(arr, axis=0))
In [48]:
# calculate the cumulative product along the column
print(np.cumprod(arr, axis=1))
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)