SciPy stands for Scientific Python. It provides more utility functions for optimization, stats and signal processing. Like NumPy, SciPy is open source so we can use it freely.
The most important question is why to use SciPy?
If SciPy uses NumPy underneath, why can we not just use NumPy?
The simplest answer would be SciPy has optimized and added functions that are frequently used in NumPy and Data Science.
How To Install SciPy?
pip install scipy
Now, once scipy is installed you can import it into your file or application from scipy import module statement:
from scipy import constants
Now, that you have imported the scipt module lets take a example and see how this module is used.
Example: We want to find out how many cubic meters are in one liter
from scipy import constants
print(constants.liter)

SciPy offers a set of mathematical constants, one of them is liter which returns 1 liter as cubic meters.
Constants in SciPy
SciPy is more focused on scientific implementations, it provides many built-in scientific constants.
These constants can be helpful when you are working with Data Science.
Examples of constants:
from scipy.constants import c, h, k
# Speed of light in vacuum (m/s)
speed_of_light = c
print("Speed of light:", speed_of_light, "m/s")
# Planck constant (Joule second)
planck_constant = h
print("Planck constant:", planck_constant, "Joule second")
# Boltzmann constant (Joule per Kelvin)
boltzmann_constant = k
print("Boltzmann constant:", boltzmann_constant, "Joule per Kelvin")
Unit Categories
The units are placed under these categories:
- Metric
- Binary
- Mass
- Angle
- Time
- Length
- Pressure
- Volume
- Speed
- Temperature
- Energy
- Power
- Force
You can also use SciPy as a optimizer. Which means if you want to find out the equation of let say x+cos(x) then,
from scipy.optimize import root
from math import cos
def eqn(x):
return x + cos(x)
myroot = root(eqn, 0)
print(myroot.x)

Sparse Data
SciPy can also help you in working with sparse data. Sparse data is a data set where most of the values are zero.
For Example:
[1, 0, 2, 0, 0, 3, 0, 0, 0, 0, 0, 0]
Now, How to work with these kind of data?
SciPy has a module, scipy.sparse that provides functions to deal with sparse data.
There are primarily two types of sparse matrices that we use:
CSC – Compressed Sparse Column. For efficient arithmetic, fast column slicing.
CSR – Compressed Sparse Row. For fast row slicing, faster matrix vector products
We will use the CSR matrix and see a example
We can create CSR matrix by passing an arrray into function scipy.sparse.csr_matrix().
import numpy as np
from scipy.sparse import csr_matrix
arr = np.array([0, 0, 0, 0, 0, 1, 1, 0, 2])
print(csr_matrix(arr))

From the result we can see that there are 3 items with value.
The 1. item is in row 0 position 5 and has the value 1.
The 2. item is in row 0 position 6 and has the value 1.
The 3. item is in row 0 position 8 and has the value 2.
Removing zero-entries from the matrix with the eliminate_zeros() method:
import numpy as np
from scipy.sparse import csr_matrix
arr = np.array([[0, 0, 0], [0, 0, 1], [1, 0, 2]])
mat = csr_matrix(arr)
mat.eliminate_zeros()
print(mat)

SciPy Graphs
SciPy provides us with the module scipy.sparse.csgraph for working with such data structures.
For Example:
import numpy as np
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import connected_components, shortest_path
# Create a graph as an adjacency matrix
graph = np.array([
[0, 1, 1, 0],
[1, 0, 1, 1],
[1, 1, 0, 1],
[0, 1, 1, 0]
])
# Convert the adjacency matrix to a CSR sparse matrix
sparse_graph = csr_matrix(graph)
# Find the number of connected components in the graph
num_components, labels = connected_components(sparse_graph)
print("Number of connected components:", num_components)
print("Component labels:", labels)
# Find the shortest path from node 0 to all other nodes
distances, predecessors = shortest_path(sparse_graph, directed=False, indices=0, return_predecessors=True)
print("Shortest distances from node 0:", distances)
print("Predecessors:", predecessors)
In this example, we create a simple undirected graph represented by an adjacency matrix. We then convert this matrix to a sparse matrix format (csr_matrix). We use the connected_components function to find the number of connected components in the graph and the shortest_path function to find the shortest path from node 0 to all other nodes in the graph.
In conclusion,
the Python SciPy tutorial provides a comprehensive and detailed analysis of how to use SciPy for scientific computing. It covers various topics such as integration, optimization, interpolation, and more, offering a thorough understanding of SciPy’s capabilities. Whether you’re a beginner or an experienced programmer, this tutorial serves as a valuable resource for leveraging SciPy in your scientific and engineering projects.





Leave a Reply