Study interactive :: Progress tools open in the Study Hub reader.

Introduction to Machine Learning

This guide covers understanding machine learning fundamentals, types, workflow, and applications.

ML for beginners curriculum map (this guide)

Use this list as a beginner track; each item links to a section with code in this file or in the next modules.

Table of Contents


What is Machine Learning?

Definition

Machine Learning (ML) is a subset of artificial intelligence that enables computers to learn and make decisions from data without being explicitly programmed for every task.

Simple Definition: Learning from data to make predictions.

Key Concept: Instead of writing explicit rules, we let the computer learn patterns from examples.

Traditional Programming vs Machine Learning

Traditional Programming:

Input Data + Rules/Program → Output

Machine Learning:

Input Data + Output → Rules/Model (learned)
Then: New Input Data → Model → Output

Example Comparison

Traditional Approach (Spam Detection):

# Hard-coded rules
def is_spam(email):
    if "free money" in email.lower():
        return True
    if "click here" in email.lower():
        return True
    # ... many more rules
    return False

Machine Learning Approach:

# Learn from examples
from sklearn.naive_bayes import MultinomialNB

# Train on labeled examples
model = MultinomialNB()
model.fit(training_emails, labels)  # Learn patterns

# Use learned model
prediction = model.predict(new_email)

Why Machine Learning?

  1. Complex Problems: Some problems are too complex for explicit rules
  2. Adaptability: Models can adapt to new patterns
  3. Scalability: Handle large amounts of data
  4. Pattern Recognition: Find patterns humans might miss
  5. Automation: Reduce manual work

Machine Learning Applications

Real-World Applications:

Machine Learning Workflow Summary

Standard ML Workflow:

Problem → Data → EDA → Features → Model → Evaluate → Improve

Detailed Steps:

  1. Problem: Define the problem and success criteria
  2. Data: Collect and prepare data
  3. EDA: Exploratory Data Analysis. Understand data patterns
  4. Features: Feature engineering and selection
  5. Model: Train machine learning model
  6. Evaluate: Assess model performance
  7. Improve: Iterate and optimize (hyperparameter tuning, feature engineering)

History and Evolution


Types of Machine Learning

1. Supervised Learning

Definition: Learning from labeled data (input-output pairs).

How it works:

Example:

# Training data
X_train = [[25], [30], [35], [40]]  # Features (age)
y_train = [50000, 60000, 70000, 80000]  # Labels (salary)

# Model learns: age → salary
# Then predicts salary for new age

Two Main Types:

A. Regression

# Example: Predicting house prices
from sklearn.linear_model import LinearRegression

# Features: size, bedrooms, location
# Target: price (continuous value)
model = LinearRegression()
model.fit(X_train, y_train)  # y_train is continuous
predicti  # Predict prices

B. Classification

# Example: Email spam detection
from sklearn.ensemble import RandomForestClassifier

# Features: email content
# Target: spam (0) or not spam (1)
model = RandomForestClassifier()
model.fit(X_train, y_train)  # y_train is categories
predicti  # Predict spam/not spam

Common Algorithms:

2. Unsupervised Learning

Definition: Learning from unlabeled data (only features, no labels).

How it works:

Example:

# Training data (no labels!)
X_train = [[25, 50000], [30, 60000], [35, 70000], [40, 80000]]
# No y_train - we don't know the groups

# Model discovers patterns/groups

Main Types:

A. Clustering

# Example: Customer segmentation
from sklearn.cluster import KMeans

# Features: purchase history, demographics
# No labels - discover customer groups
model = KMeans(n_clusters=3)
clusters = model.fit_predict(X)  # Assign customers to groups

B. Dimensionality Reduction

# Example: Reduce 100 features to 2 for visualization
from sklearn.decomposition import PCA

# Original: 1000 samples, 100 features
# Reduced: 1000 samples, 2 features (for plotting)
pca = PCA(n_components=2)
X_reduced = pca.fit_transform(X)

Common Algorithms:

3. Reinforcement Learning

Definition: Learning through interaction with an environment, receiving rewards or penalties.

How it works:

Example:

# Game playing AI
# Agent: AI player
# Environment: Game board
# Actions: Move pieces
# Rewards: Win (+1), Lose (-1), Draw (0)

# Agent learns: which moves lead to wins

Key Components:

Applications:

Note: Reinforcement Learning is advanced and typically covered after supervised/unsupervised learning.

Comparison Table

Type Data Goal Example
Supervised Labeled (X, y) Learn mapping X→y Predict house price
Unsupervised Unlabeled (X only) Find patterns Customer groups
Reinforcement Experience Maximize rewards Game playing

Training settings and learner families

These labels show up in courses and papers; they are orthogonal to supervised vs unsupervised (they describe how training runs and what is stored).

Batch vs online (incremental) learning

Instance-based vs model-based

You will meet both families again when you choose metrics, validation splits, and deployment constraints.


Machine Learning Workflow

Step-by-Step Process

1. Problem Definition

Questions to ask:

Example:

Problem: Predict customer churn
Goal: Identify customers likely to cancel subscription
Success: Reduce churn by 20%
Data needed: Customer behavior, demographics, usage patterns

2. Data Collection

Sources:

Considerations:

3. Data Preparation

Tasks:

Example:

import pandas as pd
from sklearn.preprocessing import StandardScaler, LabelEncoder

# Load data
df = pd.read_csv('data.csv')

# Handle missing values
df = df.fillna(df.mean())

# Encode categorical
le = LabelEncoder()
df['category'] = le.fit_transform(df['category'])

# Scale features
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)

4. Feature Engineering

Creating meaningful features:

Example:

# Create new features
df['total_spent'] = df['quantity'] * df['price']
df['age_group'] = pd.cut(df['age'], bins=[0, 25, 50, 75, 100])
df['days_since_signup'] = (today - df['signup_date']).days

5. Model Selection

Choose algorithm based on:

Example:

# For small dataset, need interpretability
from sklearn.tree import DecisionTreeClassifier
model = DecisionTreeClassifier(max_depth=5)

# For large dataset, need performance
from sklearn.ensemble import RandomForestClassifier
model = RandomForestClassifier(n_estimators=100)

6. Training

Process:

Example:

from sklearn.model_selection import train_test_split

# Split data
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=42
)

# Train model
model.fit(X_train, y_train)

# Validate
val_score = model.score(X_val, y_val)

7. Evaluation

Metrics:

Example:

from sklearn.metrics import accuracy_score, classification_report

# Predictions
y_pred = model.predict(X_test)

# Evaluate
accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy: {accuracy}")
print(classification_report(y_test, y_pred))

8. Deployment

Options:

Example:

# Simple API
from flask import Flask, request, jsonify

app = Flask(__name__)

@app.route('/predict', methods=['POST'])
def predict():
    data = request.json
    prediction = model.predict([data['features']])
    return jsonify({'prediction': prediction[0]})

9. Monitoring

Track:


Descriptive statistics and sampling foundations

These ideas show up in every ML project: you summarize data you have (a sample) and hope it reflects the population you care about.

Mean, median, mode, variance, and standard deviation

import numpy as np
import pandas as pd

scores = np.array([72, 85, 90, 68, 88, 91, 77, 84])

print("mean:", scores.mean())
print("median:", np.median(scores))
# mode for continuous data is less common; for discrete counts use pandas
s = pd.Series([2, 2, 3, 3, 3, 4])
print("mode:", s.mode().tolist())
print("variance (sample):", scores.var(ddof=1))   # unbiased sample variance
print("std (sample):", scores.std(ddof=1))

# Percentiles / quartiles (distribution shape)
print("25th, 50th, 75th:", np.percentile(scores, [25, 50, 75]))

Population vs sample (intuition + simulation)

rng = np.random.default_rng(42)
# True population: normal with mean 100, std 15 (toy example)
population = rng.normal(100, 15, size=100_000)

# One random sample of 200 people
sample = rng.choice(population, size=200, replace=False)
print("Population mean (known in simulation):", population.mean())
print("Sample mean:", sample.mean())
# Sample mean changes each draw; larger samples usually get closer to population mean.

Data types in ML (quick reference)

Kind Examples Typical use
Numeric continuous height, price regression targets, features
Numeric discrete count of clicks regression or classification features
Categorical nominal country code encoding then models
Categorical ordinal survey Likert scale ordered encoding
Text email body NLP pipelines
Time timestamps time-series features

Key Concepts

Training Data vs Testing Data

Training Data:

Testing Data:

from sklearn.model_selection import train_test_split

# Split: 80% train, 20% test
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=42
)

# Train on training data
model.fit(X_train, y_train)

# Evaluate on test data (unseen!)
score = model.score(X_test, y_test)

Features and Labels

Features (X):

Labels/Targets (y):

Example:

# Features (X): age, income, education
# Label (y): loan_approved (yes/no)

X = df[['age', 'income', 'education']]
y = df['loan_approved']

Overfitting vs Underfitting

Overfitting:

# Overfitting example
# Training accuracy: 99%
# Test accuracy: 60%
# Gap indicates overfitting!

Underfitting:

# Underfitting example
# Training accuracy: 55%
# Test accuracy: 53%
# Both low - model too simple!

Good Fit:

# Good fit example
# Training accuracy: 85%
# Test accuracy: 83%
# Close performance - good generalization!

Bias-Variance Tradeoff

Bias:

Variance:

Tradeoff:


Real-World Applications

Supervised Learning Applications

  1. Healthcare

    • Disease diagnosis
    • Drug discovery
    • Medical image analysis
  2. Finance

    • Fraud detection
    • Credit scoring
    • Algorithmic trading
  3. E-commerce

    • Product recommendations
    • Price optimization
    • Customer lifetime value
  4. Technology

    • Spam detection
    • Speech recognition
    • Machine translation

Unsupervised Learning Applications

  1. Marketing

    • Customer segmentation
    • Market basket analysis
  2. Anomaly Detection

    • Network security
    • Manufacturing defects
  3. Data Compression

    • Image compression
    • Feature reduction

Getting Started with Scikit-learn

Installation

pip install scikit-learn

Your First ML Model

from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score

# Load data
iris = load_iris()
X, y = iris.data, iris.target

# Split data
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=42
)

# Create and train model
model = RandomForestClassifier(n_estimators=100)
model.fit(X_train, y_train)

# Make predictions
y_pred = model.predict(X_test)

# Evaluate
accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy: {accuracy:.2f}")

Common Scikit-learn Workflow

# 1. Import
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score

# 2. Prepare data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)

# 3. Scale features (if needed)
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)

# 4. Create and train model
model = RandomForestClassifier()
model.fit(X_train_scaled, y_train)

# 5. Predict and evaluate
y_pred = model.predict(X_test_scaled)
accuracy = accuracy_score(y_test, y_pred)

Practice Exercises

Exercise 1: Identify ML Type

Task: For each scenario, identify if it's supervised (regression/classification) or unsupervised.

Scenarios:

  1. Predicting house prices
  2. Grouping customers by behavior
  3. Detecting spam emails
  4. Reducing 100 features to 2 for visualization
  5. Predicting if patient has disease

Solution:

  1. Supervised. Regression (continuous output: price)
  2. Unsupervised. Clustering (no labels, finding groups)
  3. Supervised - Classification (categories: spam/not spam)
  4. Unsupervised. Dimensionality Reduction (no labels)
  5. Supervised. Classification (categories: has disease/doesn't)

Exercise 2: Design ML Workflow

Task: Design an ML workflow for predicting customer churn.

Solution:

# 1. Problem Definition
# Goal: Predict which customers will cancel subscription
# Success metric: Identify 80% of churning customers

# 2. Data Collection
# - Customer demographics
# - Usage patterns
# - Payment history
# - Support tickets

# 3. Data Preparation
import pandas as pd
df = pd.read_csv('customer_data.csv')
df = df.dropna()
df = df.drop_duplicates()

# 4. Feature Engineering
df['days_since_signup'] = (today - df['signup_date']).days
df['avg_session_duration'] = df['total_time'] / df['sessions']
df['support_tickets_per_month'] = df['tickets'] / df['months_active']

# 5. Model Selection
from sklearn.ensemble import RandomForestClassifier
# Choose RandomForest for interpretability and performance

# 6. Training
from sklearn.model_selection import train_test_split
X = df[['age', 'usage', 'days_since_signup', ...]]
y = df['churned']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)

model = RandomForestClassifier()
model.fit(X_train, y_train)

# 7. Evaluation
from sklearn.metrics import classification_report
y_pred = model.predict(X_test)
print(classification_report(y_test, y_pred))

# 8. Deployment
# Create API endpoint for predictions

# 9. Monitoring
# Track prediction accuracy weekly

Exercise 3: First ML Model

Task: Create your first ML model using the Iris dataset.

Solution:

from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score, classification_report

# Load data
iris = load_iris()
X, y = iris.data, iris.target

print(f"Features: {iris.feature_names}")
print(f"Classes: {iris.target_names}")
print(f"Data shape: {X.shape}")

# Split data
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=42
)

# Create and train model
model = DecisionTreeClassifier(max_depth=3)
model.fit(X_train, y_train)

# Predictions
y_pred = model.predict(X_test)

# Evaluate
accuracy = accuracy_score(y_test, y_pred)
print(f"\nAccuracy: {accuracy:.2f}")
print("\nClassification Report:")
print(classification_report(y_test, y_pred, target_names=iris.target_names))

Key Takeaways

  1. ML learns from data - No explicit programming needed
  2. Three main types - Supervised, Unsupervised, Reinforcement
  3. Follow the workflow - Problem → Data → Model → Deploy
  4. Avoid overfitting - Balance model complexity
  5. Practice - Start with simple problems

Next Steps

Try next: State train vs test leakage in one sentence using your last project as the example.

Recall ::

Name the three main types of machine learning.

Supervised, unsupervised, and reinforcement learning.