# SciPy: Elevate Your Data Science with Scientific Python.

## What is SciPy ?

SciPy or a Scientific Python is a Python library that offers a variety of scientific computing tools for data analysis, manipulation, and visualization. It includes modules for optimization, integration, linear algebra, signal processing, and more, making it a comprehensive resource for data scientists. One of the main advantages of using SciPy for data science is its ease of use and flexibility.

SciPy is open source and due to this it has a large community of developers which create enormous number of modules for scientific applications.

Python Knowledge Base: Make coding great again.
- Updated: 2024-09-12 by Andrey BRATUS, Senior Data Analyst.

Because it is built on top of NumPy, another popular data science library, it is easy to integrate into existing workflows. It also offers a range of algorithms that can be used for data cleaning, preprocessing, and feature engineering. Another advantage is its ability to handle large datasets and complex data structures.

SciPy's powerful linear algebra and optimization modules make it useful for machine learning applications. It can be used for clustering, classification, regression, and other common machine learning tasks. Its visualization tools, including Matplotlib and Seaborn, make it easy to create informative data visualizations. In terms of perspectives, SciPy is likely to continue to be a popular choice for data scientists in the future.

SciPy ready to use classes and constatnts are broadly applicable across many domains such as algebraic equations, differential equations, statistics, algorithms optimization, integration, interpolation, eigenvalue problems, machine learning and more...

1. ## Solving Linear Equations.

2. ``````
# 2x + 5y = 6
# 5x + 7y = 9

import numpy as np
from scipy import linalg

equation = np.array([[2, 5], [5, 7]])
solution = np.array([[6], [9]])

roots = linalg.solve(equation, solution)

print("Еhe roots:")
print(roots)

print("\n Dot product should be zero if the solutions are correct:")
print(equation.dot(roots) - solution)
``````
3. ## Solving Polynomial Equations.

4. ``````
# 3*(2**2) + 4*2 + 7 = 27

from numpy import poly1d
first_polynomial = poly1d([3, 4, 7])
print("Solving the Polynomial: \n")
print(first_polynomial(2))
``````

5. ## Linear Algebra - Basic Matrix Operations.

6. ``````
import numpy as np
from scipy import linalg, sparse

#Creating Matrices
A = np.matrix(np.random.random((2,2)))
B = np.asmatrix(b)
C = np.mat(np.random.random((10,5)))
D = np.mat([[3,4], [5,6]])

#Inverse
#A.I Inverse
linalg.inv(A) #Inverse
#A.T Tranpose matrix
#A.H Conjugate transposition
np.trace(A) #Trace

#Norm
linalg.norm(A) #Frobenius norm
linalg.norm(A,1) #L1 norm (max column sum)
linalg.norm(A,np.inf) #L inf norm (max row sum)

#Rank
np.linalg.matrix_rank(C) #Matrix rank

#Determinant
linalg.det(A) #Determinant

#Solving linear problems
linalg.solve(A,b) #Solver for dense matrices
E = np.mat(a).T #Solver for dense matrices
linalg.lstsq(D,E) #Least-squares solution to linear matrix equation

#Generalized inverse
linalg.pinv(C) #Compute the pseudo-inverse of a matrix (least-squares solver)
linalg.pinv2(C) #Compute the pseudo-inverse of a matrix (SVD)
``````
7. ## Linear Algebra - Sparse Matrices.

8. ``````
import numpy as np
from scipy import linalg, sparse

#Creating Sparse Matrices

F = np.eye(3, k=1) #Create a 2X2 identity matrix
G = np.mat(np.identity(2)) #Create a 2x2 identity matrix
C[C > 0.5] = 0
H = sparse.csr_matrix(C) #Compressed Sparse Row matrix
I = sparse.csc_matrix(D) #Compressed Sparse Column matrix
J = sparse.dok_matrix(A) #Dictionary Of Keys matrix
E.todense() #Sparse matrix to full matrix
sparse.isspmatrix_csc(A) #Identify sparse matrix

#Sparse Matrix Operations

#Inverse
sparse.linalg.inv(I) #Inverse
#Norm
sparse.linalg.norm(I) #Norm
#Solving linear problems
sparse.linalg.spsolve(H,I) #Solver for sparse matrices
sparse.linalg.expm(I) #Sparse matrix exponential
``````
9. ## Linear Algebra - Matrix Functions.

10. ``````

#Subtraction
#np.subtract(A,D) #Subtraction

#Division
#np.divide(A,D) #Division

#Multiplication
#np.multiply(D,A) #Multiplication
np.dot(A,D) #Dot product
np.vdot(A,D) #Vector dot product
np.inner(A,D) #Inner product
np.outer(A,D) #Outer product
np.tensordot(A,D) #Tensor dot product
np.kron(A,D) #Kronecker product

#Exponential Functions
linalg.expm(A) #Matrix exponential
linalg.expm2(A) #Matrix exponential (Taylor Series)
linalg.expm3(D) #Matrix exponential (eigenvalue decomposition)

#Logarithm Function
linalg.logm(A) #Matrix logarithm

#Trigonometric Tunctions
linalg.sinm(D) #Matrix sine
linalg.cosm(D) #Matrix cosine
linalg.tanm(A) #Matrix tangent

#Hyperbolic Trigonometric Functions
linalg.sinhm(D) #Hypberbolic matrix sine
linalg.coshm(D) #Hyperbolic matrix cosine
linalg.tanhm(A) #Hyperbolic matrix tangent

#Matrix Sign Function
np.sigm(A) #Matrix sign function

#Matrix Square Root
linalg.sqrtm(A) #Matrix square root

#Arbitrary Functions
linalg.funm(A, lambda x: x*x) #Evaluate matrix function
``````
11. ## Linear Algebra - Matrix Decompositions.

12. ``````
#Eigenvalues and Eigenvectors
la, v = linalg.eig(A) #Solve ordinary or generalized eigenvalue problem for square matrix
l1, l2 = la #Unpack eigenvalues
v[:,0] #First eigenvector
v[:,1] #Second eigenvector
linalg.eigvals(A) #Unpack eigenvalues

#Singular Value Decomposition
U,s,Vh = linalg.svd(B) #Singular Value Decomposition (SVD)
M,N = B.shape
Sig = linalg.diagsvd(s,M,N) #Construct sigma matrix in SVD LU Decomposition
P,L,U = linalg.lu(C) #LU Decomposition

la, v = sparse.linalg.eigs(F,1) #Eigenvalues and eigenvectors
sparse.linalg.svds(H, 2) #SVD
``````
13. ## Useful constants.

14. ``````
from scipy import constants

print(constants.pi) #pi

#Metric (SI) Prefixes

print(constants.tera)     #1000000000000.0
print(constants.giga)     #1000000000.0
print(constants.mega)     #1000000.0
print(constants.kilo)     #1000.0
print(constants.hecto)    #100.0
print(constants.deka)     #10.0
print(constants.deci)     #0.1
print(constants.centi)    #0.01
print(constants.milli)    #0.001
print(constants.micro)    #1e-06
print(constants.nano)     #1e-09
print(constants.pico)     #1e-12
print(constants.femto)    #1e-15

#Binary Prefixes:

print(constants.kibi)    #1024
print(constants.mebi)    #1048576
print(constants.gibi)    #1073741824
print(constants.tebi)    #1099511627776
print(constants.pebi)    #1125899906842624

#Mass:

print(constants.gram)        #0.001
print(constants.metric_ton)  #1000.0
print(constants.grain)       #6.479891e-05
print(constants.lb)          #0.45359236999999997
print(constants.pound)       #0.45359236999999997
print(constants.oz)          #0.028349523124999998
print(constants.ounce)       #0.028349523124999998

#Angle:

print(constants.degree)     #0.017453292519943295
print(constants.arcmin)     #0.0002908882086657216
print(constants.arcminute)  #0.0002908882086657216
print(constants.arcsec)     #4.84813681109536e-06
print(constants.arcsecond)  #4.84813681109536e-06

#Time:

print(constants.minute)      #60.0
print(constants.hour)        #3600.0
print(constants.day)         #86400.0
print(constants.week)        #604800.0
print(constants.year)        #31536000.0

#Length:

print(constants.inch)              #0.0254
print(constants.foot)              #0.30479999999999996
print(constants.yard)              #0.9143999999999999
print(constants.mile)              #1609.3439999999998
print(constants.mil)               #2.5399999999999997e-05
print(constants.pt)                #0.00035277777777777776
print(constants.point)             #0.00035277777777777776

#Pressure:

print(constants.atm)         #101325.0
print(constants.atmosphere)  #101325.0
print(constants.bar)         #100000.0

#Area:

print(constants.hectare) #10000.0
print(constants.acre)    #4046.8564223999992

#Volume:

print(constants.liter)            #0.001
print(constants.litre)            #0.001
print(constants.gallon)           #0.0037854117839999997
print(constants.gallon_US)        #0.0037854117839999997
print(constants.gallon_imp)       #0.00454609
print(constants.fluid_ounce)      #2.9573529562499998e-05
print(constants.fluid_ounce_US)   #2.9573529562499998e-05
print(constants.fluid_ounce_imp)  #2.84130625e-05
print(constants.barrel)           #0.15898729492799998

#Speed:

print(constants.kmh)            #0.2777777777777778
print(constants.mph)            #0.44703999999999994
print(constants.mach)           #340.5
print(constants.speed_of_sound) #340.5
print(constants.knot)           #0.5144444444444445

#Temperature:

print(constants.zero_Celsius)      #273.15
print(constants.degree_Fahrenheit) #0.5555555555555556
``````