Hey guys, i hope you are doing well
In the previous blog we learn about the 1d array and today we are going to continue our chapter by learning 2d array and if you want to read about 1d array than click here.
2D array
![](https://securiumsolutions.com/blog/wp-content/uploads/2019/10/arrayrt-2.png)
2d array or two-dimensional array if simply i said it’s a matrix, i which data is stored in rows and columns, 2d array is a collection of data which is arrange in the form of the matrix.
so, in this blog we are going to perform some operations on 2d array or we can say matrix like creating the matrix, learn how to find its transpose and lot of things, let’s start
*Remember to import numpy before creating the array
Create 2D array
![](https://securiumsolutions.com/blog/wp-content/uploads/2019/10/Screenshot-110-2.png)
Here i create a 2D array or a matrix, named as g
, and here you can see i use different brackets in the inner most so, we can use either bracket ()/[]
in inner most, output will be the same.
Output
![](https://securiumsolutions.com/blog/wp-content/uploads/2019/10/Screenshot-111-1.png)
ndim, size, shape
- ndim– ndim is used to find the dimension of an array.
- size– size keyword is used to find the size of an array, it told the no of elements present in an array/matrix.
- shape– shape is used to find the no. of rows and columns present in the 2d array.
![](https://securiumsolutions.com/blog/wp-content/uploads/2019/10/Screenshot-113.png)
Output
![](https://securiumsolutions.com/blog/wp-content/uploads/2019/10/Screenshot-114-1.png)
Create some matrix
![](https://securiumsolutions.com/blog/wp-content/uploads/2019/10/Screenshot-116.png)
Here we create the matrix with 0 element.
Output
![](https://securiumsolutions.com/blog/wp-content/uploads/2019/10/Screenshot-117-1.png)
Create a matrix with random value from 0 to 1
![](https://securiumsolutions.com/blog/wp-content/uploads/2019/10/Screenshot-119-1.png)
here we create a matrix with random numbers from 0 to 1
Output
![](https://securiumsolutions.com/blog/wp-content/uploads/2019/10/Screenshot-120-1.png)
Create some basic matrix
![](https://securiumsolutions.com/blog/wp-content/uploads/2019/10/Screenshot-125-1.png)
- Unity matrix– All elements are 1.
- Diagonal matrix– Only diagonal elements are non zero.
Output
![](https://securiumsolutions.com/blog/wp-content/uploads/2019/10/Screenshot-126-1.png)
Reshaping the 2D array
In this we are going to learn how we can convert 2D array into 1D array
![](https://securiumsolutions.com/blog/wp-content/uploads/2019/10/Screenshot-128-1.png)
Output
![](https://securiumsolutions.com/blog/wp-content/uploads/2019/10/Screenshot-129-1.png)
Modify the shape in the original array form
![](https://securiumsolutions.com/blog/wp-content/uploads/2019/10/Screenshot-131.png)
Output
![](https://securiumsolutions.com/blog/wp-content/uploads/2019/10/Screenshot-133.png)
Create some array
![](https://securiumsolutions.com/blog/wp-content/uploads/2019/10/Screenshot-135.png)
Output
![](https://securiumsolutions.com/blog/wp-content/uploads/2019/10/Screenshot-137.png)
Now we are going to perform some operations on matrix
![](https://securiumsolutions.com/blog/wp-content/uploads/2019/10/Screenshot-139.png)
In this we are going to do some basic operations on matrix like transpose of matrix, trace, inverse of matrix
Output
![](https://securiumsolutions.com/blog/wp-content/uploads/2019/10/Screenshot-140.png)
there are some more operations like
![](https://securiumsolutions.com/blog/wp-content/uploads/2019/10/Screenshot-142.png)
Output
![](https://securiumsolutions.com/blog/wp-content/uploads/2019/10/Screenshot-143.png)
Now we are going to create a 2D array or matrix on which we find some statistical values
Input
![](https://securiumsolutions.com/blog/wp-content/uploads/2019/10/Screenshot-145.png)
Output
![](https://securiumsolutions.com/blog/wp-content/uploads/2019/10/Screenshot-146.png)
so here we learn about the 2D array or Matrix, I hope you find it interesting and if you want to read some interesting blogs or articles click here in the next blog we are going to learn something really interesting so wait for it and till try this by your own.