Scipy Linalg – Helpful Guide

In this Python tutorial, we will learn about the “Scipy Linalg“. Here we will learn how to solve linear algebra problems. And additionally, we will cover the following topics.

  • Scipy Linalg
  • Scipy Linalg Eig
  • Scipy Linalg Eigh
  • Scipy Linalg Lu
  • Scipy Linalg Svd.
  • Scipy Linalg Solve
  • Scipy Linalg Qr
  • Scipy Linalg Inverse
  • Scipy Linalg Norm
  • Scipy Linalg Cholesky

Scipy Linalg

The scipy.linalg module in Python Scipy is used to solve linear algebra problems.  We can use this library to solve equations, matrices, eigenvalues, and special matrix problems, among other things.

It has a variety of approaches for solving problems in linear algebra. All of the methods in the linalg the library has been divided into the following sections:

  • Eigenvalue Problems
  • Matrix Functions
  • Basics
  • Decompositions
  • Sketches and Random Projections
  • Low-Level Routines
  • Matrix Equation Solvers
  • Special Matrices

In the upcoming subsection, we will learn about the most common methods of the module scipy.linalg.

Scipy Linalg Eig

The Python Scipy has a method eig() within module scipy.linalg to deal with common eigenvalue problems.

The syntax is given below.

scipy.linalg.eig(a, b=None, right=False, left=True, overwrite_a=True, overwrite_b=True, check_finite=False, homogeneous_eigvals=True)

Where parameters are:

  • a(array_data): To provide the real or complex matrix as input for computing the eigenvector and eigenvalues.
  • b(array_data): To input the right-hand side matrix.
  • right(boolean): To compute and get the right eigenvectors.
  • left(boolean): To compute and get the left eigenvectors.
  • overwrite_a(boolean): To overwrite a.
  • overwrite_b(boolean): To overwrite b.
  • check_finite(boolean): To check if the provided matrix as input has finite numbers.
  • homogeneous_eigvals(boolean): To get the eigenvalues in homogenous coordinates

The method eig() returns the w(the eigenvalues), vr(the right vector that is normalized) and vl(the left vector that is normalized) of type complex ndarray or double

Let’s take an example by following the below steps:

Import the required libraries using the below python code.

import numpy as np
from scipy.linalg import eig

Create an array of data as a matrix using the below code.

mat_data = np.array([[-1.,0],[0.2,1]])

Pass the created matrix data to the method eig() using the below code.

eig(mat_data)
Scipy Linalg Eig
Scipy Linalg Eig

This is how to compute the eigenvalues and eigenvectors using the method eig() of Python Scipy.

Read: Scipy Convolve

Scipy Linalg Eigh

The Python Scipy has a method eigh() within module scipy.linalg to deal with standard ordinary eigenvalue problems for real symmetric or Hermitian matrices.

The syntax is given below.

scipy.linalg.eigh(a, b=None, eigvals_only=True, lower=False, overwrite_a=True, overwrite_b=True, turbo=False, eigvals=None, type=1, check_finite=False, subset_by_index=None, subset_by_value=None, driver=None)

Where parameters are:

  • a(array_data): To provide the real or complex matrix as input for computing the eigenvector and eigenvalues.
  • b(array_data): To input the right-hand side matrix.
  • lower(bool): Whether the relevant array of data is chosen from the lower or upper triangle of a.
  • egvals_only(boolean): To compute only the eigenvalues with no eigenvectors. By default, both are calculated.
  • overwrite_a(boolean): To overwrite a.
  • overwrite_b(boolean): To overwrite b.
  • check_finite(boolean): To check if the provided matrix as input has finite numbers.
  • subset_by_index(iterable): To define the beginning and end eigenvalues indices using a two-element that is iterable.
  • subset_by_value(iterable): To define the half interval to get only eigenvalues using a two-element that is iterable.

The method eigh() returns the w(selected eigenvalues) in increasing size of type ndarray.

Let’s an example by following the below steps:

Import the required library using the below python code.

import numpy as np
from scipy.linalg import eigh

Create an array of data as a matrix using the below code.

matrix_data = np.array([[5, 3, 6, 1], [5, 1, 3, 0], [2, 6, 5, 1], [1, 5, 2, 2]])

Pass the created matrix data to the method eigh() using the below code.

eigh(matrix_data)
Scipy Linalg Eigh
Scipy Linalg Eigh

This is how to compute the eigenvalues using the method eigh() of Python Scipy.

Read: Scipy Ndimage Rotate

Scipy Linalg Lu

The Python Scipy has a method lu() within module scipy.linalg to calculate the pivoted Lu decomposition of the matrix.

The syntax is given below.

scipy.linalg.lu(a, permute_l=True, check_finite=False, overwrite_a=True,)

Where parameters are:

  • a(array_data): To decompose the array.
  • permute_l (boolean): To do the multiplication
  • overwrite_a (boolean): to overwrite a.
  • check_finit(boolean): To check if the provided matrix as input has finite numbers.

The method returns p(Permutation matrix), l(Lower triangular), u(Upper triangular) in case of permute_l equal to false and pl(Permutation L matrix), u(Upper triangular) in case of permute_l equal to true of type ndarray.

Let’s understand with an example by following the below steps:

Import the required library using the below python code.

import numpy as np
from scipy.linalg import lu

Create an array of data using the below code.

array_data = np.array([[8, 7, 2, 5], [2, 8, 5, 2], [6, 6, 5, 7], [4, 5, 8, 4]])

Pass the created array of data to the method lu() using the below code.

permute_mat,lower_tri,upper_tri = lu(array_data)

print('Permutation matrix',permute_mat)
print('Lower triangular',lower_tri)
print('Upper triangular',upper_tri)
Scipy Linalg Lu
Scipy Linalg Lu

Looking at the output, the results are Permutaton matrix, Lower triangular and Upper triangular.

This is how to use the method lu() for Lu decomposition of a matrix.

Read: Scipy Normal Distribution

Scipy Linalg Svd

The Python Scipy has a method svd() within module scipy.linalg for singular decomposition value.

The syntax is given below.

scipy.linalg.svd(a, compute_uv=False, full_matrices=False, overwrite_a=True, check_finite=False, lapack_driver='gesdd')

Where parameters are:

  • a(array_data): It is matrix for decomposing.
  • compute_uv(boolean): To calculate v and uh besides s.
  • full_matrices(boolean): For true value, the shape of v and uh are (M, M) (N, N) and for false the shape are (M, K) and (K, N).
  • overwrite_a(boolean): To overwrite a.
  • check_finite(boolean): To check if the provided matrix as input has finite numbers.
  • lapack_driver(gesvd, gesdd): The more efficient general rectangular approach or divide-and-conquer approach is used.

The method svd() returns U( left singular vectors of Unitary matrix), s(singular values) and vh(right singular vectors of Unitary matrix) of type ndarray.

Let’s take an example by following the below steps:

Import the required library using the below python code.

from numpy.random import default_rng
from scipy.linalg import svd

Generate an array of data using the random generator default_rng using the below code.

random_numgen = default_rng()
m, n = 12, 8
array_data = random_numgen.standard_normal((m, n)) + 1.j*random_numgen.standard_normal((m, n))

Perform the svd on the array of data using the below code.

U, s, Vh = svd(array_data)
print("left singular vectors of Unitary matrix",U.shape)
print("singular values",s)
print("right singular vectors of Unitary matrix",Vh.shape)
Scipy Linalg Svd
Scipy Linalg Svd

This is how to use the method svd() for singular decomposition of the array.

Read: Scipy Stats Zscore

Scipy Linalg Solve

The Python Scipy has a method solve() within module scipy.linalg to deal with problems related to the linear equations such as ax = b which means this equation is solved for the value x of square matrix a.

The syntax is given below.

scipy.linalg.solve(a, b, overwrite_a=True, lower=True, debug=None, overwrite_b=True, sym_pos=True, assume_a='gen', check_finite=False, transposed=True)

Where parameters are:

  • a(array_data): To accept the square data as input.
  • b(array_data): To input the right-hand side of the data.
  • lower(bool): Whether the relevant array of data is chosen from the lower or upper triangle of a.
  • overwrite_a(boolean): To overwrite a.
  • overwrite_b(boolean): To overwrite b.
  • check_finite(boolean): To check if the provided matrix as input has finite numbers.
  • sym_post(boolean): Instead of sym_pos use the parameter assume_a = pos because sym_pos is depreciated. This parameter considers the provided value of the parameter a as asymmetric and positive definite.
  • What value it should contain is explained in the above parameter.
  • transposed (boolean): if it is true, then the a is transposed, such asa*T x = b.

The method solve returns x(which is a solution array) of type ndarray and error that occurred.

Let’s take an example by following the below steps:

Import the required libraries using the below python code.

from scipy.linalg import solve
import numpy as np

Given the value of x and y, find the value of z, and first, create the array of data for x and y using the below code.

x = np.array([[4, 3, 0], [2, -2, 0], [1, 0, 5]])
y = np.array([1, 3, -4])

Solve the x and y for z using the below code.

solve(x,y)
Scipy Linalg Solve
Scipy Linalg Solve

This is how to use the method solve() to solve linear equation problems.

Read: Scipy Signal

Scipy Linalg Qr

We will talk about QR decomposition or QR factorization of a matrix. A QR factorization is the decomposition of a matrix, such as ‘A,’ into ‘A=QR,’ where R is an upper-triangular matrix and Q is orthogonal. We factorize the matrix with the Python SciPy method scipy.linalg.qr() function.

The syntax is given below.

scipy.linalg.qr(a, overwrite_a=True, lwork=None, mode='full', pivoting=True, check_finite=False)

Where parameters are:

  • a(array_data): Input the matrix that we want to be decomposed.
  • overwrite_a(boolean): To overwrite a.
  • check_finite(boolean): To check if the provided matrix as input has finite numbers.
  • lwork(int): Size of the work array, lwork >= a.shape[1]. If None or -1 is specified, the optimal size is determined.
  • mode: Determines what data will be returned either both Q and R (‘full’, which is the default), only R (‘r’), or both Q and R but computed in economy-size.
  • pivoting(boolean): Whether or not pivoting should be included in factorization for rank-revealing qr decomposition. If pivoting, perform the decomposition A P = Q R as described above, but with P chosen so that the diagonal of R is not increasing.

The method returns the Q( which is shape as M, M), R(shape of M, N) and P(shape of N) of type ndarray or float or int.

Let’s take an example by following the below steps:

Import the required libraries using the below python code.

import numpy as np
from scipy.linalg import qr

Create an object of a random number generator and generate a sample from a normal distribution using the below code.

random_gen = np.random.default_rng()
data = random_gen.standard_normal((7, 5))
data

Perform the qr on the data and check the shape using the below code.

shape_q, shape_r = qr(data)
print('Shape of q',shape_q.shape)
print('Shape of r',shape_r.shape)
Scipy Linalg Qr
Scipy Linalg Qr

This is how to use the method qr() of Python Scipy for QR decomposition.

Read: Scipy Misc + Examples

Scipy Linalg inverse

Python SciPy contains a method inv() in a module sicpy.linalg that determines the inverse of the matrix.

The syntax is given below.

scipy.linalg.inv(a, overwrite_a=True, check_finite=False)

Where parameters are:

  • a(array_data): Input the matrix that we want to be inverted.
  • overwrite_a(boolean): To overwrite a.
  • check_finite(boolean): To check if the provided matrix as input has finite numbers.

The method inv() returns the inv (which inverse of a matrix).

Let’s take an example by following the below steps:

Import the required libraries using the below code.

from scipy.linalg import inv
import numpy as np

Create a matrix and inverse it using the below code.

mat = np.array([[2., 1.], [4., 3.]])
inv(mat)
Scipy Linalg Inverse
Scipy Linalg Inverse

This is how to inverse the given matrix using the method inv() of Python SciPy.

Read: Scipy Sparse – Helpful Tutorial

Scipy Linalg Norm

Python SciPy has a method norm() that returns matrix norms from 8 different forms.

The syntax is given below.

scipy.linalg.norm(a, axis=None, ord=None, check_finite=True,  keepdims=True)

Where parameters are:

  • a(array_data): Array of inputs Unless ord is None, the axis must be None and a must be 1-D or 2-D. If both axis and ord are None, the a.ravel 2-norm will be returned.
  • axis(2 tuple of int, int): If the axis is an integer, it specifies the axis along which the vector norms should be computed. If the axis is a 2-tuple, the axes that hold 2-D matrices are specified, and the matrix norms of these matrices are computed.
  • ord(int, inf): It is used to provide the norm order.
  • check_finite(boolean): To check if the provided matrix as input has finite numbers.
  • keepdims(boolean): f this is set to True, the axes which are normed over are left in the result as dimensions with size one.

The method norm() returns the norm of the given matrix.

Let’s take an example by following the below steps:

Import the required libraries using the below python code.

from scipy.linalg import norm
import numpy as np

Create an array of data as a matrix whose norm will be computed using the below code.

array_data = np.arange(8) - 3.0
array_data

Compute the norm of the above-created matrix or array of data using the below code.

norm(array_data)
Scipy Linalg Norm
Scipy Linalg Norm

This is how to calculate the norm of the given matrix using the method norm() of Python SciPy.

Read: Scipy Optimize – Helpful Guide

Scipy Linalg Cholesky

The Python SciPy has a method cholesky() that Returns the Cholesky decomposition of a Hermitian positive-definite matrix A, A=LL or A=UU.

The syntax is given below.

scipy.linalg.cholesky(a, lower=True, overwrite_a=True, check_finite=False)

Where parameters are:

  • a(array_data): To decompose the array.
  • lower(bool): Whether the relevant array of data is chosen from the lower or upper triangle of a.
  • overwrite_a (boolean): to overwrite a.
  • check_finite(boolean): To check if the provided matrix as input has finite numbers.

The method cholesky() returns c(The Cholesky factor of an upper- or lower-triangular) of type ndarray.

Let’s take an example by following the below steps:

Import the required libraries using the below python code.

from scipy.linalg import cholesky
import numpy as np

Create a matrix using the below code.

m = np.array([[2,-1j],[2j,2]])

Compute the Cholesky of that matrix using the below code.

cholesky(m,lower = True)
Scipy Linalg Cholesky
Scipy Linalg Cholesky

This is how to compute the Cholesky of the given matrix using the method cholesky() of Python SciPy.

Also, take a look at some more Scipy Tutorials.

So, in this tutorial, we have learned about the “Scipy Linalg” and covered the following topics.

  • Scipy Linalg
  • Scipy Linalg Eig
  • Scipy Linalg Eigh.
  • Scipy Linalg Lu
  • Scipy Linalg Svd.
  • Scipy Linalg Solve
  • Scipy Linalg Qr
  • Scipy Linalg Inverse
  • Scipy Linalg Norm
  • Scipy Linalg Cholesky