Week2-1 Numpy
Week2-1 Numpy
▪ NumPy Introduction
▪ Creation of Arrays
▪ NumPy stands for Numerical Python and it is the fundamental package for
scientific computing with Python.
▪ It contains both the data structures needed for the storing and accessing
arrays, and operations and functions for computation using these arrays.
▪ Unlike lists, the arrays must have the same data types for all its elements.
▪ The homogeneity of arrays allows highly optimized functions that use arrays
as their inputs and outputs.
Usages of high-dimensional arrays in data analysis
▪ In a two-dimensional array, you have rows and columns. The rows are indicated
as “axis 0” while the columns are the “axis 1”.
▪ The number of the axis goes up accordingly with the number of the dimensions.
Creation of arrays
▪ With np.linspace() one does not have to compute the length of the step, but
instead one specifies the wanted number of elements. By default, the endpoint is
included in the result, unlike with arange.
Creation of arrays with random elements
▪ NumPy can easily produce arrays of wanted shape with random numbers.
▪ When an array is reshaped, its number of elements stays at the same, but
they are reinterpreted into a different shape.
▪ If you want to concatenate arrays with different dimensions, you must first
reshape the arrays to have the same number of dimensions.
– E.g, add a new row (column) to a 2d array
Stack
▪ Space occupied by
NumPy is less compare
to list.
Fast computation on arrays
▪ One of the reasons for the popularity of NumPy is that these computations
can be very efficient, much more efficient than what Python can normally do.
▪ The biggest bottle-necks in efficiency are the loops, which can be iterated
millions, billions, or even more times.
▪ What slows down loops in Python is the fact that Python is dynamically typed
language: at each expression Python has to find out the types of the
arguments of the operations.
Fast computation examples
▪ Because each iteration in NumPy is using identical operations only the data
differs, this can be compiled into machine language, and then performed in
one go, hence avoiding Python’s dynamic typing.
▪ The speed of NumPy partly comes from the fact that its arrays must have same
type for all the elements. This requirement allows some efficient optimizations.
Broadcasting
▪ We have seen that NumPy allows array operations that are performed
element-wise.
▪ NumPy also allows binary operation that do not require the two arrays to
have the same shape.
▪ NumPy tries to stretch the arrays to have the same shape, then perform the
element-wise operation.
▪ A related operation is the argsort function. Which doesn’t sort the elements
but returns the indices of the sorted elements.
These indices [3, 0, 4, 1, 2] say that the smallest element of the array is in position 3 of a,
second smallest elements is in position 0 of a, third smallest is in position 4, and so on.
Matrix operations
▪ Plotting (Matplotlib)
▪ Machine Learning
▪ Signal Processing