#31 Python Tutorial for Beginners | Working with Matrix in Python
Introduction to Multi-Dimensional Arrays in NumPy
In this video, the instructor introduces multi-dimensional arrays in NumPy and explains how to create and manipulate them.
Creating a Two-Dimensional Array
- A two-dimensional array is created by adding two arrays inside one big array.
- The
dtypeattribute can be used to determine the type of data in the array.
- The
ndimattribute can be used to determine the number of dimensions or rank of the array.
- The
shapeattribute returns a tuple with the number of rows and columns in the array.
Manipulating Multi-Dimensional Arrays
- The
sizeattribute returns the total number of elements in the array.
- The
flatten()function can be used to convert a multi-dimensional array into a one-dimensional array.
- To convert a one-dimensional array into a three-dimensional array, additional values must be added to it first. Then, reshape it using
.reshape()method.
Conclusion
In this video, we learned about creating and manipulating multi-dimensional arrays in NumPy using Python programming language.
Creating 2D and 3D Arrays
In this section, the speaker explains how to create 2D and 3D arrays in Python.
Creating a 2D Array
- To create a 2D array, you need to pass the number of rows and columns as arguments.
- For example, if you want a matrix with three rows and four columns, you would pass
3for the number of rows and4for the number of columns.
- Use the
numpy.array()function to create a 2D array in Python.
Creating a 3D Array
- To create a 3D array, you need to pass three arguments: the number of matrices, the number of rows per matrix, and the number of columns per matrix.
- For example, if you want a big array with two matrices that each have two rows and three columns, you would pass
2for the number of matrices,2for the number of rows per matrix, and3for the number of columns per matrix.
- Use the
numpy.array()function to create a 3D array in Python.
Matrices in Mathematics
In this section, the speaker discusses matrices in mathematics.
What is Matrices?
- A matrix is a two-dimensional array that has multiple rows and multiple columns.
- You can represent one row or one column as an array.
Diagonal Elements
- Diagonal elements are those elements where row index is equal to column index.
- You can extract diagonal elements from any given matrix using numpy.diag() function.
Matrices in Python
In this section, the speaker explains how to use matrices in Python. They cover the concepts of minimum and maximum elements, adding two matrices, and multiplying two matrices.
Using Min and Max Functions with Matrices
- The
minfunction returns the minimum element of a matrix.
- The
maxfunction returns the maximum element of a matrix.
Adding Two Matrices
- To add two matrices, simply add their corresponding elements together.
Multiplying Two Matrices
- Multiplying two matrices is more complex than adding them.
- Watch the speaker's video on how to multiply two matrices for an explanation of the logic behind it.
- In Python, you can use the
*operator to multiply two matrices together automatically.
- Manually multiplying two matrices requires using row and column combinations.
Conclusion
Matrices are powerful tools in programming that allow for complex calculations. By understanding how to use them in Python, you can simplify your code and perform advanced operations with ease.