NumPy features in short
What is NumPy?
NumPy is a python library which provide fixed size multidimensional arrays at creation and functionalities for scientific calculations on those nd-arrays which includes logical,algebrical,mathematical,statistical operations and many more cool features. These are quite different python lists and to keep up with the new python-based software there is a need to understand numpy and have a hands-on numpy.
Now, we are aware of why we use numpy. Let’s jump into some basics of numpy.
Creating an ndarray
is the first thing we want to learn
Now, We know how to create an ndarray. It’s time to play with that a bit
let us import numpy as np
(for the rest of the tutorial)
Basics properties of ndarray
* ndim: Gives the number of dimensions(axis)
* shape: gives the shape of the ndarray
* size: gives the number of entries in the ndarray(i.e product of elements in shape)
* dtype: gives the type of array
* itemsize: gives the size of the datatype
Now, Let’s see some other functionalities to initilaize nd-arrays with some custom initializations
* np.zeros((m,n)): gives a matrix of size mxn with all zeros
* np.ones((m,n)): gives a matrix of size mxn with all ones
* np.arange(m): gives a list of size m from 0 to m-1
* np.arange(i,f,s): gives a list of elements starting from i and with a step of s
* np.ndarray.reshape(a1,...ai,....an)` : reshapes the already existing ndarray. if any one ai is -1. it's size is deciding based on the size of teh ndarray
* np.linspace(i,f, n)`: gives n points between i and f which are equi-distant
Now, Let us code this out in Jupyter Notebook:
Operations on Matrices
As said earlier numpy provides functionalities for scientific calculations to make life easy for a developer and reduce user written for loops to implement complex multiplications
Now, Let us code this in Jupyter Notebook or some-thing of your choice
Adhoc Properties
when argument like axis = 0 or 1 is mentioned then the arrays collapses along the mentioned axis and the operation happens along the mentioned axis
Example:
Math functions
np.sqrt
:return the square root matrix of the given matrix
np.exp():
return the exponential matrix of the given matrix
For Loop
Stacking
Stacking
is a process where we stack up two matrices one on the top of the other or one beside the other
with vstack, arrays are concatenated along axis 0
with hstack, arrays are concatenated along axis 1
The function column_stack, stacks 1D arrays as columns into a 2D array. It is equivalent to hstack only for 2D arrays
The function row_stack stacks similar to as vstack
Split function
np.hsplit(a,(3,4))
: returns arrays split at columns 3,4 resulting into 3 arrays
Making Copies and Deep copy
Storing values in different forms is key to improve the perfomance. Numpy provides us with some ways to make copies and store data.
Linear Algebra
Here are some of the basic Linear Algebraic functionalities like Transpose, Inverse, Identity , Solving a matrix or finding trace or finding the eigen values and corresponding eigen values. These functions provided in Numpy helps in making the code readable and consistent
Here is how we define a function and implement: