In python, the library which is used to create and manipulates matrices is NumPy. Basically, NumPy works with arrays. For instances array is an ordered series or arrangement of numbers/objects. NumPy stands for "Numerical Python".
Let's see how can we use Numpy to work with matrices.
First import NumPy module with running the following code.
import numpy as np
Let us create a list.
Now call np.array to craete a NumPy array with the list.
And type arr to see the array.
We can create this array by directly putting the values like:
The array we have created was an one dimensional array. We can create two dimensional array. Let's create a three by three array.
Let's create a sequence starting at zero, step size or difference is 2, to 11
Here, we are creating a five by five dimensional array containing only zeros:
We can also create matrics having only value 1, identity matrics etc (example are given in the notebook).
We can create array with values from a specific distribution such as standard normal distribution, uniform distribution, poisson distribution, binomial distribution, chi square distribution etc. For example we can create a two dimensional array with sample from uniform distribution(0 to 1).
We can find the maximum or minimum values is the array.
#array returning random integers ranarr=np.random.randint(0,50,10) ranarr.max()
While returning the maximum value of the array we can also find the location for the maximum value.
Let us create a two dimensional array first.
and we get
array([[ 5, 10, 15],
[20, 25, 30],
[35, 40, 45]])
If we want to get the value 10 we type
In python, indexing starts from 0, so if we want to grab except the third row we type, because third row indexed as 2.
Let's do some arithmatic operations with numpy
Substraction: Substract array from a array and substract a scaler from array
Universal Functions used to implement vectorization in NumPy which is more faster than iterating over elements. For example:
To find a array containing square root of each values in array we type
Taking exponential of each values of the array:
To find a product of arrays:
a= np.array([1, 2, 3, 4]) b = np.array([5, 6, 7, 8]) np.prod([a, b])
You can practice more example at your own. The notebook link is given below. Go to the link and practice.
Notebook Link: [https://www.kaggle.com/code/azizaafrin/python-numpy]