10-numpy-matplotlib
10-numpy-matplotlib
numpy and
matplotlib
Kameswari Chebrolu
Numpy (Numeric Python)
• Library used for high performance computing and data
analysis
– High-level math functions involving arrays/matrices
– Fast numerical computations like matrix multiplication; linear
algebra/fourier transform etc
• Very efficient for large arrays of data
– Stores data contiguously → less memory, faster operation
(10-100 times faster)
• 1000 x 1000 matrix multiply
– Python triple loop takes > 10 min.
– Numpy takes ~0.03 seconds
– Batch operations can be managed without writing loops
(vectorization)
Ndarray (n-dimentional-array)
• ndarray used for storage of homogeneous data
– All elements the same type
• Supports convenient slicing, indexing and efficient vectorized
computation
• Every array must have a shape and a dtype (data type)
– Vector: array in single dimension
– Matrix: array in two dimensions
– Tensor: 3-D or higher dimensional arrays
• Create arrays (see arrays.py)
– Different data types, dimensions
– Conversion from one type to another
– Copy vs view
• Create special arrays (see special-arrays.py)
– eye, zeros, ones, random, in a range
• Indexing of arrays
– Slicing, stepping, accessing columns/rows
– Printing rows, elements
• Shaping of arrays
– Convert 1D to 2D/3D, 2D to 3D, 3D to 1D etc
• Arithmetic
– Element by element arithmetic for same shaped
arrays
– Broadcasting in case of dissimilar shapes (see
next slide)
– Dot multiplication, transpose
– Trignometric functions
– Stat functions
Broadcasting
• Functions:
– concatenate
– stack
– split, array_split
– where
– sort
• Filtering
Matplotlib
• Used for drawing charts and for general
visualization
– Inspired by MATLAB (lot of common terms like axis,
plots etc)
• Most of the Matplotlib utilities lies under the
pyplot submodule
– You can import it via the matplotlib.pyplot namespace
– Each pyplot function makes some change to a figure
• E.g. create a plotting area, draw some lines, decorates the
plot with labels etc
• Plot (specify marker, line characteristics)
• Multiple plots
• Labels, Title, Grid
• Subplots
• Different plots
– Scatter, Bar, Pie
• Use of math functions
• Saving figures to a file
References
• Numpy:
https://www.w3schools.com/python/numpy/
default.asp
• Matplotlib:
https://www.w3schools.com/python/matplot
lib_pyplot.asp