💚
Beginner-Friendly Topic
Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on. You've got this! 🚀
Practice
Practice the key concepts from Module 2 with a hands-on coding exercise.
15 min•By Priygop Team•Updated 2026
Practice: Complete AI Learning Cycle
Practice: Complete AI Learning Cycle
# Practice: Build a simple grade predictor using the full AI cycle
# Step 1: Data, Step 2: Learn, Step 3: Predict, Step 4: Evaluate
import random
# Step 1: Generate training data
# Features: [study_hours, sleep_hours_before_exam]
# Label: exam_grade (A, B, C, D)
def generate_grade(study, sleep):
score = study * 8 + sleep * 4
if score >= 80:
return "A"
elif score >= 60:
return "B"
elif score >= 40:
return "C"
else:
return "D"
# Create training dataset
all_data = []
random.seed(42)
for _ in range(50):
study = random.randint(1, 10)
sleep = random.randint(4, 10)
grade = generate_grade(study, sleep)
all_data.append(([study, sleep], grade))
# Step 2: Train (80% data)
split = int(len(all_data) * 0.8)
train_data = all_data[:split]
test_data = all_data[split:]
print(f"Training examples: {len(train_data)}")
print(f"Testing examples: {len(test_data)}")
# Step 3: Learn patterns (calculate threshold averages per grade)
grade_study_avg = {}
for features, grade in train_data:
if grade not in grade_study_avg:
grade_study_avg[grade] = []
grade_study_avg[grade].append(features[0])
print()
print("Average study hours per grade (learned from training data):")
for grade in sorted(grade_study_avg.keys()):
avg = sum(grade_study_avg[grade]) / len(grade_study_avg[grade])
print(f" Grade {grade}: {avg:.1f} hours average study time")
# Step 4: Simple predictor
def predict_grade(study_hours, sleep_hours):
score = study_hours * 8 + sleep_hours * 4
if score >= 80:
return "A"
elif score >= 60:
return "B"
elif score >= 40:
return "C"
else:
return "D"
# Step 5: Evaluate
correct = sum(
1 for features, actual in test_data
if predict_grade(features[0], features[1]) == actual
)
accuracy = correct / len(test_data) * 100
print()
print(f"Model accuracy on test data: {accuracy:.1f}%")
print("Practice complete!")Diagram
Loading diagram…
Deep Learning ⊂ Machine Learning ⊂ Artificial Intelligence
Key Takeaways from This Module
- Machine learning means a computer finds patterns in data and uses those patterns to make predictions
- Data is examples with features (inputs) and labels (correct answers)
- Training data is what the AI learns from. Testing data is what we use to measure performance
- Features are the measurable inputs. Labels are the correct outputs the model is trying to predict
- Patterns are consistent relationships between features and labels found across many examples
- Rules-based programming writes rules manually. Machine learning discovers rules from data
- Data quality is critical: the AI is only as good as the data it learns from
- Common mistakes: overfitting, underfitting, data leakage, biased data, and wrong evaluation metrics
Key Takeaways
- Practice the key concepts from Module 2 with a hands-on coding exercise.
- Machine learning means a computer finds patterns in data and uses those patterns to make predictions
- Data is examples with features (inputs) and labels (correct answers)
- Training data is what the AI learns from. Testing data is what we use to measure performance