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
1. NumPy Arrays
Let us create a list.
my_list=[1,2,3]
Now call np.array to craete a NumPy array with the list.
arr=np.array(my_list)
And type arr to see the array.
arr
We can create this array by directly putting the values like:
arr=np.array([1,2,3])
The array we have created was an one dimensional array. We can create two dimensional array. Let's create a three by three array.
mt=[[1,2,3],[4,5,6],[7,8,9]]
np.array(mt)
Let's create a sequence starting at zero, step size or difference is 2, to 11
np.arange(0,11,2)
Here, we are creating a five by five dimensional array containing only zeros:
np.zeros((5,5))
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).
np.random.rand(5,5)
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.
ranarr.argmax()
2.NumPy Indexing and Selection
Let us create a two dimensional array first.
arr=np.array([[5,10,15],[20,25,30],[35,40,45]])
arr
and we get
array([[ 5, 10, 15],
[20, 25, 30],
[35, 40, 45]])
If we want to get the value 10 we type
arr_2d[0][1]
In python, indexing starts from 0, so if we want to grab except the third row we type, because third row indexed as 2.
arr_2d[:2]
3. NumPy Operations
Let's do some arithmatic operations with numpy
ar=np.arange(1,11)
Addition:
ar+ar
Substraction: Substract array from a array and substract a scaler from array
ar-ar
ar-4
Multiplication:
ar*3
4. Universal Array Function
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
np.sqrt(ar)
Taking exponential of each values of the array:
np.exp(ar)
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]
Happy Learning!<3
Top comments (4)
Keep up for your writing.
A suggestion: You can highlight the code blocks in the article markdownguide.org/extended-syntax/...
for python code, just add
py
after the opening triple quoteThank you so much. I would try this.
great... keep it up apu.
Thanks for your support!