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.
- Machine learning for beginners → What is ML?, Types of ML, Workflow
- Data types in ML (numeric, categorical, text, time; targets vs features) → Key concepts
- Population vs sample → Descriptive statistics and sampling foundations
- Batch vs online updates; instance-based vs model-based learners → Training settings and learner families
- Descriptive statistics (mean, median, mode, variance, standard deviation) → Descriptive statistics and sampling foundations
- Regression next (linear, metrics, distributions, saving models) → Regression guide
- Classification next (logistic, KNN, Naive Bayes, metrics) → Classification guide
- EDA and cleaning → EDA guide
- Feature engineering and ML-ready preprocessing → Feature engineering guide
- Projects (implement algorithms in context) → Beginner projects
Table of Contents
- ML for beginners curriculum map (this guide)
- Descriptive statistics and sampling foundations
- What is Machine Learning?
- Types of Machine Learning
- Training settings and learner families
- Machine Learning Workflow
- Key Concepts
- Real-World Applications
- Getting Started with Scikit-learn
- Practice Exercises
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?
- Complex Problems: Some problems are too complex for explicit rules
- Adaptability: Models can adapt to new patterns
- Scalability: Handle large amounts of data
- Pattern Recognition: Find patterns humans might miss
- Automation: Reduce manual work
Machine Learning Applications
Real-World Applications:
- Recommendation Systems: Netflix, Amazon product recommendations
- Fraud Detection: Credit card fraud, insurance fraud
- Spam Filter: Email spam detection (Gmail, Outlook)
- Self-driving Cars: Autonomous vehicle navigation
- Image Recognition: Face recognition, object detection
- Natural Language Processing: Chatbots, translation
- Medical Diagnosis: Disease detection from medical images
- Financial Trading: Stock price prediction, algorithmic trading
Machine Learning Workflow Summary
Standard ML Workflow:
Problem → Data → EDA → Features → Model → Evaluate → Improve
Detailed Steps:
- Problem: Define the problem and success criteria
- Data: Collect and prepare data
- EDA: Exploratory Data Analysis. Understand data patterns
- Features: Feature engineering and selection
- Model: Train machine learning model
- Evaluate: Assess model performance
- Improve: Iterate and optimize (hyperparameter tuning, feature engineering)
History and Evolution
- 1950s: First neural networks
- 1980s: Backpropagation algorithm
- 1990s: Support Vector Machines
- 2000s: Random Forests, Boosting
- 2010s: Deep Learning revolution
- 2020s: Large Language Models, Transformers
Types of Machine Learning
1. Supervised Learning
Definition: Learning from labeled data (input-output pairs).
How it works:
- Training data has both features (X) and labels (y)
- Model learns the mapping: X → y
- Makes predictions on new, unseen data
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
- Goal: Predict continuous values
- Examples:
- House price prediction
- Temperature forecasting
- Stock price prediction
- Output: Real numbers
# 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
- Goal: Predict categories/classes
- Examples:
- Spam email detection (spam/not spam)
- Image classification (cat/dog)
- Disease diagnosis (healthy/sick)
- Output: Categories
# 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:
- Linear Regression, Logistic Regression
- Decision Trees, Random Forest
- Support Vector Machines (SVM)
- K-Nearest Neighbors (KNN)
- Neural Networks
2. Unsupervised Learning
Definition: Learning from unlabeled data (only features, no labels).
How it works:
- Training data has only features (X), no labels
- Model finds patterns, structures, or relationships
- Discovers hidden patterns in data
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
- Goal: Group similar data points
- Examples:
- Customer segmentation
- Image compression
- Anomaly detection
- Output: Groups/clusters
# 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
- Goal: Reduce number of features while keeping important information
- Examples:
- Data visualization (reduce to 2D/3D)
- Feature selection
- Noise reduction
- Output: Reduced feature space
# 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:
- K-Means, Hierarchical Clustering
- DBSCAN
- Principal Component Analysis (PCA)
- t-SNE
3. Reinforcement Learning
Definition: Learning through interaction with an environment, receiving rewards or penalties.
How it works:
- Agent takes actions in an environment
- Receives rewards or penalties
- Learns optimal strategy to maximize rewards
- No labeled data. Learns from experience
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:
- Agent: The learner/decision maker
- Environment: The world the agent interacts with
- Actions: What the agent can do
- Rewards: Feedback from environment
- Policy: Strategy for choosing actions
Applications:
- Game playing (Chess, Go, video games)
- Robotics
- Autonomous vehicles
- Recommendation systems
- Trading algorithms
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
- Batch / offline: The model sees a large (or full) dataset each update, or you train on fixed snapshots. Most sklearn examples are batch:
fit(X, y)on a matrix you already hold. - Online / streaming / incremental: New labeled rows arrive over time; you update the model in small steps without reloading everything. Useful for feeds, sensors, and fraud: often paired with careful drift monitoring (later: MLOps modules).
Instance-based vs model-based
- Instance-based learners memorize training examples (or a subset) and decide by similarity at prediction time (classic: k-NN). More data at serving time can mean more storage and slower queries.
- Model-based learners compress the data into parameters (weights, tree rules, etc.): linear models, forests, neural nets. Prediction is usually fast once parameters are fixed.
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:
- What problem are we solving?
- What is the business goal?
- What would success look like?
- What data do we need?
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:
- Databases
- APIs
- Web scraping
- Surveys
- Public datasets (Kaggle, UCI)
Considerations:
- Is data available?
- Is data sufficient?
- Is data representative?
- Privacy and ethics
3. Data Preparation
Tasks:
- Load data
- Explore data (EDA)
- Handle missing values
- Handle outliers
- Encode categorical variables
- Scale/normalize features
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:
- Combine features
- Create polynomial features
- Extract date features
- Domain-specific 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:
- Problem type (regression/classification)
- Data size
- Interpretability needs
- Performance requirements
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:
- Split data (train/validation/test)
- Train model on training data
- Validate on validation set
- Tune hyperparameters
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:
- Regression: MSE, RMSE, MAE, R²
- Classification: Accuracy, Precision, Recall, F1-score
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:
- REST API (Flask/FastAPI)
- Web application
- Mobile app
- Batch processing
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:
- Model performance over time
- Data drift
- Prediction quality
- System performance
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)
- Population: all units you could measure (often unknown or infinite).
- Sample: the subset you actually collected and train on.
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:
- Used to teach the model
- Model learns patterns from this
- Typically 70-80% of data
Testing Data:
- Used to evaluate model
- Model has never seen this
- Typically 20-30% of data
- Simulates real-world performance
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):
- Input variables
- What the model uses to make predictions
- Also called: predictors, independent variables
Labels/Targets (y):
- Output variable
- What we want to predict
- Also called: target, dependent variable
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:
- Model memorizes training data
- Performs well on training, poorly on test
- Too complex model
- Solution: Simplify model, more data, regularization
# Overfitting example
# Training accuracy: 99%
# Test accuracy: 60%
# Gap indicates overfitting!
Underfitting:
- Model too simple
- Can't capture patterns
- Poor performance on both train and test
- Solution: More complex model, better features
# 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:
- Error from overly simplistic assumptions
- High bias = underfitting
Variance:
- Error from sensitivity to small fluctuations
- High variance = overfitting
Tradeoff:
- Can't minimize both simultaneously
- Need to find balance
Real-World Applications
Supervised Learning Applications
Healthcare
- Disease diagnosis
- Drug discovery
- Medical image analysis
Finance
- Fraud detection
- Credit scoring
- Algorithmic trading
E-commerce
- Product recommendations
- Price optimization
- Customer lifetime value
Technology
- Spam detection
- Speech recognition
- Machine translation
Unsupervised Learning Applications
Marketing
- Customer segmentation
- Market basket analysis
Anomaly Detection
- Network security
- Manufacturing defects
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:
- Predicting house prices
- Grouping customers by behavior
- Detecting spam emails
- Reducing 100 features to 2 for visualization
- Predicting if patient has disease
Solution:
- Supervised. Regression (continuous output: price)
- Unsupervised. Clustering (no labels, finding groups)
- Supervised - Classification (categories: spam/not spam)
- Unsupervised. Dimensionality Reduction (no labels)
- 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
- ML learns from data - No explicit programming needed
- Three main types - Supervised, Unsupervised, Reinforcement
- Follow the workflow - Problem → Data → Model → Deploy
- Avoid overfitting - Balance model complexity
- Practice - Start with simple problems
Next Steps
- Practice with the exercises
- Try the Iris classification example
- Move to 03-supervised-learning-regression for regression
- Or 04-supervised-learning-classification for classification
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.