NumPy is package for scientific computing. It provides library for multidimensional array. This library can perform many mathematical operation on large set of dataset. Also helpful for sorting large dataset and performing IO operations. This python library can be using for random simulation data generator.
Main NumPy object is “ndarray”. This object can be single dimensional to multi-dimensional array for same data-type. “ndarray” can be relaed to python List but its working is far different. “ndarray” is fixed size object in case you wanted to increase or decrease size or shape of ndarray, NumPy will create new object and delete old object.
NumPy Use cases
- Importing large dataset
- Performing mathematical computation over large dataset
- Perform efficient way of sorting
- Random data generator for AI workflow
NumPy Installation
You can Anaconda or Python environment to install Numpy. Its very simple installation for NumPy. Just enter following command
pip install numpy
To check numpy installed successfully. Try importing module in your python code from python cli
>>> import numpy
NumPy Basic Array
Simgle dimentional array is created using following command. First import numpy as np(generally imported as np but can use other name. Then create your array.

np.zeros() and np.ones()
To create array with all zeros or ones use following command. Just provide length of array. I am using Jupyter notebook for simplicity but same commands can be run from python CLI or python script.

By default zeros() and ones() function creating array with float type but one can use following function to create integer. Convert array with argument dtype=np.int64

np.arange()
Create sequential array using “arrange” function. This will create integer starting from 0(zero) till incremented by 1(one).

Arange function can be used with step element. if want to create array with from 1000(including) till 10000(excluding). We can use following command. First argument is low number it was included, second no is high number which is excluding and step function how many steps we are jumping. To get even no, stepping 2.

np.linspace()
use np.linspace()
to create an array with values that are spaced linearly in a specified interval. This function will create equal space between first arg and second arg in regular interval. First and last no are including in interval.

np.random.rand()
Create random array between Zero(0) and One(1). This will create float random array.

np.random.randint()
Create random array for integer value. Low number is including and high number is excluding. Size is array size. It can be single dimentional or multi dimentational array.

NumPy N-dimensional Array
Create two dimensional array using numpy.

Create N-dimentional integer array –

np.reshape()
Reshape commands will change array shape. Please ensure that array should have same numbers of elements with target shape. For example. If we need to create 4×3 array source array should have at exactly 12 element. For 4×4 target array source need to have 16 element.

Getting shape and size of Array
Below functions are attribute of array and not function.
ndarray.size
To get total no of element in the array :

ndarray.shape
to get current shape for an array. This parameter come very handy when array or matrix multiplication is performed.

ndarray.ndim
to get current array dimension

Sorting and Joining Arrays
np.sort()
np.sort() is simple way to sort any NumPy array.

np.concatenate()
np.concatenate used to concatenate array. You can concatenate only same dimensional or shape array –

2-D addition concatenation

np.append()
To add element at the end of array

np.delete()
To delete element from array –

np.flip()
To reverse array use np.flip()

Indexing and slicing
NumPy arrays are working same as python list. Please find bellow examples. Indexes starts from Zero.

Stored sliced array into new array to use sliced array –

To update value in element use indexes for that –

N-D Indexing
Indexing for multidimensional works same as 1D. Just use tuple each dimension. For Example, for 2D use arr_name[3,4] to get 4th row and 5th element.

Normally, if not specified output from column is single array. Use reshape command to get output properly stored on respective row/column format.

Arithmetic Manipulation of array Elements
Main usecase for NumPy is arithmetic manupulation. NumPy makes this very easy.
In below example, we are adding 5 to each element in the array. This operation is performed to all element in array. This does not make change to existing array if you want to make change perform assignment. Again, as I mentioned earlier, this will assign new memory.

Boolean Operation
NumPy make it easier for boolean operation. Boolean operation will create array of all element which satisfy operation. In this case, b[b>5] will sent list of all elements in a array.

np.nonzero()
To get list of all indices where condition is satisfied use np.nonzero()

Arithmetic Manipulation of Array
To add and subtract array only works on same shape of array. If shape is not same it will not work.

As long as columns are matching newly added matrix will be added to all rows.

Matrix axis definition
Each array has different axis. In two dimensional array –
- For Column : axis=0
- For Rows : axis=1
As per below example if you select axis=0 that means it will return value of maximum value from appropriate rows and columns.

Conclusion
NumPy is very good library for large number of dataset and can be useful tool for data manipulation with minimum code. We can perform same task from python but with NumPy library its easy.