[box type="note" align="" class="" width=""]This article is an excerpt from a book co-authored by L. Felipe Martins, Ruben Oliva Ramos and V Kishore Ayyadevara titled SciPy Recipes. This book includes hands-on recipes for using different components of the SciPy Stack such as NumPy, SciPy, matplotlib, pandas, etc.[/box]
In this article, we will discuss how to leverage the power of SciPy and NumPy to perform numerous matrix operations and solve common challenges faced while proceeding with statistical analysis.
Basic matrix operations form the backbone of quite a few statistical analyses—for example, neural networks. In this section, we will be covering some of the most used operations and functions on 2D arrays:
In the following sections, we will look into the methods of implementing each of them in Python using SciPy/NumPy.
Let's look at the different methods.
In order to understand how matrix addition is done, we will first initialize two arrays:
# Initializing an array
x = np.array([[1, 1], [2, 2]])
y = np.array([[10, 10], [20, 20]])
Similar to what we saw in a previous chapter, we initialize a 2 x 2 array by using the np.array function. There are two methods by which we can add two arrays.
A simple addition of the two arrays x and y can be performed as follows:
x+y
Note that x evaluates to:
[[1 1]
[2 2]]
y evaluates to:
[[10 10]
[20 20]]
The result of x+y would be equal to:
[[1+10 1+10]
[2+20 2+20]]
Finally, this gets evaluated to:
[[11 11]
[22 22]]
The same preceding operation can also be performed by using the add function in the numpy package as follows:
np.add(x,y)
Matrix multiplication by a scalar can be performed by multiplying the vector with a number. We will perform the same using the following two steps:
We perform the steps, as follows:
x = np.array([[1, 1], [2, 2]])
k*x
For example, if the scalar value k = 2, then the value of k*x translates to:
2*x
array([[2, 2],
[4, 4]])
Standard arithmetic operators can be performed on top of NumPy arrays too. The operations used most often are:
The other major arithmetic operations are similar to the addition operation we performed on two matrices in the Matrix addition section earlier:
# subtraction
x-y
array([[ -9, -9],
[-18, -18]])
# multiplication
x*y
array([[10, 10],
[40, 40]])
While performing multiplication here, there is an element to element multiplication between the two matrices and not a matrix multiplication (more on matrix multiplication in the next section):
# division
x/y
array([[ 0.1, 0.1],
[ 0.1, 0.1]])
# exponential
x**y
array([[ 1, 1],
[1048576, 1048576]], dtype=int32)
Matrix to matrix multiplication works in the following way:
Matrix A has n rows and m columns and matrix B has m rows and p columns.
The matrix operation is performed by using the built-in dot function available in NumPy as follows:
x=np.array([[1, 1], [2, 2]])
y=np.array([[10, 10], [20, 20]])
np.dot(x,y)
array([[30, 30],
[60, 60]])
array([[1*10 + 1*20, 1*10 + 1*20],
[2*10 + 2*20, 2*10 + 2*20]])
Whenever matrix multiplication happens, the number of columns in the first matrix should be equal to the number of rows in the second matrix.
Matrix transposition is performed by using the transpose function available in numpy package.
The process to generate the transpose of a matrix is as follows:
A = np.array([[1,2],[3,4]])
A.transpose()
array([[1, 3],
[2, 4]])
The transpose of a matrix with m rows and n columns would be a matrix with n rows and m columns
While we performed most of the basic arithmetic operations on top of matrices earlier, we have not performed any specialist functions within scientific computing/analysis—for example, matrix inversion, transposition, ranking of a matrix, and so on.
The other functions available within the scipy package shine through (over and above the previously discussed functions) in such a scenario where more data manipulation is required apart from the standard ones.
Matrix inversion can be performed by using the function available in scipy.linalg. The process to perform matrix inversion and its implementation in Python is as follows:
A = np.array([[1,2],[3,4]])
linalg.inv(A)
array([[-2. , 1. ],
[ 1.5, -0.5]])
We saw how to easily perform implementation of all the basic matrix operations with Python’s scientific library - SciPy.
You may check out this book SciPy Recipes to perform advanced computing tasks like Discrete Fourier Transform and K-means with the SciPy stack.