import numpy as np
arr = np.random.randint(10,99,[3,3])
arr
print(arr + 10)
print(arr - 10)
print(arr * 10)
print(arr / 10)
# 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)
print(np.exp(arr))
# natural log
print(np.log(arr))
# base 2
print(np.log2(arr))
# base 10
print(np.log10(arr))
print(np.sqrt(arr))
print(np.sin(arr))
print(np.cos(arr))
arr
# sum along the row
print(np.sum(arr, axis=0))
# sum along the column
print(np.sum(arr, axis=1))
arr
# calculate min along the row
print(np.min(arr, axis=0))
# calculate max along the column
print(np.max(arr, axis=1))
# if axis not specified, calculate the max/min value of all elements
print(np.max(arr))
print(np.min(arr))
arr
# along the row
print(np.argmin(arr, axis=0))
print(np.argmax(arr, axis=0))
# along the column
print(np.argmin(arr, axis=1))
print(np.argmax(arr, axis=1))
# if axis not specified, return the index of the flattened array
print(np.argmin(arr))
print(np.argmax(arr))
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))
arr1 = np.random.rand(10) * 10
re, intg = np.modf(arr1)
print('fractional: ', re)
print('integral: ', intg)
arr
# compute the overall mean
print(np.mean(arr))
# compute the mean along the row
print(np.mean(arr, axis=0))
# compute the mean along the column
print(np.mean(arr, axis=1))
arr
# compute the overall median
print(np.median(arr))
# compute the median along the row
print(np.median(arr, axis=0))
# compute the median along the column
print(np.median(arr, axis=1))
arr1 = np.random.rand(100)
# compute 5, 65, and 95 percentiles of the array
print(np.percentile(arr1, [5, 65, 95]))
arr
# compute the overall standard deviation
print(np.std(arr))
# compute the standard deviation along the row
print(np.std(arr, axis=0))
# compute the standard deviation along the column
print(np.std(arr, axis=1))
# compute the overall variance
print(np.var(arr))
# compute the variance along the row
print(np.var(arr, axis=0))
# compute the variance along the column
print(np.var(arr, axis=1))
arr = np.random.rand(5,8)
arr
print(np.cov(arr))
print(np.corrcoef(arr[:,0], arr[:,1]))
# calculate the cumulative sums along the row
print(np.cumsum(arr, axis=0))
# calculate the cumulative sums along the column
print(np.cumsum(arr, axis=1))
# calculate the cumulative product along the row
print(np.cumprod(arr, axis=0))
# calculate the cumulative product along the column
print(np.cumprod(arr, axis=1))
arr1 = np.array([1,2,3,4,5])
arr2 = np.array([5,4,3,2,1])
# return an array of bools
print(arr1 == arr2)
print(arr1 < 3)