Change language

Analyzing data with SciPy

The advantage of using the SciPy library in Python when building ML models is that it also makes a powerful programming language available for use in developing less complex programs and applications.

# import digital library

import numpy as np

A = np.array ([[ 1 , 2 , 3 ], [ 4 , 5 , 6 ], [ 7 , 8 , 8 ]])

Linear Algebra

  1. Matrix determinant

    # import linalg function from scipy

    from scipy import linalg

     
    # Calculate matrix determinant
    linalg.det (A)

      Output:  2.999999999999997 
  2. Computing the expanded LU decomposition of a matrix
    LU decomposition — it is a method that reduces a matrix into its constituent parts, which helps in easier computation of complex matrix operations. Decomposition methods are also called matrix factorization methods, which are the backbone of linear algebra in computers, even for basic operations such as solving systems of linear equations, computing the inverse, and computing the determinant of a matrix. 
    The decomposition is:
    A = PLU
    where P — permutation matrix, L lower triangular with unit diagonal elements and U upper triangular.

    P, L, U = linalg.lu (A)

    print (P)

    print (L)

    print (U)

    # print LU decomposition

    print (np.dot (L, U))

      Output:  array ([[0., 1., 0.], [0., 0., 1.], [1., 0., 0.]]) array ([[1., 0., 0.], [0.14285714, 1., 0.], [0.57142857, 0.5, 1. ]]) array ([[7., 8., 8.], [0., 0.85714286, 1.85714286], [0., 0., 0.5]]) array ([[7., 8., 8.] , [1., 2., 3 .], [4., 5., 6.]]) 
  3. Eigenvalues ​​and eigenvectors of this matrix

    eigen_values, eigen_vectors = linalg.eig (A)

    print (eigen_values)

    print (eigen_vectors)

      Output :  array ([15.55528261 + 0.j, -1.41940876 + 0.j, -0.13587385 + 0.j]) array ([[- 0.24043423, -0.67468642, 0.51853459], [-0.54694322, -0.23391616, - 0.78895962], [-0.80190056, 0.70005819, 0.32964312]]) 
  4. Solving systems of linear equations can also be done

    v   = np.array ([[ 2 ], [ 3 ], [ 5 ]])

    print (v)

    s = linalg.solve (A, v)

    print (s)

      Output:  array ([[2], [3], [5]]) array ([ [-2.33333333], [3.66666667], [-1. ]]) 

Sparse Linear Algebra

SciPy has several routines for computing sparse and potentially very large matrices. The required tools are in the scipy.sparse submodule. 
Let’s see how to build a large sparse matrix:

# import the required modules

from scipy import sparse

# String-based sparse linked list matrix

A = sparse.lil_matrix (( 1000 , 1000 ))

print (A)

 

A [ 0 ,: 100 ] < code class = "keyword"> = np.random.rand ( 100 )

A [ 1 , 100 : 200 ] = A [ 0 ,: 100 ]

A.setdiag (np.random.rand ( 1000 ))

print (A)

  Output:  "1000x1000 sparse matrix of type ’’ with 0 stored elements in LInked List format" "1000x1000 sparse matrix of type ’’ with 1199 stored elements in LInked List format" 
  1. Linear algebra for sparse matrices

    from scipy .sparse import linalg

     
    # Convert this matrix to Compressed Sparse Row format.
    A.tocsr ()

     

    A = A.tocsr ()

    b = np.random.rand ( 1000 )

    ans = linalg.spsolve (A, b)

    # will print an array of data x 1000

    print (ans)

      Output:  array ([- 2.53380006e + 03, -1.25513773e + 03, 9.14885544e-01, 2.74521543e + 00, 5.99942835e-01, 4.57778093e-01, 1.87104209e-01, 2.15228367e + 00, 8.78588432e-01, 1.85105721e + 03, 1.00842538e + 00, 4.33970632e + 00, 5.26601699e + 00, 2.17572231e-01, 1.79869079e + 00, 3.83800946e-01, 2.57817130e-01, 5.18025462e-01, 1.68672669e + 00, 3.07971950e + 00, 6.20604437e-01, 1.41365890e-01, 3.18167429e-01, 2.06457302e-01, 8.94813817e-01, 5.06084834e + 00, 5.00913942e-01, 1.37391305e + 00, 2.32081425e + 00, 4.98093749e + 00, 1.75492222e + 00, 3.17278127e-01, 8.50013844e-01, 1.17524493e + 00, 1.70173722e + 00, .............)) 

integration

When a function is very difficult to integrate analytically, you can simply find a solution using numerical integration methods. SciPy can do numeric integration as well. Scipi has integration methods in the scipy.integrate module.

  1. Single Integrals
    The Quad subroutine is an important feature from SciPy’s integration functions ... If the integration over the function f (x), where x ranges from a to b, then the integral looks like this. 

    Parameters quad — this is scipy.integrate.quad (f, a, b), where "f" — this is a function for integration. Whereas "a" and "b" are the lower and upper range of the limitation of x. Let’s look at an example of integrating in the range from 0 to 1 relative to dx
    First we define the function f (x) = e ^ (- x ^ 2), this is done with a lambda expression, and then the quad subroutine is used.

    import scipy.integrate

    f = lambda x: np.exp ( - x * * 2 )

    # print results

    i = scipy.integrate.quad (f, 0 , 1 )

    print (i) < / p>

     (0.7468241328124271, 8.291413475940725e-15) 

    The quad function returns two values, where the first number is the integral value and the second — probable error in the value of the integral.

  2. Double integrals
    Function parameters dblquad : scipy.integrate.dblquad ( f, a, b, g, h) . Where "f" — this is the function to be integrated, "a" and "b" — the lower and upper ranges of the variable x, respectively, and "g" and "h" — functions that report the lower and upper limits. variable y. 
    As an example, let’s run the double integral x * y ^ 2 over the range 0 to 2 and y from 0 to 1.

    We define functions f, g and h using lambda expressions. Note that even though g and h are constants, as they can in many cases, they must be defined as functions, as we did here for the lower limit.

    from scipy import integrate

    f = lambda y, x : x * y * * 2

    i = integrate.dblquad (f, 0 , 2 , lambda x: 0 , lambda x : 1 )

    # print results

    print (i)

     Output: (0.6666666666666667, 7.401486830834377e-15) 

SciPy can do a lot do for example Fourier transforms, Bessel functions, etc.
You can refer to the documentation for more details!

Shop

Gifts for programmers

Learn programming in R: courses

$FREE
Gifts for programmers

Best Python online courses for 2022

$FREE
Gifts for programmers

Best laptop for Fortnite

$399+
Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best computer for crypto mining

$499+
Gifts for programmers

Best laptop for Sims 4

$

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically