NumPy
Workshop 2 – Interactive classroom
More on NumPy
What does ipython tell us about numpy?
np?
np.
+[TAB]
np.linspace()
+[Shift+TAB]
A numpy cheet sheat
Creating Arrays:
numpy.array()
: Create an array from a list or iterable.numpy.zeros()
: Create an array filled with zeros.numpy.ones()
: Create an array filled with ones.numpy.empty()
: Create an empty array (uninitialized values).numpy.full()
: Create an array filled with a scalar of your choice.numpy.arange()
: Create an array with regularly spaced values.numpy.linspace()
: Create an array with a specified number of evenly spaced values.
Mathematical Operations:
numpy.sqrt()
: Compute the square root of each element.numpy.sin()
,numpy.cos()
,numpy.tan()
: Trigonometric functions.numpy.dot()
: Dot product of two arrays (matrix multiplication).numpy.cross()
: Cross product of two arraysnumpy.transpose()
: Transpose an array or matrix.
Statistical Functions:
numpy.sum()
: Sum of array elements.numpy.mean()
,numpy.median()
: Mean and median of array elements.numpy.std()
: Standard deviation of array elements.numpy.max()
,numpy.min()
: Maximum and minimum values.numpy.argmax()
,numpy.argmin()
: Indices of maximum and minimum values.
Array Attributes:
shape
: Get the shape (dimensions) of an array.dtype
: Get the data type of an array.ndim
: Get the number of dimensions.size
: Get the number of elements in an array.
Array Manipulation:
numpy.concatenate()
: Join arrays along an existing axis.numpy.stack
,numpy.vstack()
,numpy.hstack()
: Stack arrays along new axis, vertically, or horizontally.numpy.split()
: Split an array into multiple sub-arrays.numpy.copy()
: Create a copy of an array.numpy.resize()
: Resize an array.numpy.reshape()
: Reshape an array into a new shape.numpy.array_equal()
: Check whether two arrays have the same shape and values.
Random Number Generation:
numpy.random.randint()
: Generate random integers.numpy.random.normal()
: Generate random numbers from a normal distribution.numpy.random.seed()
: Set the random seed for reproducibility.
Linear Algebra:
numpy.linalg.inv()
: Compute the matrix inverse.numpy.linalg.det()
: Compute the determinant of a matrix.numpy.linalg.eig()
: Compute eigenvalues and eigenvectors of a matrix.
Learning checklist
- I know what NumPy is and what I need it for.
- I have an overview of the functionalities that NumPy offers.
- I have created and manipulated my first NumPy arrays.
- I know that arrays are mutable, and I am aware that this can lead to confusing behaviour when several references exist for the same data stored in memory. I also know that I can avoid this problem by copying arrays instead of creating multiple references to the same array.