Python library SciPy
SciPy, which stands for Scientific Python, is an open source, BSD-licensed library (i.e. module) for mathematics, science and engineering. The SciPy library depends on NumPy, which provides a large number of functions that operate on Numpy arrays and are useful for various scientific and engineering applications.
It is a fully-featured version of Linear Algebra while Numpy contains only a few features. SciPy also offers many user-friendly and efficient numerical practices such as functions for numerical integration and optimization. Most new Data Science features are available in SciPy rather than Numpy.
SciPy for Linear Algebra
For performing operations of linear algebra, we will have to import linalg from SciPy. See the example below:
import numpy as np
from scipy import linalg
## Solving for the set of equations: 5x + 3y = 21 and 2x + 2y = 10
# Creating input and solution arrays for this problem
A = np.array([[5, 3], [2, 2]])
B = np.array([[21], [10]])
print(linalg.solve(A, B)) # Solution: x = 3, y = 2
Output:
[[3.]
[2.]]
SciPy for Matrix Operations, Polynomials and Integration
Examples involving matrix operations using linear algebra are given below. We also create polynomials and perform integration using “poly1d” and “integrate” respectively. See the example below.
import numpy as np
from scipy import linalg
from numpy import poly1d
from scipy import integrate
# Sample square matrix
mat = np.array([[8,2], [1,4]])
print("Sample matrix:")
print(mat)
# Find determinant of square matrix
print("\nDeterminant: ")
print(linalg.det(mat))
# Find inverse of square matrix
print("\nInverse: ")
print(linalg.inv(mat))
# Find singular value decomposition (svd)
print("\nSVD: ")
print(linalg.svd(mat))
# Find LU decomposition
print("\nLUD: ")
print(linalg.lu(mat))
# Work with polynomial using "poly1d"
print("\nPolynomial: ")
p = poly1d([5, 3, 2, 7])
print(p)
val = p(5)
print("Value: ", val) # Value of polynomial for x = 5
# Work with integration using "integrate"
print("\nResult of Integration: ")
res = integrate.quad(lambda x: x**2,0,4) # last 2 arguments are lower & upper limits
print(res)
Output:
Sample matrix:
[[8 2]
[1 4]]
Determinant:
30.0
Inverse:
[[ 0.13333333 -0.06666667]
[-0.03333333 0.26666667]]
SVD:
(array([[-0.9610057 , -0.27652857],
[-0.27652857, 0.9610057 ]]), array([8.52079729, 3.52079729]), array([[-0.9347217 , -0.35538056],
[-0.35538056, 0.9347217 ]]))
LUD:
(array([[1., 0.],
[0., 1.]]), array([[1. , 0. ],
[0.125, 1. ]]), array([[8. , 2. ],
[0. , 3.75]]))
Polynomial:
5x³ + 3x² + 2x + 7 # result slightly modified for better understanding
Value: 717
Result of Integration:
(21.333333333333336, 2.368475785867001e-13)
SciPy special functions
SciPy special function includes Cubic Root, Exponential, Log sum Exponential, Lambert, Permutation and Combinations, Gamma, Bessel, hyper-geometric, Kelvin, beta, parabolic cylinder, Relative Error Exponential, etc. See use of some of these functions in the example below. For this, we will have to import special from SciPy.
from scipy import special
# Find cubic root of 125 & 64 using cbrt() function
cb = special.cbrt([125, 64])
print(cb)
# Define exp10 function and pass value in it
exp = special.exp10([1, 5])
print(exp)
# Find permutations of 6, 4 values using comb(N, k)
per = special.perm(6, 4, exact=True)
print(per)
# Find combinations of 6, 4 values using comb(N, k)
com = special.comb(6, 4, exact=True)
print(com)
# Find Log Sum Exponential Function
res = special.logsumexp(100)
print(res)
Output:
[5. 4.]
[1.e+01 1.e+05]
360
15
100.0