In This Article, You Will Learn About Python SciPy Spatial Data.
Python SciPy Spatial Data – Before moving ahead, let’s know a bit about Python SciPy Graphs
Table of Contents
Spatial Data is data that is shown as a geographical image.
Spatial Data problems are common problems; therefore, we deal with them every day’s task.
Example – A road map. It is two-dimensional object points, lines, and polygons that can represent cities, roads, etc.
Finding which lines connect a street to another street.
To work with Spatial Data, SciPy has the module scipy.spatial.
A triangulation of an amorphous polygon is the division of the polygon into several triangles that allow us to calculate the amount of polygonal area.
A triangulation using points creates a surface made of triangular shapes in which all the points mentioned are at least one side of any triangle on the surface.
SciPy provides method to work with triangulation is Delaunay().
Example – Creating a triangulation from the given points.
import numpy as np from scipy.spatial import Delaunay import matplotlib.pyplot as plt points = np.array([ [3, 4], [3, 6], [7, 2], [4, 10], [3, 1]]) simplices = Delaunay(points).simplices plt.triplot(points[:, 1], points[:, 0], simplices) plt.scatter(points[:, 1], points[:, 0], color='g') plt.show()
Notice: The simplices property is an extension that is a reinterpretation of the notation triangle.
A convex hull is the most compact polygon which covers all specified points.
To work with Convex Hull, SciPy has the method ConvexHull().
Example – Creating a convex hull for the given points.
import numpy as np from scipy.spatial import ConvexHull import matplotlib.pyplot as plt points = np.array([ [1, 4], [3, 6], [3, 0], [3, 2], [4, 3], [1, 2], [5, 2], [4, 1], [1, 2], [2, 3]]) Hull = ConvexHull(points) Hull_points = Hull.simplices plt.scatter(points[:, 1], points[:, 0]) for simplex in Hull_points: plt.plot(points[simplex, 1], points[simplex, 1], 'k-') plt.show()
KDTrees are a kind of data structure created to deal with nearest points.
E.g., By using KDTrees, we can easily determine which points are nearest to each other in a set or collection of points.
SciPy provides the KDTree() method that returns a KDTree object.
SciPy provides the query() method that returns the distance to the nearest neighbor and the location of the neighbors.
Example – Find the nearest neighbor to point (1,1).
from scipy.spatial import KDTree points = [(2, -1), (2, 1), (-2, 1), (2, -1)] kdtree = KDTree(points) result = kdtree.query((1, 1)) print(result)
Output - (1.0, 1)
Distance Metrics are used for finding out the distance between the two points in data science such as Euclidean distsance, cosine distsance etc.
A distance of two vectors might not just be the length of straight line connecting them, but it could additionally be the angle from their origin, or the quantity of unit steps needed.
A large portion aspects of Machine Learning algorithm’s performance depends heavily on distance metrics. E.g. “K Nearest Neighbors”, or “K Means” etc.
Example – Finding the euclidean distance between given points.
from scipy.spatial.distance import euclidean a = (1, 5) b = (5, 2) result = euclidean(a, b) print(result)
Output - 5.0
Cityblock Distance (Manhattan Distance)
Let’s check whether distance is computed using 4 degrees of movement or not?
E.g. we can only try going to- up, down, right, or left, but not diagonally.
from scipy.spatial.distance import cityblock a = (3, 0) b = (4, 2) result = cityblock(a, b) print(result)
Output - 3
Is the value of the cosine angle lies between the two points A and B?
from scipy.spatial.distance import cosine a = (2, 0) b = (4, 2) result = cosine(a, b) print(result)
Output - 0.10557280900008414
Is the proportion of bits where two bits are different?
It is a way through which we can measure distance for binary sequences.
from scipy.spatial.distance import hamming a = (True, True, False) b = (False, True, False) result = hamming(a, b) print(result)
Output - 0.3333333333333333
If you find anything incorrect in the above-discussed topic and have any further questions, please comment below.
Like us on