| by Arround The Web | No comments

SciPy Matrices

Matrices are powerful tools for manipulating data and solving complex mathematics problems. Matrices are rectangular arrays of numbers organized in columns and rows. Python provides various libraries, such as Scipy and Numpy, that offer built-in functions for creating, manipulating, and performing operations on matrices.

This Python tutorial presents a comprehensive guide on Scipy matrices using numerous examples:

Scipy Matrices in Python

Python has modules like Scipy and Numpy to perform operations on matrices. The “scipy.linalg” module of the Scipy library is used in Python to perform various operations, such as finding the inverse of the matrixes, finding determinants, and others.

Let’s start by finding the inverse of the matrix using the below example:

Example 1: Finding Inverse of the Matrix

To find/determine the matrix inverse, the below code is used in Python:

from scipy.linalg import inv
import numpy
matrix = numpy.array([[4, 6], [8, 6]])
print(inv(matrix))

 

Here in this code, the “inv” method of the “scipy.linalg” module and the “numpy” module is imported. The “numpy.array()” method creates the array matrix. The “inv()” method determines the inverse of the matrix by accepting the matrix as an argument.

Output

The inverse of the matrix has been determined.

Example 2: Finding the Matrix Determinant

Let’s overview the below code to determine the determinant of the matrix.

import scipy.linalg
import numpy
value1 = numpy.array([[4, 2], [4, 5]])
print(scipy.linalg.det(value1))

 

In the above code, the “scipy.linalg.det()” method of the “scipy.linalg” module is used to determine the determinant of the input matrix.

Output

The determinant of the specified matrix has been calculated.

Scipy Sparse Matrices in Python

The “scipy.sparse” module supports various functions to work with sparse matrices or sparse data. The sparse matrices contain a lot of zeros values of unwanted/unused elements. In Python, there are two kinds of sparse matrices.

  • CSC – Compressed Sparse Column
  • CSR – Compressed Sparse Row

Let’s explore the Scipy sparse matrices via the following examples:

Example 1: Creating CSR (compressed sparse row) Matrix

The following code is utilized to create a CSR matrix:

import numpy
import scipy
arr = numpy.array([0, 3, 2, 0, 0, 4, 0, 0, 2])
print(scipy.sparse.csr_matrix(arr))

 

In this code, the “numpy” and “scipy” modules are imported. The “numpy.array()” function creates an array and passes it to the “scipy.sparse.csr_matrix()” method. After that, the “csr_matrix()” returns the compressed sparse row matrix.

Output

The above output states that:

  • The value “3” is placed at the row “0” and position “1”.
  • The value “2” is placed at the row “0” and position “2”.
  • The value “4” is placed at the row “0” and position “5”.
  • The value “2” is placed at the row “0” and position “8”.

Example 2: Creating CSC (compressed sparse column) Matrix

Let’s overview the below code to create a compressed sparse column matrix:

import numpy, scipy
row_val = numpy.array([0, 2, 2, 0])
col_val = numpy.array([0, 0, 1, 2])
data = numpy.array([1, 2, 3, 4])
print(scipy.sparse.csc_matrix((data, (row_val, col_val)), shape=(3, 3)).toarray())

 

Here in this code, the “scipy”, and “numpy” modules are imported at the start. The “numpy.array()” function is utilized multiple times to create a numpy array for the row, column, and matrix element data. The “scipy.sparse.csc_matrix()” method creates the compressed sparse column matrix by taking the data, row, column, and shape of the return matrix as an argument. The “toarray()” method converts the csc matrix to an array.

Output

The above output states that:

  • The value “1” is placed at the row “0” and position “0”.
  • The value “2” is placed at the row “2” and position “0”.
  • The value “3” is placed at the row “2” and position “1”.
  • The value “4” is placed at the row “0” and position “2”.

Example 3: Converting From CSR to CSC

The following code is utilized to transform/convert the CSR sparse matrix to CSC sparse matrix:

import numpy, scipy
arr = numpy.array([[0, 0, 0], [0, 0, 1], [1, 0, 2]])
newarr = scipy.sparse.csr_matrix(arr)
print(newarr,'\n')
arr1 = newarr.tocsc()
print(arr1)

 

In the above code, the “scipy.sparse.csr_matrix()” creates the compressed sparse row matrix by accepting the array as an argument. The “tocsc()” method converts the CSR matrix to the CSC matrix and assigns the value to variable “arr1”.

Output

The above snippet shows that the CSR matrix has been converted into a CSC matrix.

Conclusion

The “scipy.linalg” module and the “scipy.sparse” module of the Scipy library is used to perform various operations on Scipy matrices in Python. In the “scipy.linalg” module, different functions are used to find the inverse of the matrix, determine the determinant, etc. Similarly, the “scipy.sparse” module also provides various functions for working with Scipy sparse data or matrices. This Python tutorial provided an in-depth explanation of Scipy matrices, with examples.

Share Button

Source: linuxhint.com

Leave a Reply