Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon

Implementing matrix operations using SciPy and NumPy

Save for later
  • 5 min read
  • 07 Mar 2018

article-image

[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.

Matrix operations and functions on two-dimensional arrays

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:

  • Addition
  • Multiplication by scalar
  • Matrix arithmetic
  • Matrix-matrix multiplication
  • Matrix inversion
  • Matrix transposition

In the following sections, we will look into the methods of implementing each of them in Python using SciPy/NumPy.

How to do it

Let's look at the different methods.

Matrix addition

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.

Method 1

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]]
Method 2

The same preceding operation can also be performed by using the add function in the numpy package as follows:

np.add(x,y)

Multiplication by a scalar

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:

  1. Initialize a two-dimensional array.
  2. Multiply the two-dimensional array with a scalar.

We perform the steps, as follows:

  1. To initialize a two-dimensional array:
x = np.array([[1, 1], [2, 2]])
  1. To multiply the two-dimensional array with the k scalar:
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]])

Matrix arithmetic

Standard arithmetic operators can be performed on top of NumPy arrays too. The operations used most often are:

  • Addition
  • Subtraction
  • Multiplication
  • Division
  • Exponentials

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-matrix multiplication

Matrix to matrix multiplication works in the following way:

Unlock access to the largest independent learning library in Tech for FREE!
Get unlimited access to 7500+ expert-authored eBooks and video courses covering every tech area you can think of.
Renews at AU $24.99/month. Cancel anytime
  1. We have a set of two matrices with the following shape:

implementing-matrix-operations-using-scipy-numpy-img-0

Matrix A has n rows and m columns and matrix B has m rows and p columns.

  1. The matrix multiplication of A and B is calculated as follows:

implementing-matrix-operations-using-scipy-numpy-img-1

The matrix operation is performed by using the built-in dot function available in NumPy as follows:

  1. Initialize the arrays:
x=np.array([[1, 1], [2, 2]])

y=np.array([[10, 10], [20, 20]])
  1. Perform the matrix multiplication using the dot function in the numpy package:
np.dot(x,y)

array([[30, 30],

[60, 60]])
  1. The np.dot function does the multiplication in the following way:
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

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:

  1. Initialize a matrix:
A = np.array([[1,2],[3,4]])
  1. Calculate the transpose of the matrix:
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

Matrix inversion

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:

  1. Import relevant packages and classes/functions within a package: from scipy import linalg
  1. Initialize a matrix:
A = np.array([[1,2],[3,4]])
  1. Pass the initialized matrix through the inverse function in package:
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.

implementing-matrix-operations-using-scipy-numpy-img-2