Only 6 questions are needed to easily master the fundamentals of SciPy spatial computing | Python skill tree

0. Preface

Spatial computing discusses how to use the principles and methods of spatial principle computing to process spatial data. Spatial computing refers to the data represented in geometric space. We need to deal with spatial problems in many tasks, such as calculating the distance between two points in space. Let's master the method to solve basic spatial computing problems through 6 Python SciPy programming problems!

1. Question 1: triangulation

Knowledge point description: polygon triangulation can divide the polygon into multiple triangles, which can be used to calculate the area of the polygon.
Question Description: write a program to triangulate the polygon generated from a given point. Please choose the correct answer from the following options:
A.

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], [5, 5]])
simplices = Delaunay(points).points
plt.triplot(points[:, 0], points[:, 1], simplices)
plt.scatter(points[:, 0], points[:, 1], color='m')
plt.show()

B.

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], [5, 5]])
simplices = Delaunay(points).neighbors
plt.triplot(points[:, 0], points[:, 1], simplices)
plt.scatter(points[:, 0], points[:, 1], color='m')
plt.show()

C.

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], [5, 5]])
simplices = Delaunay(points).simplices
plt.triplot(points[:, 0], points[:, 1], simplices)
plt.scatter(points[:, 0], points[:, 1], color='m')
plt.show()

D.

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], [5, 5]])
simplices = Delaunay(points).coplanar
plt.triplot(points[:, 0], points[:, 1], simplices)
plt.scatter(points[:, 0], points[:, 1], color='m')
plt.show()

Correct answer: C

2. Question 2: convex hull

Knowledge point description: convex hull is the smallest convex polygon covering all given points.
Problem Description: it is known that there are several given points. Create the minimum convex polygon that can cover all these points. Please choose the correct answer from the following options:
A.

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], [5, 5], [6, 7]])
hull_points = ConvexHull(points).points
plt.scatter(points[:,0], points[:,1])
for simplex in hull_points:
    plt.plot(points[simplex,0], points[simplex,1], 'm-')
plt.show()

B.

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], [5, 5], [6, 7]])
hull_points = ConvexHull(points).simplices
plt.scatter(points[:,0], points[:,1])
for simplex in hull_points:
    plt.plot(points[simplex,0], points[simplex,1], 'm-')
plt.show()

C.

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], [5, 5], [6, 7]])
hull_points = ConvexHull(points).neighbors
plt.scatter(points[:,0], points[:,1])
for simplex in hull_points:
    plt.plot(points[simplex,0], points[simplex,1], 'm-')
plt.show()

D.

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], [5, 5], [6, 7]])
hull_points = ConvexHull(points).coplanar
plt.scatter(points[:,0], points[:,1])
for simplex in hull_points:
    plt.plot(points[simplex,0], points[simplex,1], 'm-')
plt.show()

Correct answer: B

3. Question 3: K-D tree

Knowledge point description: K-D tree is a data structure optimized for nearest neighbor query.
Question Description: use a given point to construct a K-D tree, and according to the nearest point from the constructed K-D tree to the query point (2, 3), please choose the correct answer from the following options:
A.

from scipy.spatial import KDTree
points = [(-2, -1), (3, 8), (-2, 5), (-2, -3)]
kdtree = KDTree(points)
res = kdtree.query((2, 3))
print(res)

B.

from scipy.spatial import KDTree
points = [(-2, -1), (3, 8), (-2, 5), (-2, -3)]
kdtree = KDTree(points)
res = kdtree.query_ball_point((2, 3))
print(res)

C.

from scipy.spatial import KDTree
points = [(-2, -1), (3, 8), (-2, 5), (-2, -3)]
kdtree = KDTree(points)
res = kdtree.query_pairs((2, 3))
print(res)

D.

from scipy.spatial import KDTree
points = [(-2, -1), (3, 8), (-2, 5), (-2, -3)]
kdtree = KDTree(points)
res = kdtree.query_ball_tree((2, 3))
print(res)

Correct answer: A

4. Question 4: Manhattan distance

Knowledge point description: calculate Manhattan distance, that is, the distance between urban blocks.
Question Description: to find the Manhattan distance between given points, please choose the correct answer from the following options:
A.

from scipy.spatial.distance import euclidean
points_1 = (5, 5)
points_2 = (10, 10)
res = euclidean(points_1, points_2)
print(res)

B.

from scipy.spatial.distance import cityblock
points_1 = (5, 5)
points_2 = (10, 10)
res = cityblock(points_1, points_2)
print(res)

C.

from scipy.spatial.distance import euclidean
points_1 = (5, 5)
points_2 = (10, 10)
res = euclidean(points_1, points_2) ** 2
print(res)

D.

from scipy.spatial.distance import cityblock
points_1 = (5, 5)
points_2 = (10, 10)
res = cityblock(points_1, points_2) ** 2
print(res)

Correct answer: B

5. Question 5: cosine distance

Knowledge point description: cosine distance is the cosine value of the angle between two vectors.
Question Description: to find the cosine distance between fixed points, please choose the correct answer from the following options:
A.

from scipy.spatial.distance import euclidean
points_1 = (5, 5)
points_2 = (8, 10)
res = euclidean(points_1, points_2)
print(res)

B.

from scipy.spatial.distance import euclidean
points_1 = (5, 5)
points_2 = (8, 10)
res = 1 / euclidean(points_1, points_2)
print(res)

C.

from scipy.spatial.distance import cosine
points_1 = (5, 5)
points_2 = (8, 10)
res = 1 / cosine(points_1, points_2)
print(res)

D.

from scipy.spatial.distance import cosine
points_1 = (5, 5)
points_2 = (8, 10)
res = cosine(points_1, points_2)
print(res)

Correct answer: D

6. Question 6: Hamming distance

Description of knowledge points: Hamming distance is the proportion of different bits in the corresponding position of a given point.
Question Description: to find the Hamming distance between given points, please choose the correct answer from the following options:
A.

from scipy.spatial.distance import hamming
points_1 = (1, 2, 3, 4, 5)
points_2 = (1, 3, 4, 4, 5)
res = 1 / hamming(points_1, points_2)
print(res)

B.

from scipy.spatial.distance import euclidean
points_1 = (1, 2, 3, 4, 5)
points_2 = (1, 3, 4, 4, 5)
res = 1 / euclidean(points_1, points_2)
print(res)

C.

from scipy.spatial.distance import hamming
points_1 = (1, 2, 3, 4, 5)
points_2 = (1, 3, 4, 4, 5)
res = hamming(points_1, points_2)
print(res)

D.

from scipy.spatial.distance import euclidean
points_1 = (1, 2, 3, 4, 5)
points_2 = (1, 3, 4, 4, 5)
res = euclidean(points_1, points_2)
print(res)

Correct answer: C

Test question code address

https://codechina.csdn.net/LOVEmy134611/python_problem

Tags: Python Machine Learning scipy

Posted on Fri, 15 Oct 2021 23:34:32 -0400 by igorlopez