Python-NumPy Tutorial CIS 581
Intro to NumPy A Python-based Matlab-style library for scientific calculation Uses array as basic operation units For MATLAB Users: Array -- Matrix Indexing & Slicing Row Major -- Column Major
NumPy Arrays A NumPy array is a grid of values, all of the same type. The shape of an array is a tuple of integers giving the size of array along each dimension Creating arrays: np.array([]) np.zeros, np.ones, np.full, np.eye, np.empty, … np.arange np.random Code
Datatypes Some functions require specific datatypes to run properly np.uint8 -- for most images, .png, .jpg, etc. np.int16/32/64 -- integers of different bits length np.float16/32/64 -- floats of different bits length np.bool -- boolean value Manipulation: Code
Slicing Python indexing starts from 0 Slicing a[0:i] will have a[i] excluded Default in slicing Minus value in slicing Use a[0, :] and a[0, …] to omit other axis Use bool value as index Code
Element-wise functions +, -, *, /, &, | np.cos, np.sin, np.tan, np.radians, np.angles np.acos, np.asin, np.atan, np.atan2 np.round, np.ceil, np.floor np.cumsum np.log, np.log2, np.log10, np.exp np.bitwise_and, np.bitwise_or
Shape and broadcasting Most numpy functions has shape constraints for inputs (e.g., input element must equal for most element-wise functions) Use np.reshape() to manipulate the shape Broadcasting NumPy will automatically repeat array to meet shape requirements Start from the last dimension, must fulfill basic rules
Other important functions np.sum, np.mean, np.std, np.max, np.min axis, keep_dims np.sort, np.argsort np.where np.linalg Linear algebra module np.random Generate random variables