NumPy: Array Functions in Python

NumPy functions that work on an array include the following:
np.sum()  Returns the sum of all elements.
np.min(), np.max()  Returns the minimum and maximum value in an array, respectively.
np.cumsum()  Returns the cumulative sum of the elements.
np.mean(), np.median()  Returns the mean and median for an array, respectively.

All of the functions mentioned above support the optional axis=1 parameter for the value in each row, axis=0 for columns.

np.corrcoef()  Returns the correlation coefficient for the array.
np.std()  Returns the standard deviation for the array.

The code below demonstrates array functions:

import numpy as np

x = np.array([[1,2,3,4], [5,6,7,8]])
y = np.array([[9,10,11,12], [13,14,15,16]])

print(x.sum(axis=0)) # np.sum(x) for the entire array
print(np.min(x)) # or x.min()

NumPy: Broadcasting When arithmetic operations on arrays of different sizes are performed, an operation called broadcasting is performed to expand a smaller array to match the larger array. Broadcasting requires that the arrays have the same dimensions or that a corresponding dimension be 1 so that it can be stretched. Additionally, dimensions that do no match cannot be stretched if one of them is not 1. A good way to understand broadcasting is by looking at some examples:

import numpy as np

two_d = np.array([[1,2,3,4], [5,6,7,8]]) # 2 x 4
one_d = np.array([[10]]) # 1
three_d = np.ones((3, 2)) # 3 x 2


print(np.add(one_d, three_d))

# print(np.add(two_d, three_d)) # ValueError