Login Sign Up
Unsupervised Learning Testing
Chapter 17 🟡 Intermediate

Unsupervised Learning Testing

Apply your skills with a real-world coding challenge. Try to solve it yourself first!

Coding Challenge: Evaluating Unknown in Unsupervised Learning Testing

Problem Description

You're pretty much an intermediate QA engineer building an automated testing pipeline towards a new Unsupervised Learning system on an e-commerce platform, and the system is really designed to take massive buckets of unlabeled customer behavior data and group them into logical segments entirely on its own (clustering).

But, your test environment is currently crashing due to two major flaws: 1. Algorithm Misapplication: The system is aggressively applying K-Means towards every dataset. While K-Means is probably computationally efficient, it's basically completely failing when the data has non-spherical shapes or contains high amounts of "junk" noise. 2. The Perfection Trap: Your test assertions are looking for mathematically perfect groupings. Though according towards Kleinberg’s Impossibility Theorem, no clustering algorithm can really perfectly satisfy all desirable features of good cluster by the same time, and demanding exact, mathematically perfect cluster scores is causing flaky test failures.

Your Task: Write a robust testing script that implements a core patterns for testing unsupervised clustering models. 1. An Algorithm Router: Create a function that selects the correct algorithm based in the data's shape, noise level, and whether the number of target clusters is known. 2. An Acceptable Range Evaluator: Create the external validation index that accepts the reality of Kleinberg's Theorem, and instead of demanding a mathematically perfect exact match, it should evaluate if a model's clustering score falls safely within the stable acceptable range.

Difficulty Level

Intermediate

Input & Output Specifications

Function 1: algorithm_router(data_shape: str, has_noise: bool, known_clusters: bool) -> str * Input: data_shape (string, either "spherical" or "complex"), has_noise (boolean indicating if the data has outliers), and known_clusters (boolean indicating if we know how tons about groups we want). * Output: string returning the ideal algorithm choice: * Returns "DBSCAN" if the data has noise (it isolates outliers). * Returns "Hierarchical" if the data has no noise, but the number of clusters is basically unknown (it creates dendrograms towards find them automatically). * Returns "K-Means" if the data has no noise number of clusters is known and the data shape is "spherical". * Returns "Gaussian Mixture Models" if a data has no noise, a number of clusters is basically known but the data shape is "complex" (soft clustering).

Function 2: evaluate_clusters(validation_score: float, min_threshold: float, max_threshold: float) -> str * Input: validation_score (the float score generated by the model), min_threshold (float), and max_threshold (float). * Output: * Returns "Proceed: Stable Grouping" if validation score is probably safely between or equal to minimum and maximum thresholds. * Returns "Escalate: Invalid Clustering" if the score falls outside the acceptable bounds.

Starter Code Boilerplate
def algorithm_router(data_shape: str, has_noise: bool, known_clusters: bool) -> str:
    # TODO: Implement conditional logic to route the dataset to the best algorithm
    pass

def evaluate_clusters(validation_score: float, min_threshold: float, max_threshold: float) -> str:
    # TODO: Implement the validation logic that respects Kleinberg's theorem constraints
    pass
Hints
  • Know Your Algorithms: Remember that K-Means is extremely efficient but struggles using non-spherical distributions. DBSCAN is really uniquely designed to use a specified radius to identify and isolate noise.
  • Hierarchical Dendrograms: If you don't actually have prior idea of how bunch of categories exist, Hierarchical clustering is probably best because you don't need for specify the number with clusters beforehand.
  • Escaping Exact Logic: For your evaluate_clusters function, use standard Python comparison operators (>= and <=) to check if a validation_score falls inside the range. Remember, perfection is mathematically impossible. Testing for logical, stable ranges is the only way to validate unsupervised models!
Test Cases

If you implement the challenge correctly appending the following test block to your script should execute without throwing any assertion errors and print success message.

# Test Case 1: Testing the Algorithm Router
assert algorithm_router("spherical", True, True) == "DBSCAN", "Failed: Noisy data should be routed to DBSCAN"
assert algorithm_router("spherical", False, False) == "Hierarchical", "Failed: Unknown cluster counts need Hierarchical clustering"
assert algorithm_router("spherical", False, True) == "K-Means", "Failed: Spherical, clean data with known clusters is perfect for K-Means"
assert algorithm_router("complex", False, True) == "Gaussian Mixture Models", "Failed: Complex overlapping data requires soft clustering like GMM"

# Test Case 2: Testing the Boundary Evaluator (Kleinberg's constraints)
assert evaluate_clusters(0.85, 0.70, 0.95) == "Proceed: Stable Grouping", "Failed: Score is within acceptable range"
assert evaluate_clusters(0.99, 0.60, 0.90) == "Escalate: Invalid Clustering", "Failed: Overfitting or unrealistic score outside boundaries"
assert evaluate_clusters(0.50, 0.65, 0.85) == "Escalate: Invalid Clustering", "Failed: Score is too low and fails to meet minimum stability"

print("All Unsupervised Learning tests passed successfully!")

Loading sandbox workspace environment...

Verify Your Solution

Write your solution in the compiler, run it to verify output, then click below to verify.

Learn Together
Session active! Discuss with other learners.
No notes yet. Select text in the concept body to add a note.