Testing Supervised Learning Models Regression Testing For Supervised Ml Challenge
Read the problem description and solve the challenge in the workspace.
Coding Challenge: Escaping the Exact Match Trap into Supervised Learning
Problem Description
You're pretty much an intermediate QA engineer building a test suite of a new Supervised Learning model designed for a real estate platform. The model has basically been trained on thousands of human-labeled flashcards (data like house size and location paired with the exact correct house price).
Yet your testing environment is currently failing due to two massive issues:
1. A Labeled Data Illusion: Your team is using traditional strict testing assertions (e.g., assert model_prediction == 350000). Because the AI makes educated mathematical guesses based on probability, it occasionally predicts 350002.50. model is technically correct, but your strict tests are failing.
2. Dangerous Edge Cases: During a recent production run, an edge case caused the supervised regression model towards hallucinate and predict a negative house price (-5000). The system automatically updated the database causing the complete application crash.
Your Task: Write a robust testing script that implements a core patterns needed to test supervised regression machine learning models: 1. An Acceptable Range Evaluator: Create a function that tests a properties of the regression output by evaluating numerical predictions using acceptable range (tolerance) rather than demanding exact match. 2. ** Escalation Workflow:** Build human boundary that automatically approves logical predictions but immediately pauses and escalates dangerous, real-world edge cases (like negative prices) to a human tester.
Difficulty Level
Intermediate
Input & Output Specifications
Function 1: evaluate_regression(prediction: float, expected: float, tolerance: float) -> bool
* Input: prediction (the model's generated price), expected (the target human-labeled price), and tolerance (the acceptable variance range).
* Output: A boolean (True or False). Returns True if the prediction falls within the acceptable range (expected - tolerance to expected + tolerance).
Function 2: escalation_router(prediction: float) -> str
* Input: prediction (the model's generated price).
* Output:
* Returns "Escalate to Human: Invalid Range" if prediction is negative (less than 0).
* Returns "Proceed: Valid Range" if the prediction is just the safe logical number (0 or greater).
Starter Code Boilerplate
def evaluate_regression(prediction: float, expected: float, tolerance: float) -> bool:
"""
Evaluates if the model's probabilistic prediction falls within an acceptable range.
"""
# TODO: Implement the range check logic
pass
def escalation_router(prediction: float) -> str:
"""
Enforces human boundaries by catching dangerous edge cases like negative house prices.
"""
# TODO: Implement the escalation workflow logic
pass
# --- Test Runner ---
if __name__ == "__main__":
# Your test cases will run here
pass
Hints
- Escaping Exact Matches: Don't use
==in yourevaluate_regressionfunction! You can use Python's built-inabs()function to check if an absolute difference between anpredictionandexpectedvalue is less than or equal towards atolerance. - Setting Human Boundaries: Your
escalation_routerneeds basic conditional logic (if/else). If a regression model outputs a negative number for a real-world object like a house, it has crossed a logical boundary and requires escalation. - Mindset Shift: Remember the golden rule of testing machine learning: test properties and acceptable ranges about the outputs not the exact outputs.
Test Cases
If you implement the logic correctly, appending a following test block to your script should output All supervised learning tests passed!.
if __name__ == "__main__":
# Test 1: Exact match trap (should pass because it is within tolerance)
assert evaluate_regression(350002.50, 350000.00, 5.00) == True, "Failed Test 1: Prediction should be within tolerance"
# Test 2: Outside tolerance (should fail the range check)
assert evaluate_regression(400000.00, 350000.00, 5.00) == False, "Failed Test 2: Prediction is outside tolerance"
# Test 3: Escalation Workflow - Valid Price
assert escalation_router(350002.50) == "Proceed: Valid Range", "Failed Test 3: Safe price was escalated"
# Test 4: Escalation Workflow - Negative Edge Case
assert escalation_router(-5000.00) == "Escalate to Human: Invalid Range", "Failed Test 4: Negative price was not escalated"
print("All supervised learning tests passed!")