SciPy Spatial Data
Spatial data, also known as geometric data, is used to represent information about an object's position, shape, and size distribution, such as points on a coordinate system.
SciPy processes spatial data through the scipy.spatial module, such as determining whether a point is inside a boundary, computing the nearest point to a given point, and finding all points within a given distance.
Triangulation
Triangulation is a method in trigonometry and geometry for measuring the distance to a target by measuring the angles from the target point to the known endpoints of a fixed baseline.
Triangulation of a polygon divides the polygon into multiple triangles, and we can use these triangles to calculate the area of the polygon.
A known fact in topology tells us: any surface admits a triangulation.
Suppose there is a triangulation on a surface. Let p be the total number of triangle vertices (shared vertices counted once), a the number of edges, and n the number of triangles. Then e = p - a + n is a topological invariant of the surface. That is, regardless of the triangulation, e always yields the same value. e is called the Euler characteristic.
The method for triangulating a series of points is Delaunay() triangulation.
Example
Create triangles from the given points:
import
numpy
as
np
from
scipy.
spatial
import
Delaunay
import
matplotlib.
pyplot
as
plt
points
=
np.
array
(
[
[
2
,
4
]
,
[
3
,
4
]
,
[
3
,
0
]
,
[
2
,
2
]
,
[
4
,
1
]
]
)
simplices
=
Delaunay
(
points
)
.
simplices
Indices of vertices in the triangle
plt.
triplot
(
points
[
:
,
0
]
,
points
[
:
,
1
]
,
simplices
)
plt.
scatter
(
points
[
:
,
0
]
,
points
[
:
,
1
]
,
color
=
'r'
)
plt.
show
(
)
The output is shown in the figure below:

**Note:** The ids of the triangle vertices are stored in the simplices attribute of the triangulation object.
Convex Hull
The convex hull is a concept in computational geometry (graphics).
In a real vector space V, for a given set X, the intersection S of all convex sets containing X is called the convex hull of X. The convex hull of X can be constructed using the convex combinations of all points (X1, ... Xn) in X.
We can use the ConvexHull() method to create a convex hull.
Example
Create a convex hull from the given points:
import
numpy
as
np
from
scipy.
spatial
import
ConvexHull
import
matplotlib.
pyplot
as
plt
points
=
np.
array
(
[
[
2
,
4
]
,
[
3
,
4
]
,
[
3
,
0
]
,
[
2
,
2
]
,
[
4
,
1
]
,
[
1
,
2
]
,
[
5
,
0
]
,
[
3
,
1
]
,
[
1
,
2
]
,
[
0
,
2
]
]
)
hull
=
ConvexHull
(
points
)
hull_points
=
hull.
simplices
plt.
scatter
(
points
[
:
,
0
]
,
points
[
:
,
1
]
)
for
simplex
in
hull_points:
plt.
plot
(
points
[
simplex
,
0
]
,
points
[
simplex
,
1
]
,
'k-'
)
plt.
show
(
)
The output is shown in the figure below:

K-D Tree
A kd-tree (short for k-dimensional tree) is a tree data structure that stores instance points in k-dimensional space for fast retrieval. It is mainly used for searching key data in multidimensional space (such as range search and nearest neighbor search).
K-D trees can be used in many applications, such as multidimensional key-value search (range search and nearest neighbor search).
Nearest neighbor search is used to find the point in the tree closest to the input point.
KDTree() The method returns a KDTree object.
query() The method returns the nearest neighbor distance and the nearest neighbor position.
Example
Find the nearest neighbor distance to (1, 1):
from
scipy.
spatial
import
KDTree
points
=
[
(
1
,
1
)
,
(
2
,
3
)
,
(
2
,
3
)
,
(
2
,
3
)
]
kdtree
=
KDTree
(
points
)
res
=
kdtree.
query
(
(
1
,
1
)
)
(
res
)
The output is shown in the figure below:
(2.0, 0)
Distance Matrix
In mathematics, a distance matrix is a matrix (2D array) whose entries are the distances between points. Thus, given N points in Euclidean space, its distance matrix is an NxN symmetric matrix whose elements are non-negative real numbers. The concept is similar to an adjacency matrix, the difference being that the latter only indicates whether elements (points) are connected, without containing information about the connected distances. Therefore, a distance matrix can be viewed as a weighted form of an adjacency matrix.
For example, we analyze the following 2D points a through f. Here, we use the Euclidean metric between the pixels where the points lie as the distance metric.

Its distance matrix is:

These distance matrix values can be further viewed as a graphical heatmap (shown below), where black represents zero distance and white represents the maximum distance.

In bioinformatics, distance matrices are used to represent coordinate-system-independent protein structures, as well as the distance between two sequences in sequence space. These representations are used in structural alignment, sequence alignment, and in determining protein structures via NMR, X-ray, and crystallography.
Euclidean Distance
In mathematics, the Euclidean distance or Euclidean metric is the "ordinary" (i.e., straight-line) distance between two points in Euclidean space. Using this distance, Euclidean space becomes a metric space. The associated norm is called the Euclidean norm. Earlier literature calls it the Pythagorean metric.
The Euclidean metric (also called Euclidean distance) is a commonly used definition of distance, referring to the true distance between two points in m-dimensional space, or the natural length of a vector (i.e., the distance from that point to the origin). In two- and three-dimensional space, the Euclidean distance is the actual distance between two points.
The following example computes the Euclidean distance between given points:
Example
from
scipy.
spatial
.
distance
import
euclidean
p1
=
(
1
,
0
)
p2
=
(
10
,
2
)
res
=
euclidean
(
p1
,
p2
)
(
res
)
The output is shown in the figure below:
9.21954445729
Manhattan Distance
Taxicab geometry or Manhattan distance is a term coined by Hermann Minkowski in the 19th century. It is a geometric term used in metric spaces to indicate the sum of the absolute axial distances between two points on a standard coordinate system.
Movement using Manhattan distance can only go up, down, left, or right, and the Manhattan distance between two points is the shortest distance between them.
Manhattan vs Euclidean distance: the red, blue, and yellow lines show that all Manhattan distances have the same length (12), while the green line shows the Euclidean distance has a length of 6xsqrt(2) which is approximately 8.48.

The following example computes the Manhattan distance between points:
Example
from
scipy.
spatial
.
distance
import
cityblock
p1
=
(
1
,
0
)
p2
=
(
10
,
2
)
res
=
cityblock
(
p1
,
p2
)
(
res
)
The output is:
11
Cosine Distance
Cosine distance, also known as cosine similarity, measures the similarity between two vectors by measuring the cosine of the angle between them.
The cosine of a 0-degree angle is 1, while the cosine of any other angle is no greater than 1, and its minimum value is -1.
The following example computes the cosine distance between points A and B:
Example
from
scipy.
spatial
.
distance
import
cosine
p1
=
(
1
,
0
)
p2
=
(
10
,
2
)
res
=
cosine
(
p1
,
p2
)
(
res
)
The output is:
0.019419324309079777
Hamming Distance
In information theory, the Hamming distance between two strings of equal length is the number of positions at which the corresponding characters differ. In other words, it is the number of character substitutions required to change one string into the other.
The Hamming weight of a string is its Hamming distance from a zero string of the same length; that is, the number of non-zero elements in the string: for a binary string, it is the number of 1s, so the Hamming weight of 11101 is 4.
- 1011101The difference between1001001The Hamming distance between them is 2.
- 2143896The difference between2233796The Hamming distance between them is 3.
- "toned"and"roses"The Hamming distance between them is 3.
The following example computes the Hamming distance between two points:
Example
from
scipy.
spatial
.
distance
import
hamming
p1
=
(
True
,
False
,
True
)
p2
=
(
False
,
True
,
True
)
res
=
hamming
(
p1
,
p2
)
(
res
)
The output is:
0.666666666667