A matrix is a two-dimensional data structure where numbers are arranged into rows and columns. For example: Show This matrix is a 3x4 (pronounced "three by four") matrix because it has 3 rows and 4 columns. Python doesn't have a built-in type for matrices. However, we can treat a list of a list as a matrix. For example:
We can treat this list of a list as a matrix having 2 rows and 3 columns. Be sure to learn about Python lists before proceed this article. Let's see how to work with a nested list.
When we run the program, the output will be: A = [[1, 4, 5, 12], [-5, 8, 9, 0], [-6, 7, 11, 19]] A[1] = [-5, 8, 9, 0] A[1][2] = 9 A[0][-1] = 12 3rd column = [5, 9, 11] Here are few more examples related to Python matrices using nested lists.
Using nested lists as a matrix works for simple computational tasks, however, there is a better way of working with matrices in Python using NumPy package. NumPy ArrayNumPy is a package for scientific computing which has support for a powerful N-dimensional array object. Before you can use NumPy, you need to install it. For more info,
Once NumPy is installed, you can import and use it. NumPy provides multidimensional array of numbers (which is actually an object). Let's take an example:
As you can see, NumPy's array class is called How to create a NumPy array?There are several ways to create NumPy arrays. 1. Array of integers, floats and complex Numbers
When you run the program, the output will be: [[1 2 3] [3 4 5]] [[1.1 2. 3. ] [3. 4. 5. ]] [[1.+0.j 2.+0.j 3.+0.j] [3.+0.j 4.+0.j 5.+0.j]] 2. Array of zeros and ones
Here, we have specified 3. Using arange() and shape()
Learn more about other ways of creating a NumPy array. Matrix OperationsAbove, we gave you 3 examples: addition of two matrices, multiplication of two matrices and transpose of a matrix. We used nested lists before to write those programs. Let's see how we can do the same task using NumPy array. Addition of Two Matrices We use
Multiplication of Two Matrices To multiply two
matrices, we use Note:
Transpose of a Matrix We use numpy.transpose to compute transpose of a matrix.
As you can see, NumPy made our task much easier. Access matrix elements, rows and columnsAccess matrix elements Similar like lists, we can access matrix elements using index. Let's start with a one-dimensional NumPy array.
When you run the program, the output will be: A[0] = 2 A[2] = 6 A[-1] = 10 Now, let's see how we can access elements of a two-dimensional array (which is basically a matrix).
When we run the program, the output will be: A[0][0] = 1 A[1][2] = 9 A[-1][-1] = 19 Access rows of a Matrix
When we run the program, the output will be: A[0] = [1, 4, 5, 12] A[2] = [-6, 7, 11, 19] A[-1] = [-6, 7, 11, 19] Access columns of a Matrix
When we run the program, the output will be: A[:,0] = [ 1 -5 -6] A[:,3] = [12 0 19] A[:,-1] = [12 0 19] If you don't know how this above code works, read slicing of a matrix section of this article. Slicing of a MatrixSlicing of a one-dimensional NumPy array is similar to a list. If you don't know how slicing for a list works, visit Understanding Python's slice notation. Let's take an example:
Now, let's see how we can slice a matrix.
As you can see, using NumPy (instead of nested lists) makes it a lot easier to work with matrices, and we haven't even scratched the basics. We suggest you to explore NumPy package in detail especially if you trying to use Python for data science/analytics. NumPy Resources you might find helpful:
|