If you want to read why you should learn linear algebra or SciPy for data science or which NumPy functions are useful when you’re working with SciPy, check out the full tutorial .
Now that you know what you need to use both packages to your advantage, it’s time to dig into the topic of this tutorial: linear algebra.
Prepping Your Workspace: InstallSciPyBut before you go into how you can use python, make sure that your workspace is completely ready!
Of course you first need to make sure firstly that you have Python installed. Go to this page if you still need to do this:) If you’re working on windows, make sure that you have added Python to the PATH environment variable. In addition, don’t forget to install a package manager, such as pip , which will ensure that you’re able to use Python’s open-source libraries.
Note that recent versions of Python 3 come with pip, so double check if you have it and if you do, upgrade it before you install any other packages:
pip install pip --upgrade pip --versionBut just installing a package manager is not enough; You also need to download to download the wheel for the library: go here to get your SciPy wheel. After the download, open up the terminal at the download directory on your pc and install it. Additionally, you can check whether the installation was successful and confirm the package version that you’re running:
# Install the wheel install "scipy0.18.1cp36cp36mwin_amd64.whl" # Confirm successful install import scipy # Check package version scipy.__version__After these steps, you’re ready to goy!
Tip: install the package by downloading the Anaconda Python distribution. It’s an easy way to get started quickly, as Anaconda not only includes 100 of the most popular]( https://docs.continuum.io/anaconda/pkg-docs ) Python, R and Scala packages for data science, but also includes several open course development environments such as Jupyter and Spyder. If you’d like to start working with Jupyter Notebook, check out this Jupyter notebook tutorial .
If you haven’t downloaded it already, go here to get it.
Linear Algebra: Vectors &MatricesNow that you have made sure that your workspace is prepped, you can finally get started with linear algebra in Python. In essence, this discipline is occupied with the study of vector spaces and the linear mappings that exist between them. These linear mappings can be described with matrices, which also makes it easier to calculate.
Rememberthat a vector space is a fundamental concept in linear algebra. It’s a space where you have a collection of objects (vectors) and where you can add or scale two vectors without the resulting vector leaving the space. Remember also that vectors are rows (or columns) of a matrix.
But how does this work in Python?
You can easily create a vector with the np.array() function. Similarly, you can give a matrix structure to every one-or two-dimensional ndarray with either the np.matrix() or np.mat() commands.
So arrays and matrices are the same, besides from the formatting?
Well, not exactly. There are some differences:
A matrix is 2-D, while arrays are usually n -D, As the functions above already implied, the matrix is a subclass of ndarray , Both arrays and matrices have .T() , but only matrices have .H() and .I() , Matrix multiplication works differently from element-wise array multiplication, and To add to this, the ** operation has different results for matrices and arraysWhen you’re working with matrices, you might sometimes have some in which most of the elements are zero. These matrices are called “sparse matrices”, while the ones that have mostly non-zero elements are called “dense matrices”.
In itself, this seems trivial, but when you’re working with SciPy for linear algebra, this can sometimes make a difference in the modules that you use to get certain things done. More concretely, you can use scipy.linalg for dense matrices, but when you’re working with sparse matrices, you might also want to consider checking up on the scipy.sparse module, which also contains its own scipy.sparse.linalg .
For sparse matrices, there are quite a number of options to create them. Go here to read about all the options.
There are really a lot of options, but which one should you choose if you’re making a sparse matrix yourself?
It’s not that hard.
Basically, it boils down to first is how you’re going to initialize it. Next, consider what you want to be doing with your sparse matrix.
More concretely, you can go through the following checklist to decide what type of sparse matrix you want to use:
If you plan to fill the matrix with numbers one by one, pick a coo_matrix() or dok_matrix() to create your matrix. If you want to initialize the matrix with an array as the diagonal, pick dia_matrix() to initialize your matrix. For sliced-based matrices, use lil_matrix() . If you’re constructing the matrix from blocks of smaller matrices, consider using bsr_matrix() . If you want to have fast access to your rows and columns, convert your matrices by using the csr_matrix() and csc_matrix() functions, respectively. The last two functions are not great to pick when you need to initialize your matrices, but when you’re multiplying, you’ll definitely notice the difference in speed.Easy peasy!
Vector OperationsNow that you have learned or refreshed the difference between vectors, dense matrices and sparse matrices, it’s time to take a closer look at vectors and what kind of mathematical operations you can do with them. The tutorial focuses here explicitly on mathematical operations so that you’ll come to see the similarities and differences with matrices, and because a huge part of linear algebra is, ultimately, working with matrices.
You have already seen that you can easily create a vector with np.array() . But now that you have vectors at your disposal, you might also want to know of some basic operations that can be performed on them.
Now that you have successfully seen some vector operations, it’s time to get started on to the real matrix work!
Matrices: Operations &RoutinesSimilarly to where you left it off at the start of the previous section, you know how to create matrices, but you don’t know yet how you can use them to your advantage. This section will provide you with an overview of some matrix functions and basic matrix routines that you can use to work efficiently.
F