Search

Array : 2D array

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

arrayrt 2

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

Screenshot 110 2

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

Screenshot 111 1
Here you can see our 2d array is created

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.
Screenshot 113

Output

Screenshot 114 1

Create some matrix

Screenshot 116

Here we create the matrix with 0 element.

Output

Screenshot 117 1

Create a matrix with random value from 0 to 1

Screenshot 119 1

here we create a matrix with random numbers from 0 to 1

Output

Screenshot 120 1

Create some basic matrix

Screenshot 125 1
  • Unity matrix– All elements are 1.
  • Diagonal matrix– Only diagonal elements are non zero.

Output

Screenshot 126 1

Reshaping the 2D array

In this we are going to learn how we can convert 2D array into 1D array

Screenshot 128 1

Output

Screenshot 129 1

Modify the shape in the original array form

Screenshot 131
resize(), will modify the shape in the original array

Output

Screenshot 133

Create some array

Screenshot 135

Output

Screenshot 137

Now we are going to perform some operations on matrix

Screenshot 139

In this we are going to do some basic operations on matrix like transpose of matrix, trace, inverse of matrix

Output

Screenshot 140

there are some more operations like

Screenshot 142

Output

Screenshot 143

Now we are going to create a 2D array or matrix on which we find some statistical values

Input

Screenshot 145

Output

Screenshot 146

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.

Book A Free Demo Class

    Social Media
    Facebook
    Twitter
    WhatsApp
    LinkedIn