Python has a well-supported scientific computing ecosystem called SciPy. When we installed Anaconda, we also installed all the packages we need to take advantage of SciPy. In this tutorial, we will be learning how to work with matrices using NumPy.
NumPy is a library which makes working with arrays a similar experience to using MATLAB. First, let's bring NumPy into our workspace:
import numpy as np
Now we can work with any part of the NumPy library by typing "np."
Let's declare a matrix:
mat = np.ones((5,6), int)
print(mat)
You should see an output like this:
[[1 1 1 1 1 1]
[1 1 1 1 1 1]
[1 1 1 1 1 1]
[1 1 1 1 1 1]
[1 1 1 1 1 1]]
A 5x6 matrix consisting entirely of 1s. The int parameter tells NumPy to make all the entries of the array integers. We can omit this, as it is an optional parameter. If we did, we would have a matrix of floats, which are decimal numbers.
The first parameter is a tuple, which is a grouping of two pieces of data. In this case, the (5,6) defines the shape of our matrix: 5 rows and 6 columns.
NumPy has all sorts of handy ways to create an array:
https://docs.scipy.org/doc/numpy/reference/routines.array-creation.html
We can use most of the usual MATLAB commands for working with arrays:
Adding a constant
mat = mat + 1
Division
mat = mat / 2
We can also access specific parts of our matrix:
This pulls out the entire second column:
mat[:,2]
This pulls out the entire second row:
mat[2,:]
We can operate on just this part of the matrix too:
mat[2,:] = mat[2,:] * 10
NumPy allows us to get pretty clever with indexing:
mat[:3,:]
This commands pulls out rows 0, 1, and 2 for mat. We can also pull out from row 3 onwards:
mat[3:,:]
This is called "slicing," and you can do in in any dimension of your arrays. You can even combine them:
mat[:3,:2]
This will pull out rows 0, 1, and 2, as well as columns 0 and 1. So if our matrix looked liked this:
[[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4]]
Then mat[:3,:2] looks like this:
[[0 1]
[0 1]
[0 1]]
A few other useful commands for NumPy arrays are:
Transpose array x:
np.transpose(x)
Size and shape:
mat.size
Returns the size of the matrix (number of rows * number of columns)
mat.shape
Returns the shape of the array as a tuple (eg., (5,6)).
This is of course just the tip of the iceberg. NumPy is a very capable library, with options for loading arrays straight from files, and manipulating arrays in sophisticated ways. The NumPy User Guide is helpful to get started with NumPy, and the reference documentation fills in the details when you need it:
User Guide
https://docs.scipy.org/doc/numpy/user/index.html#user
Reference Documentation
https://docs.scipy.org/doc/numpy/reference/index.html#reference
NumPy is a library which makes working with arrays a similar experience to using MATLAB. First, let's bring NumPy into our workspace:
import numpy as np
Now we can work with any part of the NumPy library by typing "np."
Let's declare a matrix:
mat = np.ones((5,6), int)
print(mat)
You should see an output like this:
[[1 1 1 1 1 1]
[1 1 1 1 1 1]
[1 1 1 1 1 1]
[1 1 1 1 1 1]
[1 1 1 1 1 1]]
A 5x6 matrix consisting entirely of 1s. The int parameter tells NumPy to make all the entries of the array integers. We can omit this, as it is an optional parameter. If we did, we would have a matrix of floats, which are decimal numbers.
The first parameter is a tuple, which is a grouping of two pieces of data. In this case, the (5,6) defines the shape of our matrix: 5 rows and 6 columns.
NumPy has all sorts of handy ways to create an array:
https://docs.scipy.org/doc/numpy/reference/routines.array-creation.html
We can use most of the usual MATLAB commands for working with arrays:
Adding a constant
mat = mat + 1
Division
mat = mat / 2
We can also access specific parts of our matrix:
This pulls out the entire second column:
mat[:,2]
This pulls out the entire second row:
mat[2,:]
We can operate on just this part of the matrix too:
mat[2,:] = mat[2,:] * 10
NumPy allows us to get pretty clever with indexing:
mat[:3,:]
This commands pulls out rows 0, 1, and 2 for mat. We can also pull out from row 3 onwards:
mat[3:,:]
This is called "slicing," and you can do in in any dimension of your arrays. You can even combine them:
mat[:3,:2]
This will pull out rows 0, 1, and 2, as well as columns 0 and 1. So if our matrix looked liked this:
[[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4]]
Then mat[:3,:2] looks like this:
[[0 1]
[0 1]
[0 1]]
A few other useful commands for NumPy arrays are:
Transpose array x:
np.transpose(x)
Size and shape:
mat.size
Returns the size of the matrix (number of rows * number of columns)
mat.shape
Returns the shape of the array as a tuple (eg., (5,6)).
This is of course just the tip of the iceberg. NumPy is a very capable library, with options for loading arrays straight from files, and manipulating arrays in sophisticated ways. The NumPy User Guide is helpful to get started with NumPy, and the reference documentation fills in the details when you need it:
User Guide
https://docs.scipy.org/doc/numpy/user/index.html#user
Reference Documentation
https://docs.scipy.org/doc/numpy/reference/index.html#reference