🤖 Intro to Machine Learning

Unit 9 — ML, SQL, APIs & Web Scraping. Real-world Python skills.

What is Machine Learning?

Machine Learning is teaching computers to learn from data without being explicitly programmed for every case. Instead of writing rules, you show the computer examples and it figures out the patterns.

💡 Analogy: Traditional programming = "If price > 100, label as expensive." ML = Show the computer 10,000 examples of expensive/cheap items and let it figure out the rule itself.

Types of ML

  • Supervised Learning — Learn from labeled examples (most common). Predict house prices, classify spam.
  • Unsupervised Learning — Find patterns in unlabeled data. Clustering customers by behavior.
  • Reinforcement Learning — Learn by trial and error with rewards. Games, robotics.

The ML Workflow

python
# The ML workflow — every project follows this pattern # 1. Get data # 2. Explore & clean data (EDA) # 3. Choose features (columns used to predict) # 4. Split: train set + test set # 5. Train a model on train set # 6. Evaluate on test set # 7. Improve & iterate import pandas as pd from sklearn.model_selection import train_test_split # Example: house price dataset df = pd.DataFrame({ 'size': [50, 80, 120, 200, 300], 'rooms': [1, 2, 3, 4, 5], 'price': [250, 400, 600, 950, 1400] # in thousands }) X = df[['size', 'rooms']] # Features (inputs) y = df['price'] # Target (what we predict) X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.2, random_state=42 ) print("Train:", len(X_train), "Test:", len(X_test))
Output
Train: 4 Test: 1

Key Vocabulary

  • Features (X) — Input columns used to make predictions
  • Target (y) — The column we're trying to predict
  • Training set — Data the model learns from (usually 80%)
  • Test set — Data to evaluate how well the model performs (20%)
  • Overfitting — Model memorized training data but fails on new data
🎯 TASKExplore the Iris Dataset
Load the built-in Iris dataset from sklearn. Print its shape, feature names, and target names. Then split it into train/test (80/20).
Starter code:
python
from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split iris = load_iris() # Print: shape, feature_names, target_names print(???) X_train, X_test, y_train, y_test = train_test_split(???)

🔧 Scikit-learn Basics

The most important Python ML library.

What is Scikit-learn?

Scikit-learn (sklearn) is Python's most popular ML library. It has consistent API across all algorithms: every model has the same fit(), predict(), and score() methods.

python
from sklearn.linear_model import LinearRegression from sklearn.tree import DecisionTreeClassifier from sklearn.ensemble import RandomForestClassifier # All sklearn models follow the same pattern: # 1. Create model model = LinearRegression() # 2. Train (fit) on training data model.fit(X_train, y_train) # 3. Predict on new data predictions = model.predict(X_test) # 4. Evaluate score = model.score(X_test, y_test) print(f"R² score: {score:.3f}")

sklearn's Main Modules

  • sklearn.linear_model — Linear/Logistic Regression
  • sklearn.tree — Decision Trees
  • sklearn.ensemble — Random Forest, Gradient Boosting
  • sklearn.preprocessing — Scale, encode, transform data
  • sklearn.model_selection — train_test_split, cross-validation
  • sklearn.metrics — accuracy, MSE, confusion matrix

Preprocessing — Scaling Features

python
from sklearn.preprocessing import StandardScaler import numpy as np # Raw data: salary (60000-120000) vs age (25-55) # These scales are very different — scaling equalizes them X = np.array([[25, 60000], [35, 80000], [45, 120000]]) scaler = StandardScaler() X_scaled = scaler.fit_transform(X) print("Original:\n", X) print("\nScaled:\n", X_scaled.round(2))
Output
Original: [[ 25 60000] [ 35 80000] [ 45 120000]] Scaled: [[-1.22 -1.07] [ 0. -0.27] [ 1.22 1.34]]
📌 Rule: Always fit the scaler on training data only, then transform both train and test. Never fit on test data — this causes data leakage.
🎯 TASKScale the Iris Dataset
Load the Iris dataset, split into train/test, then apply StandardScaler. Print the mean and std of the scaled training features (should be ~0 and ~1).
Starter code:
python
from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler iris = load_iris() X_train, X_test, y_train, y_test = train_test_split( iris.data, iris.target, test_size=0.2, random_state=42) scaler = StandardScaler() X_train_s = ??? # fit_transform on train X_test_s = ??? # transform only on test print("Mean:", X_train_s.mean(axis=0).round(2))

📈 Linear Regression

Predicting continuous values.

What is Linear Regression?

Linear Regression finds the best straight line through your data to predict a continuous number. It answers: "Given house size, what is the predicted price?"

python
import numpy as np import pandas as pd from sklearn.linear_model import LinearRegression from sklearn.model_selection import train_test_split from sklearn.metrics import mean_squared_error, r2_score # Generate sample data: house size vs price np.random.seed(42) size = np.random.randint(50, 300, 100) noise = np.random.normal(0, 30, 100) price = 3.5 * size + 80 + noise # price = 3.5×size + 80 + noise X = size.reshape(-1, 1) # sklearn needs 2D array y = price X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) # Train model model = LinearRegression() model.fit(X_train, y_train) # Results print(f"Coefficient: {model.coef_[0]:.2f} (slope)") print(f"Intercept: {model.intercept_:.2f}") print(f"Formula: price = {model.coef_[0]:.1f} × size + {model.intercept_:.0f}") # Predict y_pred = model.predict(X_test) print(f"\nR² Score: {r2_score(y_test, y_pred):.3f}") print(f"RMSE: {mean_squared_error(y_test, y_pred)**0.5:.1f}")
Output
Coefficient: 3.50 (slope) Intercept: 80.68 Formula: price = 3.5 × size + 81 R² Score: 0.978 RMSE: 29.4

Metrics: R² and RMSE

  • R² (R-squared) — How much variance the model explains. 1.0 = perfect, 0 = no better than average. Aim for > 0.85.
  • RMSE (Root Mean Squared Error) — Average prediction error in the same unit as y. Lower is better.
💡 Rule of thumb: R² > 0.9 = excellent, 0.7-0.9 = good, < 0.7 = consider other models or more features.
🎯 TASKPredict Salary from Years of Experience
Create a dataset with 50 people: years_experience (1-20) and salary (roughly 30000 + 5000×years + noise). Train a LinearRegression model and predict the salary for someone with 10 years experience. Print R² score.
Starter code:
python
import numpy as np from sklearn.linear_model import LinearRegression from sklearn.model_selection import train_test_split from sklearn.metrics import r2_score np.random.seed(0) years = np.random.randint(1, 21, 50) salary = 30000 + 5000 * years + np.random.normal(0, 3000, 50) X = years.reshape(-1, 1) # Split, train, predict salary for [[10]], print R²

🔀 Logistic Regression

Classification — predicting categories.

Classification vs Regression

When the answer is a category (spam/not spam, fraud/normal, pass/fail) — use classification. Logistic Regression is the simplest classifier.

python
from sklearn.linear_model import LogisticRegression from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split from sklearn.metrics import accuracy_score, classification_report iris = load_iris() X_train, X_test, y_train, y_test = train_test_split( iris.data, iris.target, test_size=0.2, random_state=42) model = LogisticRegression(max_iter=200) model.fit(X_train, y_train) y_pred = model.predict(X_test) print(f"Accuracy: {accuracy_score(y_test, y_pred):.1%}") print("\nDetailed report:") print(classification_report(y_test, y_pred, target_names=iris.target_names))
Output
Accuracy: 100.0% Detailed report: precision recall f1-score setosa 1.00 1.00 1.00 versicolor 1.00 1.00 1.00 virginica 1.00 1.00 1.00

Understanding the Classification Report

  • Precision — Of all predicted positives, how many were actually positive? (avoid false alarms)
  • Recall — Of all actual positives, how many did we catch? (avoid missing cases)
  • F1-score — Balance between precision and recall. Use when classes are imbalanced.
🎯 TASKCancer Diagnosis Classifier
Using the breast cancer dataset (sklearn.datasets.load_breast_cancer), train a LogisticRegression model and print accuracy + classification report. This dataset has 2 classes: malignant / benign.
Starter code:
python
from sklearn.datasets import load_breast_cancer from sklearn.linear_model import LogisticRegression from sklearn.model_selection import train_test_split from sklearn.metrics import accuracy_score, classification_report from sklearn.preprocessing import StandardScaler data = load_breast_cancer() # 1. Split data into train/test # 2. Scale features using StandardScaler # 3. Train LogisticRegression(max_iter=1000) # 4. Print accuracy and classification_report

🌳 Decision Trees & Random Forest

The most powerful and interpretable models.

Decision Trees

A Decision Tree splits data by asking yes/no questions. Random Forest = 100s of trees voting together — much more accurate and robust.

python
from sklearn.ensemble import RandomForestClassifier from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split from sklearn.metrics import accuracy_score import pandas as pd iris = load_iris() X_train, X_test, y_train, y_test = train_test_split( iris.data, iris.target, test_size=0.2, random_state=42) # Random Forest = many decision trees rf = RandomForestClassifier(n_estimators=100, random_state=42) rf.fit(X_train, y_train) print(f"Accuracy: {accuracy_score(y_test, rf.predict(X_test)):.1%}") # Feature importance — which features matter most? importance = pd.DataFrame({ 'feature': iris.feature_names, 'importance': rf.feature_importances_ }).sort_values('importance', ascending=False) print("\nFeature Importance:") print(importance.to_string(index=False))
Output
Accuracy: 100.0% Feature Importance: feature importance petal length (cm) 0.44 petal width (cm) 0.42 sepal length (cm) 0.09 sepal width (cm) 0.05
💡 Random Forest tip: Start with n_estimators=100 for most problems. Feature importance tells you which columns actually matter — use this to simplify your model.
🎯 TASKCompare Models on Wine Dataset
Load sklearn.datasets.load_wine. Compare accuracy of LogisticRegression, DecisionTreeClassifier, and RandomForestClassifier. Which one wins?
Starter code:
python
from sklearn.datasets import load_wine from sklearn.linear_model import LogisticRegression from sklearn.tree import DecisionTreeClassifier from sklearn.ensemble import RandomForestClassifier from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler from sklearn.metrics import accuracy_score wine = load_wine() X_tr, X_te, y_tr, y_te = train_test_split( wine.data, wine.target, test_size=0.2, random_state=42) scaler = StandardScaler() X_tr = scaler.fit_transform(X_tr) X_te = scaler.transform(X_te) models = { "Logistic": LogisticRegression(max_iter=500), "Decision Tree": DecisionTreeClassifier(random_state=42), "Random Forest": RandomForestClassifier(n_estimators=100, random_state=42) } # Loop through models, fit each one, and print test accuracy.

📊 Model Evaluation

How to properly measure model performance.

The Confusion Matrix

python
from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay from sklearn.ensemble import RandomForestClassifier from sklearn.datasets import load_breast_cancer from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler data = load_breast_cancer() X_tr, X_te, y_tr, y_te = train_test_split(data.data, data.target, test_size=0.2, random_state=42) sc = StandardScaler() X_tr = sc.fit_transform(X_tr); X_te = sc.transform(X_te) rf = RandomForestClassifier(100, random_state=42) rf.fit(X_tr, y_tr) y_pred = rf.predict(X_te) cm = confusion_matrix(y_te, y_pred) print("Confusion Matrix:") print(cm) print(f"\nTrue Positives: {cm[1,1]}") print(f"False Positives: {cm[0,1]} (predicted cancer, was benign)") print(f"False Negatives: {cm[1,0]} (missed cancer!)")
Output
Confusion Matrix: [[40 3] [ 1 70]] True Positives: 70 False Positives: 3 (predicted cancer, was benign) False Negatives: 1 (missed cancer!)

Cross-Validation

python
from sklearn.model_selection import cross_val_score import numpy as np # 5-fold cross validation — more reliable than a single split scores = cross_val_score(rf, data.data, data.target, cv=5, scoring='accuracy') print(f"CV Scores: {scores.round(3)}") print(f"Mean: {scores.mean():.3f} ± {scores.std():.3f}")
Output
CV Scores: [0.956 0.965 0.956 0.974 0.965] Mean: 0.963 ± 0.006
🎯 TASKEvaluate with Cross-Validation
Load the digits dataset (load_digits). Run 5-fold cross-validation with RandomForestClassifier and print mean accuracy ± std. This dataset has 10 classes (digits 0-9).
Starter code:
python
from sklearn.datasets import load_digits from sklearn.ensemble import RandomForestClassifier from sklearn.model_selection import cross_val_score digits = load_digits() model = RandomForestClassifier(n_estimators=100, random_state=42) # Run 5-fold cross-validation. # Print the mean accuracy and standard deviation.

⚙️ Feature Engineering

Creating better inputs for your model.

What is Feature Engineering?

"Garbage in, garbage out." The quality of your features determines the quality of your model more than the algorithm itself.

python
import pandas as pd from sklearn.preprocessing import LabelEncoder, OneHotEncoder df = pd.DataFrame({ 'age': [25, 35, 45, 22, 58], 'city': ['Cairo', 'Alex', 'Cairo', 'Giza', 'Alex'], 'salary': [45000, 72000, 95000, 38000, 88000] }) # 1. Label Encoding (for ordinal/binary categories) le = LabelEncoder() df['city_encoded'] = le.fit_transform(df['city']) # 2. One-Hot Encoding (for nominal categories) df_ohe = pd.get_dummies(df, columns=['city'], prefix='city') print(df_ohe.head()) # 3. Create new features df['salary_per_age'] = df['salary'] / df['age'] df['is_senior'] = (df['age'] >= 40).astype(int) print("\nNew features:") print(df[['age','salary','salary_per_age','is_senior']])

Handling Missing Values for ML

python
from sklearn.impute import SimpleImputer import numpy as np X = np.array([[1, 2], [np.nan, 3], [7, np.nan]]) imp = SimpleImputer(strategy='mean') # fill NaN with column mean X_filled = imp.fit_transform(X) print(X_filled)
Output
[[1. 2. ] [4. 3. ] [7. 2.5]]
🎯 TASKEngineer Features from Date
Create a DataFrame with 100 random sales dates (2023-2024) and amounts. Extract: month, day_of_week, is_weekend, quarter. Then check which feature correlates most with amount using .corr().
Starter code:
python
import pandas as pd import numpy as np np.random.seed(42) dates = pd.date_range('2023-01-01', '2024-12-31', periods=100) df = pd.DataFrame({ 'date': dates, 'amount': np.random.randint(100, 5000, 100) }) # Create these columns: # month, day_of_week, is_weekend, quarter # Then calculate correlation with amount.

🗄️ SQL with Python

Query databases directly from Python.

Why SQL + Python?

Most real-world data lives in databases. Python's sqlite3 module lets you query SQLite databases, and pandas can read SQL queries directly.

python
import sqlite3 import pandas as pd # Create in-memory database conn = sqlite3.connect(':memory:') # Create a table and insert data conn.executescript(""" CREATE TABLE sales ( id INTEGER PRIMARY KEY, product TEXT, amount REAL, month INTEGER, region TEXT ); INSERT INTO sales VALUES (1,'Laptop',1200,1,'Cairo'), (2,'Phone',800,1,'Alex'), (3,'Laptop',1100,2,'Cairo'), (4,'Tablet',600,2,'Giza'), (5,'Phone',850,3,'Cairo'), (6,'Laptop',1300,3,'Alex'); """) # Query with pandas df = pd.read_sql_query("SELECT * FROM sales", conn) print(df)
Output
id product amount month region 0 1 Laptop 1200.0 1 Cairo 1 2 Phone 800.0 1 Alex ...

SQL Queries with pandas

python
# GROUP BY — total sales per product q1 = pd.read_sql_query(""" SELECT product, SUM(amount) as total, COUNT(*) as orders FROM sales GROUP BY product ORDER BY total DESC """, conn) print("Sales by product:\n", q1) # WHERE + aggregate q2 = pd.read_sql_query(""" SELECT region, AVG(amount) as avg_sale FROM sales WHERE month >= 2 GROUP BY region """, conn) print("\nAvg sale by region (from month 2):\n", q2) # Write DataFrame back to SQL df_new = pd.DataFrame({'product':['Camera'],'amount':[950],'month':[4],'region':['Giza'],'id':[7]}) df_new.to_sql('sales', conn, if_exists='append', index=False) print("\nRows after insert:", pd.read_sql_query("SELECT COUNT(*) FROM sales", conn).iloc[0,0])
🎯 TASKEmployee Database Query
Create an SQLite table 'employees' with columns: name, department, salary, hire_year. Insert 8 employees. Query: (1) average salary per department, (2) employees hired after 2020, (3) the top earner.
Starter code:
python
import sqlite3 import pandas as pd conn = sqlite3.connect(':memory:') conn.executescript(""" CREATE TABLE employees ( name TEXT, department TEXT, salary REAL, hire_year INT ); INSERT INTO employees VALUES ('Ahmed','Engineering',85000,2019), ('Sara','Marketing',72000,2021), ('Mohamed','Engineering',92000,2018), ('Nour','HR',65000,2022), ('Karim','Marketing',78000,2020), ('Dina','Engineering',88000,2023), ('Omar','HR',60000,2019), ('Layla','Marketing',82000,2021); """) # Write 3 SQL queries: # 1. Average salary per department # 2. Employees hired after 2020 # 3. The top earner

🌐 REST APIs & requests

Fetch live data from the internet.

What is a REST API?

APIs let programs talk to each other over HTTP. REST APIs return data (usually JSON) when you call a URL. Python's requests library makes this simple.

python
import requests # GET request — fetch data from an API response = requests.get('https://api.coinbase.com/v2/prices/BTC-USD/spot') print("Status:", response.status_code) # 200 = success print("Headers:", dict(list(response.headers.items())[:3])) # Parse JSON response data = response.json() print("\nBitcoin price:", data['data']['amount'], data['data']['currency'])

Working with JSON APIs + pandas

python
import requests import pandas as pd # Public API: Random user data response = requests.get('https://randomuser.me/api/?results=5') data = response.json() users = [] for user in data['results']: users.append({ 'name': user['name']['first'] + ' ' + user['name']['last'], 'country': user['location']['country'], 'age': user['dob']['age'], 'email': user['email'] }) df = pd.DataFrame(users) print(df)

API Authentication & Headers

python
import requests # Many APIs require an API key headers = { 'Authorization': 'Bearer YOUR_API_KEY', 'Content-Type': 'application/json' } params = {'country': 'EG', 'limit': 10} # Example pattern (don't run — needs real key) # response = requests.get('https://api.example.com/data', # headers=headers, params=params) # POST request — send data to an API payload = {'name': 'Ahmed', 'score': 95} # response = requests.post('https://api.example.com/scores', # json=payload, headers=headers) # Error handling try: r = requests.get('https://httpbin.org/status/404', timeout=5) r.raise_for_status() # Raises exception for 4xx/5xx except requests.exceptions.HTTPError as e: print(f"HTTP Error: {e}") except requests.exceptions.Timeout: print("Request timed out")
🎯 TASKFetch & Analyze Country Data
Use the REST Countries API (restcountries.com/v3.1/region/africa) to fetch all African countries. Create a DataFrame with name, capital, population, and area. Then find the top 5 most populous countries.
Playground note: Live API requests may be limited by browser CORS. If a request fails in the Playground, run the same code locally in Jupyter or VS Code.
Starter code:
python
import requests import pandas as pd url = 'https://restcountries.com/v3.1/region/africa' r = requests.get(url, timeout=10) r.raise_for_status() countries = [] for c in r.json(): countries.append({ 'name': c['name']['common'], # Add capital, population, and area_km2 }) df = pd.DataFrame(countries) # Print top 5 countries by population.

🕷️ Web Scraping

Extract data from websites automatically.

Web Scraping with BeautifulSoup

When there's no API, you can scrape the HTML directly. requests fetches the page, BeautifulSoup parses the HTML.

python
import requests from bs4 import BeautifulSoup import pandas as pd # Fetch a page url = 'https://books.toscrape.com/' response = requests.get(url) soup = BeautifulSoup(response.text, 'html.parser') print("Title:", soup.title.text) print("H1:", soup.find('h1').text if soup.find('h1') else 'N/A')

Extracting Structured Data

python
import requests from bs4 import BeautifulSoup import pandas as pd # books.toscrape.com is a practice scraping site url = 'https://books.toscrape.com/' soup = BeautifulSoup(requests.get(url).text, 'html.parser') books = [] for article in soup.select('article.product_pod'): title = article.h3.a['title'] price = article.select_one('.price_color').text.strip() rating = article.p['class'][1] # "One", "Two", etc. books.append({'title': title, 'price': price, 'rating': rating}) df = pd.DataFrame(books) print(df.head())

Scraping Multiple Pages

python
import requests, time from bs4 import BeautifulSoup import pandas as pd all_books = [] for page in range(1, 4): # First 3 pages url = f'https://books.toscrape.com/catalogue/page-{page}.html' soup = BeautifulSoup(requests.get(url).text, 'html.parser') for art in soup.select('article.product_pod'): all_books.append({ 'title': art.h3.a['title'], 'price': float(art.select_one('.price_color').text.replace('£','').replace('£','')) }) time.sleep(0.5) # Be polite — don't hammer the server! df = pd.DataFrame(all_books) print(f"Scraped {len(df)} books") print(f"Avg price: £{df['price'].mean():.2f}") print(df.nlargest(3,'price')[['title','price']])
📌 Ethics: Always check robots.txt, respect rate limits, and don't scrape private/copyrighted data. Use APIs when available.
🎯 TASKScrape & Analyze Quotes
Scrape quotes.toscrape.com — get all quotes from the first 3 pages. Create a DataFrame with: quote_text, author, tags. Then find the most frequent author and the most common tag.
Playground note: Scraping may be blocked by browser CORS. Treat this task as local Python practice if the Playground cannot fetch the page.
Starter code:
python
import requests import time import pandas as pd from bs4 import BeautifulSoup quotes = [] for page in range(1, 4): url = f'https://quotes.toscrape.com/page/{page}/' soup = BeautifulSoup(requests.get(url, timeout=10).text, 'html.parser') # Loop through .quote elements and append: # quote_text, author, tags time.sleep(0.3) df = pd.DataFrame(quotes) # Find the most frequent author and most common tag.