Modul Praktikum SciPy
Modul Praktikum SciPy
Modul Praktikum SciPy
What is SciPy?
SciPy is a scientific computation library that uses NumPy underneath.
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.
SciPy was created by NumPy's creator Travis Olliphant.
Import SciPy
Once SciPy is installed, import the SciPy module(s) you want to use in your applications by adding the from
scipy import module statement:
Now we have imported the constants module from SciPy, and the application is ready to use it:
Example
How many cubic meters are in one liter:
constants: SciPy offers a set of mathematical constants, one of them is liter which returns 1 liter as cubic
meters.
You will learn more about constants in the next chapter.
Constants in SciPy
As 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.
PI is an example of a scientific constant.
Example
Print the constant value of PI:
Unit Categories
The units are placed under these categories:
• Metric
• Binary
• Mass
• Angle
• Time
• Length
• Pressure
• Volume
• Speed
• Temperature
• Energy
• Power
• Force
Binary Prefixes:
Return the specified unit in bytes (e.g. kibi returns 1024)
Mass:
Return the specified unit in kg (e.g. gram returns 0.001)
Example
Angle:
Return the specified unit in radians (e.g. degree returns 0.017453292519943295)
Example
Time:
Return the specified unit in seconds (e.g. hour returns 3600.0)
Example
Pressure:
Return the specified unit in pascals (e.g. psi returns 6894.757293168361)
Example
Area:
Return the specified unit in square meters(e.g. hectare returns 10000.0)
Example
Volume:
Return the specified unit in cubic meters (e.g. liter returns 0.001)
Speed:
Return the specified unit in meters per second (e.g. speed_of_sound returns 340.5)
Example
Temperature:
Return the specified unit in Kelvin (e.g. zero_Celsius returns 273.15)
Example
Energy:
Return the specified unit in joules (e.g. calorie returns 4.184)
Example
Power:
Return the specified unit in watts (e.g. horsepower returns 745.6998715822701)
Example
Force:
Return the specified unit in newton (e.g. kilogram_force returns 9.80665)
Optimizers in SciPy
Optimizers are a set of procedures defined in SciPy that either find the minimum value of a function, or the root
of an equation.
Optimizing Functions
Essentially, all of the algorithms in Machine Learning are nothing more than a complex equation that needs to
be minimized with the help of given data.
Roots of an Equation
NumPy is capable of finding roots for polynomials and linear equations, but it can not find roots for non linear
equations, like this one:
x + cos(x)
For that you can use SciPy's optimze.root function.
This function takes two required arguments:
fun - a function representing an equation.
x0 - an initial guess for the root.
The function returns an object with information regarding the solution.
The actual solution is given under attribute x of the returned object:
Example
Find root of the equation x + cos(x):
Note: The returned object has much more information about the solution.
Example
Print all information about the solution (not just x which is the root)
print(myroot)
Minimizing a Function
A function, in this context, represents a curve, curves have high points and low points.
High points are called maxima.
Low points are called minima.
The highest point in the whole curve is called global maxima, whereas the rest of them are called local maxima.
The lowest point in whole curve is called global minima, whereas the rest of them are called local minima.
Finding Minima
We can use scipy.optimize.minimize() function to minimize the function.
The minimize() function takes the following arguments:
CSR Matrix
We can create CSR matrix by passing an arrray into function scipy.sparse.csr_matrix().
Example
Create a CSR matrix from an array:
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.
Note: Apart from the mentioned sparse specific operations, sparse matrices support all of the operations that
normal matrices support e.g. reshaping, summing, arithemetic, broadcasting etc.
Adjacency Matrix
Adjacency matrix is a nxn matrix where n is the number of elements in a graph.
And the values represents the connection between the elements.
Example:
Below follows some of the most used methods for working with adjacency matrices.
Connected Components
Find all of the connected components with the connected_components() method.
Example
Dijkstra
Use the dijkstra method to find the shortest path in a graph from one element to another.
It takes following arguments:
1. return_predecessors: boolean (True to return whole path of traversal otherwise
False).
2. indices: index of the element to return all paths from that element only.
3. limit: max weight of path.
Floyd Warshall
Use the floyd_warshall() method to find shortest path between all pairs of elements.
Example
Find the shortest path between all pairs of elements:
Bellman Ford
The bellman_ford() method can also find the shortest path between all pairs of elements,
but this method can handle negative weights as well.
Example
Find shortest path from element 1 to 2 with given graph with a negative weight:
Convex Hull
A convex hull is the smallest polygon that covers all of the given points.
Use the ConvexHull() method to create a Convex Hull.
Example
Create a convex hull for following points:
Euclidean Distance
Find the euclidean distance between given points.
Example
Cosine Distance
Is the value of cosine angle between the two points A and B.
Example
Find the cosine distsance between given points:
Hamming Distance
Is the proportion of bits where two bits are difference.
It's a way to measure distance for binary sequences.
Example
Find the hamming distance between given points: