Bird
Raised Fist0
SciPydata~20 mins

Convex hull computation in SciPy - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Convex Hull Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of Convex Hull vertices indices
What is the output of this code snippet that computes the convex hull of a set of 2D points using scipy?
SciPy
import numpy as np
from scipy.spatial import ConvexHull
points = np.array([[0, 0], [1, 1], [1, 0], [0, 1], [0.5, 0.5]])
hull = ConvexHull(points)
print(list(hull.vertices))
A[0, 2, 1, 3]
B[0, 1, 2, 3, 4]
C[4, 2, 1]
D[1, 2, 3]
Attempts:
2 left
💡 Hint
The convex hull vertices are the outermost points forming the polygon around all points.
❓ data_output
intermediate
2:00remaining
Number of simplices in 3D convex hull
Given a set of 3D points, how many simplices (triangular faces) does the convex hull have?
SciPy
import numpy as np
from scipy.spatial import ConvexHull
points = np.random.rand(10, 3)
hull = ConvexHull(points)
print(len(hull.simplices))
A8
B10
C20
D16
Attempts:
2 left
💡 Hint
The number of simplices depends on the shape formed by the points, but for 10 random points in 3D, it is usually around 16.
🔧 Debug
advanced
2:00remaining
Identify the error in convex hull computation
What error does this code raise when trying to compute the convex hull of a single point?
SciPy
import numpy as np
from scipy.spatial import ConvexHull
points = np.array([[0, 0]])
hull = ConvexHull(points)
AIndexError: list index out of range
BQhullError: input must contain at least 3 points
CTypeError: unsupported operand type(s)
DNo error, returns empty hull
Attempts:
2 left
💡 Hint
Convex hull requires at least 3 points in 2D to form a polygon.
❓ visualization
advanced
2:00remaining
Visualizing convex hull edges in 2D
Which option correctly plots the convex hull edges for a set of 2D points using matplotlib and scipy?
SciPy
import numpy as np
import matplotlib.pyplot as plt
from scipy.spatial import ConvexHull
points = np.random.rand(15, 2)
hull = ConvexHull(points)
plt.plot(points[:,0], points[:,1], 'o')
for simplex in hull.simplices:
    plt.plot(points[simplex, 0], points[simplex, 1], 'r-')
plt.show()
APlots points and blue lines connecting all points, not just hull edges.
BPlots points but no lines connecting hull edges.
CPlots points and red lines connecting hull edges correctly.
DRaises an error due to incorrect indexing.
Attempts:
2 left
💡 Hint
The hull.simplices attribute contains pairs of indices for edges of the hull.
🚀 Application
expert
3:00remaining
Using convex hull to filter outliers in 2D data
You have 2D data points with some outliers far from the main cluster. Which approach uses the convex hull to identify and remove outliers?
ACompute the convex hull of the main cluster only, then remove points outside this hull.
BCompute the convex hull of all points, then remove points inside the hull polygon.
CCompute the convex hull of all points, then remove points that are vertices of the hull.
DCompute the convex hull of all points, then remove points outside the hull's polygon.
Attempts:
2 left
💡 Hint
Outliers lie outside the main cluster's convex hull.

Practice

(1/5)
1. What does the scipy.spatial.ConvexHull function compute for a set of points?
easy
A. The smallest convex shape enclosing all points
B. The average distance between points
C. The maximum value among the points
D. The sum of all point coordinates

Solution

  1. Step 1: Understand the purpose of ConvexHull

    The ConvexHull function finds the smallest convex polygon that contains all given points.
  2. Step 2: Compare options with definition

    Only The smallest convex shape enclosing all points describes this smallest convex shape enclosing all points.
  3. Final Answer:

    The smallest convex shape enclosing all points -> Option A
  4. Quick Check:

    Convex hull = smallest convex shape [OK]
Hint: Convex hull wraps points tightly in a convex boundary [OK]
Common Mistakes:
  • Confusing convex hull with average or sum calculations
  • Thinking it finds maximum or minimum coordinate values
  • Assuming it returns all points instead of boundary points
2. Which of the following is the correct way to import the ConvexHull class from scipy?
easy
A. from scipy.spatial import ConvexHull
B. import scipy.ConvexHull
C. from scipy import ConvexHull
D. import ConvexHull from scipy.spatial

Solution

  1. Step 1: Recall correct import syntax in Python

    To import a class from a submodule, use 'from module.submodule import Class'.
  2. Step 2: Match with scipy.spatial.ConvexHull

    The correct import is 'from scipy.spatial import ConvexHull'. Other options have syntax errors or wrong paths.
  3. Final Answer:

    from scipy.spatial import ConvexHull -> Option A
  4. Quick Check:

    Correct import = from scipy.spatial import ConvexHull [OK]
Hint: Use 'from module.submodule import Class' for importing classes [OK]
Common Mistakes:
  • Using 'import scipy.ConvexHull' which is invalid syntax
  • Trying 'import ConvexHull from ...' which is wrong order
  • Importing directly from 'scipy' without 'spatial'
3. What will be the output of the following code?
import numpy as np
from scipy.spatial import ConvexHull
points = np.array([[0, 0], [1, 0], [1, 1], [0, 1], [0.5, 0.5]])
hull = ConvexHull(points)
print(hull.vertices)
medium
A. [1 2 3]
B. [0 1 2 3 4]
C. [4]
D. [0 1 2 3]

Solution

  1. Step 1: Identify points forming the convex hull

    The points at indices 0,1,2,3 form the square boundary; point 4 is inside.
  2. Step 2: Understand hull.vertices output

    hull.vertices returns indices of points forming the hull boundary, excluding interior points.
  3. Final Answer:

    [0 1 2 3] -> Option D
  4. Quick Check:

    Vertices exclude inside points = [0 1 2 3] [OK]
Hint: Hull vertices exclude points inside the shape [OK]
Common Mistakes:
  • Including interior points in hull vertices
  • Expecting all points as hull vertices
  • Misunderstanding output as coordinates instead of indices
4. The following code throws an error. What is the likely cause?
import numpy as np
from scipy.spatial import ConvexHull
points = np.array([[0, 0], [1, 1]])
hull = ConvexHull(points)
print(hull.vertices)
medium
A. Missing import statement
B. Points are not in ascending order
C. Not enough points to form a convex hull
D. Points array has wrong data type

Solution

  1. Step 1: Check minimum points needed for convex hull

    Convex hull in 2D requires at least 3 points to form a polygon.
  2. Step 2: Analyze given points

    Only 2 points are given, so ConvexHull cannot compute a polygon, causing an error.
  3. Final Answer:

    Not enough points to form a convex hull -> Option C
  4. Quick Check:

    Minimum 3 points needed for 2D hull [OK]
Hint: Convex hull needs at least 3 points in 2D [OK]
Common Mistakes:
  • Assuming any number of points works
  • Thinking order of points matters for hull computation
  • Ignoring error message about input size
5. You have a set of 2D points representing city locations. You want to find the smallest fence enclosing all cities. Which steps correctly use scipy.spatial.ConvexHull to find the fence points?
hard
A. Import ConvexHull, sort points by x-coordinate, use hull.simplices to get fence points
B. Import ConvexHull, create numpy array of points, compute hull, use hull.vertices to get fence points
C. Import ConvexHull, compute hull, use hull.area to get fence points
D. Import ConvexHull, compute hull, use hull.points to get fence points

Solution

  1. Step 1: Prepare data and compute hull

    Import ConvexHull, convert city coordinates to numpy array, then compute hull = ConvexHull(points).
  2. Step 2: Extract fence points using hull.vertices

    hull.vertices gives indices of points forming the convex hull boundary (fence).
  3. Final Answer:

    Import ConvexHull, create numpy array of points, compute hull, use hull.vertices to get fence points -> Option B
  4. Quick Check:

    hull.vertices = fence points [OK]
Hint: Use hull.vertices for boundary points, not hull.area or hull.points [OK]
Common Mistakes:
  • Using hull.area which is a number, not points
  • Using hull.points which is all points, not boundary
  • Sorting points manually instead of using hull.vertices
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard