Close this search box.

Introduction to Python SciPy Spatial Data

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

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')


Notice: The simplices property is an extension that is a reinterpretation of the notation triangle.

Convex Hull

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-')



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))


					Output - 

(1.0, 1)

Distance Matrix

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.

Euclidean Distance

Example – Finding the euclidean distance between given points.

					from scipy.spatial.distance import euclidean

a = (1, 5)
b = (5, 2)

result = euclidean(a, b)


					Output - 


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)


					Output - 


Cosine Distance

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)


					Output -


Hamming Distance

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)


					Output -


If you find anything incorrect in the above-discussed topic and have any further questions, please comment below.

Recent Post

Popular Post

Top Articles


Share on