Python for ML
45 min

Control Flow in Python

Master conditionals, loops, and program flow for ML preprocessing

Progress20%

Conditional Statements

Conditional statements let your program make decisions. In ML, you'll use them for:

- Data validation and cleaning - Feature engineering (creating new features based on conditions) - Handling edge cases in model predictions

### The if-elif-else Structure

# Basic if statement
age = 25
if age >= 18:
    print("Adult")

# if-else score = 0.75 if score >= 0.5: print("Passed") else: print("Failed")

# if-elif-else chain accuracy = 0.92 if accuracy >= 0.95: grade = "A" elif accuracy >= 0.85: grade = "B" elif accuracy >= 0.75: grade = "C" else: grade = "F" print(f"Model grade: {grade}") # Output: B

### Comparison Operators

Operator | Meaning |----------|---------| == | Equal to != | Not equal to < | Less than > | Greater than <= | Less than or equal >= | Greater than or equal

### Logical Operators

Combine multiple conditions:

x = 10
y = 20

# AND - both must be True if x > 5 and y < 30: print("Both conditions met")

# OR - at least one must be True if x > 15 or y > 15: print("At least one condition met")

# NOT - inverts the condition is_error = False if not is_error: print("No errors!")

### Practical ML Example

def classify_confidence(confidence_score):
    """Classify prediction confidence level"""
    if confidence_score >= 0.9:
        return "high", True  # High confidence, use prediction
    elif confidence_score >= 0.7:
        return "medium", True  # Medium confidence, use with caution
    elif confidence_score >= 0.5:
        return "low", False  # Low confidence, maybe reject
    else:
        return "very_low", False  # Very low, definitely reject

# Test the function predictions = [0.95, 0.82, 0.61, 0.35] for pred in predictions: level, use = classify_confidence(pred) print(f"Score {pred}: {level} confidence - Use: {use}")

Hands-On Exercise: Build a Data Validator

Create a function that validates and cleans a list of ML samples

def validate_samples(samples, min_features=2):
    """
    Validate and clean ML samples.
    
    Requirements:
    1. Remove samples with fewer than min_features
    2. Remove samples where any value is None
    3. Remove samples where any numeric value is negative
    4. Return (valid_samples, removed_count)
    
    Example:
        samples = [
            {"a": 1, "b": 2},
            {"a": None, "b": 3},
            {"a": -1, "b": 2},
            {"a": 1}
        ]
        validate_samples(samples, min_features=2)
        # Returns: ([{"a": 1, "b": 2}], 3)
    """
    # YOUR CODE HERE
    valid = []
    removed = 0
    
    # Loop through each sample
    
    # Check conditions:
    # 1. len(sample) >= min_features
    # 2. No None values
    # 3. No negative numbers
    
    return valid, removed


# Test your function
test_data = [
    {"x": 10, "y": 20, "z": 30},
    {"x": 5, "y": None},
    {"x": -5, "y": 10},
    {"x": 100, "y": 200},
    {"x": 50},
]

valid, removed = validate_samples(test_data, min_features=2)
print(f"Valid samples: {len(valid)}")
print(f"Removed: {removed}")
print("Valid data:", valid)

Knowledge Check

Quiz

Question 1 of 5

What is the output of: [x**2 for x in range(4) if x % 2 == 0]

Next Lesson

Functions in Python

Learn to write reusable code with functions