Chapter 8 Real-World Patterns: Practical Scenarios

8.1 Introduction

Theory is great. Reality is messier. This chapter shows 5 real scenarios and how to handle them.

8.2 Pattern 1: The “It Works in Dev, Fails in Prod” Problem

8.2.1 The Scenario

You built a model in a notebook. It works beautifully. You deploy it. It fails.

Common causes: - Dev data is different from production data - Python versions differ - Dependencies have different versions - Paths are hardcoded - Random seeds not set

8.2.2 The Solution

Ask Claude for help preparing for production:

I have a model that works in development.

Dev environment:
- Python 3.10
- pandas 2.0
- scikit-learn 1.3
- Data: CSV with 10K rows

Production environment:
- Python 3.9
- Hosted on AWS Lambda
- Data: 1M rows from database
- Must process in < 30 seconds

Help me:
1. Test on production-like data
2. Handle version differences
3. Optimize for speed
4. Add monitoring
5. Create fallback behavior

8.2.3 Key Practices

  • ✓ Test with production-sized data
  • ✓ Set all random seeds
  • ✓ Use relative paths
  • ✓ Version your dependencies (requirements.txt)
  • ✓ Add logging
  • ✓ Monitor in production

8.3 Pattern 2: The “Colleague Can’t Understand My Code” Problem

8.3.1 The Scenario

You wrote Python code. Your colleague opens it. They’re confused. This happens all the time in teams.

8.3.2 The Solution

Ask Claude to improve for clarity:

My colleague says this code is hard to understand:

[paste code]

Can you:
1. Add clear variable names
2. Break into smaller functions
3. Add comments explaining the logic
4. Add docstrings with examples
5. Simplify the logic if possible

Make it understandable for a junior data scientist.

8.3.3 Key Practices

  • ✓ Write for humans first, computers second
  • ✓ Name variables clearly
  • ✓ Keep functions small (< 15 lines)
  • ✓ Comment the “why”, not the “what”
  • ✓ Include examples

8.4 Pattern 3: The “Model Performance Degradation” Problem

8.4.1 The Scenario

Your model works great for 3 months. Then performance drops. Why?

Common causes: - Data distribution shifted - Seasonal effects - New data patterns - Bugs in data pipeline - Model training issues

8.4.2 The Solution

Ask Claude to help diagnose:

My model's accuracy dropped from 92% to 78%.

Setup:
- Built 3 months ago
- Retrained monthly
- Current data: [sample]
- Previous data: [sample]

Can you:
1. Compare the data distributions
2. Show which features changed
3. Suggest what went wrong
4. Recommend fixes
5. Show how to detect this early

What monitoring should I add?

8.4.3 Key Practices

  • ✓ Monitor model performance continuously
  • ✓ Track data quality metrics
  • ✓ Compare current vs historical data
  • ✓ Set up alerts for performance drops
  • ✓ Retrain regularly

8.5 Pattern 4: The “We Need to Scale This” Problem

8.5.1 The Scenario

Your analysis works on 100K rows. Now you need to handle 100M rows. How?

Common approaches: - Use Spark instead of Pandas - Use databases for filtering - Parallelize computation - Sample data for exploration

8.5.2 The Solution

Ask Claude for scaling strategy:

My analysis currently:
- Processes 100K rows in 5 minutes
- Uses pandas, single machine
- Loads entire dataset into memory

Now I need to handle 100M rows.

Options to consider:
- Spark on cluster
- DuckDB for SQL
- Pandas with chunking
- Cloud processing (BigQuery)

Which is best for:
1. One-time analysis?
2. Daily pipeline?
3. Interactive exploration?

Show example implementation for option [your choice].

8.5.3 Key Practices

  • ✓ Profile first (where’s the bottleneck?)
  • ✓ Sample for development
  • ✓ Use databases for filtering
  • ✓ Parallelize where possible
  • ✓ Choose right tools (Spark, DuckDB, etc)

8.6 Pattern 5: The “Unclear Requirements” Problem

8.6.1 The Scenario

Your manager says “build a dashboard” but you’re not sure what they want.

8.6.2 The Solution

Use Claude to help clarify:

I need to build a dashboard for [domain].

Requirements (unclear):
- "Show the data"
- "Make it easy to understand"
- "Something executives will like"

Who are the users?
- [Describe them]

What decisions will they make?
- [List decisions]

What data is available?
- [List sources]

Can you:
1. Help me ask clarifying questions
2. Define realistic requirements
3. Suggest what metrics matter
4. Show a prototype dashboard
5. Create a checklist for success

8.6.3 Key Practices

  • ✓ Clarify who the users are
  • ✓ Understand what decisions they’ll make
  • ✓ Know success criteria upfront
  • ✓ Iterate with feedback
  • ✓ Start simple, add complexity

8.7 General Approach for Any Situation

When stuck, use this framework:

1. DESCRIBE THE PROBLEM
   - What are you trying to do?
   - What's not working?
   - What have you tried?

2. PROVIDE CONTEXT
   - Environment (language, libraries, data size)
   - Constraints (time, money, skill level)
   - Success criteria (what does "solved" look like?)

3. SHOW EXAMPLES
   - Sample data
   - Expected output
   - Error messages

4. ASK SPECIFICALLY
   - "What's the root cause?"
   - "How would you approach this?"
   - "What am I missing?"
   - "How do I test this?"

5. ITERATE
   - Try the solution
   - Report what happened
   - Get refined answer

8.8 Exercise: Handle a Real Problem

Take a current problem you’re facing:

  1. Describe it following the framework above
  2. Ask Claude with full context
  3. Try the solution
  4. Report back (what worked, what didn’t)
  5. Iterate until solved

8.9 Key Takeaways

✅ Production is different from development
✅ Write code for humans to understand
✅ Monitor model performance continuously
✅ Profile before optimizing
✅ Clarify requirements early
✅ Always provide context
✅ Iterate when stuck