Welcome to Module 9, @name! Darwin here with Machine Learning - the science of machines that learn! 🤖🧠
Safari Analogy: Machine Learning is like training Safari animals - an algorithm learns from examples, just like a lion cub learns to hunt by watching its mother. The more examples, the better the predictions!
Machine Learning (ML) is a field of AI where algorithms learn patterns from data without being explicitly programmed with rules.
1# Traditional programming
2def is_lion(animal):
3 if animal.has_mane and animal.color == 'golden':
4 return True
5 return False
6
7# Machine Learning
8# The model learns to recognize lions from THOUSANDS of images
9# without defining rules - it discovers them on its own!The model learns from labeled data - it knows the correct answers.
1# Training data with labels
2X_train = [
3 [180, 200, 4], # weight, length, legs
4 [5000, 400, 4],
5 [50, 150, 4],
6]
7y_train = ['lion', 'elephant', 'cheetah'] # labels
8
9# The model learns the mapping: features -> label
10# Then predicts for new dataApplications:
The model discovers patterns in unlabeled data on its own.
1# Data without labels
2animals = [
3 [180, 200, 'savanna'],
4 [5000, 400, 'forest'],
5 [50, 150, 'savanna'],
6 [3000, 300, 'forest'],
7]
8
9# The model discovers groups (clusters) on its own
10# e.g. "savanna animals" vs "forest animals"Applications:
An agent learns through trial and error - receiving rewards for good decisions.
1# Agent (Safari robot) in an environment
2# Actions: go_right, go_left, stop
3# Reward: +10 for finding an animal, -1 for each step
4
5# The agent learns the optimal exploration strategyApplications:
1# Dataset
2X = [[1, 2], [3, 4], [5, 6]] # Features
3y = [0, 1, 0] # Labels
4
5# Training set - data for learning (70-80%)
6# Validation set - data for tuning (10-15%)
7# Test set - data for final evaluation (10-20%)
8
9# Model - an algorithm that learns from data
10# Prediction - forecasting for new data
11# Accuracy - % of correct predictions1# 1. Collect data
2data = load_safari_data()
3
4# 2. Prepare data (EDA, cleaning)
5X = data.drop('species', axis=1)
6y = data['species']
7
8# 3. Split into train/test
9from sklearn.model_selection import train_test_split
10X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
11
12# 4. Select and train a model
13from sklearn.ensemble import RandomForestClassifier
14model = RandomForestClassifier()
15model.fit(X_train, y_train)
16
17# 5. Evaluate the model
18accuracy = model.score(X_test, y_test)
19print(f"Accuracy: {accuracy:.2%}")
20
21# 6. Use the model for predictions
22predictions = model.predict(new_data)