0% found this document useful (0 votes)
81 views86 pages

Predictive Analytics Study Notes

The document provides comprehensive study notes on predictive analytics, covering its definition, types, project lifecycle, and various modeling techniques. It emphasizes the importance of understanding data types, problem setup, and success metrics in predictive analytics projects. Additionally, it outlines the CRISP-DM framework as a structured approach to data mining and predictive modeling.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
81 views86 pages

Predictive Analytics Study Notes

The document provides comprehensive study notes on predictive analytics, covering its definition, types, project lifecycle, and various modeling techniques. It emphasizes the importance of understanding data types, problem setup, and success metrics in predictive analytics projects. Additionally, it outlines the CRISP-DM framework as a structured approach to data mining and predictive modeling.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PREDICTIVE ANALYTICS: COMPLETE STUDY NOTES

DS701PC: PREDICTIVE ANALYTICS - [Link] IV Year I Semester | JNTU Hyderabad

TABLE OF CONTENTS

1. Introduction to Predictive Analytics

2. Problem Setup and Variables

3. Data Types and Measures

4. Predictive Analytics Project Lifecycle & Data Effect

5. Exploratory Data Analysis (EDA)

6. Regression Models Introduction

7. Logistic Regression Concept

8. Artificial Neural Networks Basics

9. K-Nearest Neighbors and Bayesian Models

10. Decision Trees and Model Assessment

11. Batch Approach & Model Validation

12. Advanced Regression Techniques

13. Ensemble Methods Introduction

14. Random Forests and Advanced Ensemble Concepts

15. Model Validation and Tuning

16. Deployment of Predictive Models

17. Industry Use Cases

18. Challenges in Predictive Analytics

19. Answering Questions, Persuasion Techniques

20. Review and Assessment Preparation


SESSION 1: INTRODUCTION TO PREDICTIVE ANALYTICS

What is Predictive Analytics?

Predictive analytics is all about using historical data to make educated guesses about
the future. Think of it this way: if you've seen 100 customers behave in a certain way
before they left your company, you can use those patterns to identify similar customers
right now who might leave soon. That's predictive analytics in action.

The term itself combines two ideas:

Predictive - Making forecasts about what will happen

Analytics - Analyzing data to find meaningful patterns

Three Main Types of Analytics

You'll see these three terms a lot, so let's be clear:

1. Descriptive Analytics - "What happened?"

- Example: Your sales were ₹50 lakhs last month

- Uses: Reports, dashboards, summaries

- This is what most businesses do today

2. Diagnostic Analytics - "Why did it happen?"

- Example: Sales dropped because competitor launched a sale

- Uses: Root cause analysis, investigations

- Requires deeper diving into data

3. Predictive Analytics - "What will happen?"

- Example: Sales will drop 20% next month if we don't change strategy

- Uses: Forecasting, risk assessment, planning

- This is what we're learning

4. Prescriptive Analytics - "What should we do about it?"

- Example: To prevent sales drop, we should launch loyalty program


- Uses: Recommendations, optimization

- Combines prediction with business logic

Banking & Finance:

- Credit scoring: Decide if someone qualifies for a loan

- Fraud detection: Identify suspicious transactions before they happen

- Stock market prediction: Forecast price movements

Retail:

- Customer churn prediction: Who will stop shopping with us?

- Demand forecasting: How much inventory do we need?

- Recommendation systems: What products should we suggest?

Healthcare:

- Patient readmission risk: Who might come back to hospital?

- Disease diagnosis: Does this patient have condition X?

- Treatment success prediction: Will this medicine work?

Telecom:

- Network failure prediction: Where will problems occur?

- Customer lifetime value: Which customers are most valuable?

- Churn prediction: Who might switch providers?

The Data Science Process (Important Framework!)

Every predictive analytics project follows this flow:

1. Problem Understanding - What are we trying to predict?

2. Data Collection - Where do we get the data?

3. Exploratory Analysis - What patterns exist?


4. Data Preparation - Clean and format the data

5. Model Building - Create the prediction algorithm

6. Model Evaluation - How accurate is it?

7. Deployment - Put it into production

8. Monitoring - Does it still work over time?

Why is Predictive Analytics Important Now?

- Business Pressure : Companies need competitive advantage

- Data Explosion : We have more data than ever before

- Computing Power : Machines can process millions of records instantly

- ROI Focus : Businesses want measurable returns on investment

Remember: Predictive analytics is not guessing. It's using math and statistics on
historical data to make calculated predictions.

SESSION 2: PROBLEM SETUP AND VARIABLES

The Foundation: Asking the Right Question

Before you write a single line of code, you need to be crystal clear about what you're
trying to predict. This is arguably the most important session because everything else
depends on getting this right.

SMART Problem Definition Use the SMART framework (you should memorize this):

- Specific : Define exactly what you want to predict

- Bad: "Improve customer satisfaction"


- Good: "Predict which customers will rate us below 3 stars in the next 30 days"

- Measurable : How will you know if you succeeded?

- Bad: "Better predictions"

- Good: "Achieve 85% accuracy in identifying at-risk customers"

- Achievable : Is it realistic with available data and resources?

- Bad: "Predict earthquake locations"

- Good: "Predict which stores will exceed sales targets"

- Relevant : Does it matter to the business?

- Bad: "Predict number of vowels in customer names"

- Good: "Predict product defect rates to reduce returns"

- Time-bound : What's the prediction timeframe?

- Bad: "Predict if customer will churn"

- Good: "Predict if customer will churn in next 60 days"

Understanding Variables

Every dataset has two types of variables:

1. Target Variable (What We Predict)

This is what we're trying to forecast. It's also called:

- Dependent variable

- Output

- Response variable

- y or Y

Examples:

- Will customer churn? (Yes/No)

- What will house price be? (₹25 lakhs, ₹50 lakhs, etc.)
- Will patient survive surgery? (Yes/No)

2. Predictor Variables (What We Use)

These are the features we use to make the prediction. Also called:

- Independent variables

- Features

- Input variables

- X or X1, X2, X3...

Examples:

- Customer age, tenure, monthly charges (for churn prediction)

- House size, location, age (for price prediction)

- Patient age, health score, comorbidities (for surgery survival)

Key Concept: Relationship Between Variables

The whole game is about finding which predictor variables have strong relationships
with the target variable.

Strong Relationship Example:

If we're predicting loan default:

- Credit score = strong predictor (people with low credit scores default more)

- Income level = strong predictor (people with high income default less)

Weak Relationship Example:

- Customer's favorite color = weak predictor (unlikely to affect loan default)

- Name length = weak predictor (doesn't tell us about financial reliability)

Success Metrics: How Do We Know If We Win?

Before building any model, define how you'll measure success:

For Churn Prediction:

- Identify 80% of customers who will actually churn


- Keep false alarms below 20% (don't bother customers who won't actually leave)

For Fraud Detection:

- Catch 95% of fraudulent transactions

- Accept that 0.1% of legitimate transactions might be blocked

For Sales Forecasting:

- Predictions should be within 10% of actual sales

- Better to overestimate than underestimate (avoid stockouts)

Common Mistakes in Problem Setup

Mistake 1: Predicting Too Far Into Future

- Trying to predict customer churn 5 years out (data patterns change)

- Better: Predict 30-60 days out

Mistake 2: Vague Target Definition

- "Predict good customers" (what's "good"?)

- Better: "Predict customers who will spend >₹50,000 this year"

Mistake 3: Not Having Enough Data

- Trying to predict rare events with limited examples

- Better: Choose target events that occur 5-50% of the time

Mistake 4: Ignoring Business Constraints

- Predicting something we can't actually act on

- Better: Predict things that business can intervene on


---

SESSION 3: DATA TYPES AND MEASURES

Understanding Data: The Foundation - Before you can build predictions, you need to
understand what type of data you're working with. Different data types require different
treatments.

Data Classification: The Four Levels

Think of data like a hierarchy (Measurement Scales):

1. Nominal Data (Categories with no order)

These are just labels or categories. No ranking exists.

Examples:

- Color: Red, Blue, Green (red isn't "better" than blue)

- Gender: Male, Female, Other

- Product Category: Electronics, Clothing, Books

- Customer Segment: Premium, Standard, Basic

How to treat it:

- Can't do math on it (can't add male + female)

- Can use frequency analysis (30% are male, 70% are female)

- Predictive models need conversion (we'll learn one-hot encoding later)

2. Ordinal Data (Categories with order)

These have a meaningful order, but gaps between them aren't necessarily equal.
Examples:

- Education: High School < Bachelor < Master < PhD

- Customer Satisfaction: Poor < Average < Good < Excellent

- Hotel Rating: 1 star < 2 stars < 3 stars < 5 stars

Important difference from nominal: There IS a meaningful order

- PhD > Master (someone with PhD has more education)

- Excellent > Good (better satisfaction)

How to treat it:

- Can use median (middle value)

- Can rank things

- Still can't assume the gap between 3 and 4 stars equals gap between 1 and 2 stars

3. Interval Data (Equal intervals, no true zero)

Numbers where the intervals between values are equal, but zero doesn't mean "nothing."

Examples:

- Temperature in Celsius: 20°C is not twice as hot as 10°C

- Year: 2020 vs 2010 (can measure gap, but year "0" isn't meaningful)

- IQ Score: 100 is not twice as intelligent as 50

Key issue: No true zero point. You can't say "this is twice that."

How to treat it:

- Can use mean and standard deviation

- Can do most math operations

- Just remember ratios aren't meaningful

4. Ratio Data (Equal intervals, true zero exists)

Perfect data! All math operations make sense.


Examples:

- Height: 180 cm is exactly twice as tall as 90 cm

- Weight: 80 kg is twice 40 kg

- Sales Revenue: ₹100 is twice ₹50

- Age: 40 years is twice 20 years

- Number of Customers: 1000 customers is twice 500

How to treat it:

- Can do all math operations

- Can use all statistical techniques

- Ratios are meaningful

Encoding Categorical Data: The Real Challenge

Here's the practical problem: Machine learning algorithms (mostly) work with numbers,
not text. If you have "Electronics," "Clothing," "Books," you need to convert these to
numbers.

Method 1: One-Hot Encoding (Most Common)

Convert each category into binary columns:

Original:

Becomes:
When to use: When you have nominal data (no order)

Advantage: Each category is treated equally; no artificial ordering

Disadvantage: Creates many columns if you have lots of categories

Method 2: Label Encoding

Convert categories to numbers directly:

Original:
Becomes:

When to use: When you have ordinal data (has order)

Advantage: Keeps data compact

Disadvantage: Creates artificial relationships (2 is "between" 1 and 3)

Important Note: Don't use label encoding for nominal data! It will make the algorithm
think "Books" is between "Electronics" and "Clothing."

Method 3: Target Encoding

For each category, replace with the average target value:

Churn prediction example:

Replace each customer's type with their churn rate.

When to use: When you have many categories and want to reduce columns

Advantage: Compresses information meaningfully


Disadvantage: Can overfit if not careful

Continuous Data Handling

Continuous data is already numeric, so less transformation needed. But you should still
think about:

Scaling/Normalization

Different features might have different ranges:

- Age: 18 to 80

- Income: ₹200,000 to ₹5,000,000

- Support Calls: 0 to 15

Some algorithms treat larger-scale features as more important. We fix this by scaling.

Min-Max Scaling:

Converts all values to 0-1 range

New Value = (Value - Min) / (Max - Min)

Standardization:

Converts to mean 0, standard deviation 1

New Value = (Value - Mean) / Standard Deviation

Missing Data: The Real-World Problem

In textbooks, datasets are clean. In reality, data is messy. Some values are missing.

Why Data Goes Missing

1. Data Entry Errors: Someone forgot to fill in a field

2. Equipment Failure: Sensor stopped recording

3. Intentional Non-Response: Customer didn't want to answer

4. Merging Issues: Data from different sources doesn't line up


Strategies for Handling Missing Data

Strategy 1: Delete Rows with Missing Values

Dataset: 1000 rows

5% rows have missing values

After deletion: 950 rows

Pros: Simple, clean

Cons: Lose data; bias if missingness isn't random

Strategy 2: Fill with Mean/Median

Age column: 20, 25, ?, 30, 35

Average: (20+25+30+35)/4 = 27.5

Fill missing with 27.5

Pros: Keep all data

Cons: Reduces variability; doesn't use other info

Strategy 3: Predict Missing Values

Use other features to predict missing value

Example: If customer age is missing, use income, tenure,

purchase history to predict age

Pros: Uses all available information


Cons: More complex; can introduce bias

Strategy 4: Create "Missing" Category

For categorical data:

Original: Electronics, Clothing, Books, [Missing]

Becomes: Electronics, Clothing, Books, Unknown

Pros: Might reveal pattern (why is data missing?)

Cons: Adds complexity

Data Quality: A Real-World Perspective

In practice, you'll spend 50-60% of your time on data preparation, not modeling. Here are
key quality checks:

Check 1: Duplicates

- Does the dataset have identical rows?

- Remove or flag them

Check 2: Outliers

- Are there extreme values that don't make sense?

- Example: Customer age = 999 years (clearly wrong)

- Example: House price = ₹-50 lakhs (negative price?)

Check 3: Typos

- Different spellings of same thing

- "Electronics," "electronic," "ELECTRONICS" (treat as same)


Check 4: Format Issues

- Dates in different formats: 01/02/2024 vs 2024-02-01

- Phone numbers with different formats

- Currency with/without symbols

SESSION 4: PREDICTIVE ANALYTICS PROJECT LIFECYCLE & DATA EFFECT

The Big Picture: CRISP-DM Framework

In industry, companies follow structured processes. The most common is CRISP-DM


(Cross-Industry Standard Process for Data Mining). Learn this—it's in every textbook and
interview question.

The Six Phases:

Phase 1: BUSINESS UNDERSTANDING (20% of effort)

What you do:

- Talk to business stakeholders

- Understand what problem we're solving

- Define success metrics

- Identify data sources

- Estimate resources needed

Key Questions:

- Why do we need this prediction?

- How will it be used?

- What's the business impact?

- What's our deadline?

- What's our budget?


Real Example:

A retail company says: "We want to predict which customers will churn next 60 days. If
we can identify 80% of them, we save ₹50 lakh in customer acquisition costs."

Phase 2: DATA UNDERSTANDING (15% of effort)

What you do:

- Collect initial data

- Explore the data

- Identify data quality issues

- Get preliminary insights

Key Activities:

- Look at first few rows

- Check data types

- Calculate basic statistics (mean, median, range)

- Visualize distributions

- Check for missing values

Real Example:

"We have 50,000 customer records. 23% have churned. Income field has 5% missing
values. Age ranges from 18 to 95. We see customers with >100 support calls."

Phase 3: DATA PREPARATION (25% of effort, often the longest!)

What you do:

- Clean the data

- Handle missing values

- Remove outliers or fix them

- Create new features

- Encode categorical variables


- Scale numerical variables

Real Example:

- Replace missing ages with median

- Convert "Male"/"Female" to 1/0

- Combine first/last purchase dates to create "tenure" feature

- Remove customers with negative purchase amounts

Phase 4: MODELING (20% of effort)

What you do:

- Select appropriate algorithms

- Train models on historical data

- Tune model parameters

- Compare different approaches

Real Example:

"We'll try logistic regression, decision trees, and random forest. We'll use 70% of data for
training, 30% for testing."

Phase 5: EVALUATION (10% of effort)

What you do:

- Test model on unseen data

- Calculate performance metrics

- Compare against baseline

- Get business validation

Real Example:

"Our model is 85% accurate. It catches 80% of actual churners (we wanted 80%). False
alarm rate is 15% (we wanted <20%). The business accepts this."
Phase 6: DEPLOYMENT (10% of effort)

What you do:

- Move model to production

- Monitor performance

- Get user feedback

- Plan for retraining

Real Example:

"Every Sunday, we score all customers. Those with >60% churn probability get targeted
retention offers. We track how many actually churn vs predicted."

Important: The Iterative Nature

CRISP-DM isn't strictly linear. You often cycle back:

- Modeling reveals data quality issues → Go back to Phase 3

- Evaluation shows poor performance → Try different models in Phase 4

- Deployment reveals concept drift → Retrain in Phase 4

The Data Effect: Why Data Quality Matters Most

Here's a crucial concept for exams: The quality of your prediction is limited by the
quality of your data.

The Data Hierarchy:

Perfect Predictions


| Depends on →

Great Algorithms

| Depends on →

Excellent Features

| Depends on →

Clean, Complete Data

Examples of Data Effect:

Example 1: Missing Feature

If you're predicting student performance but don't have "study hours" data, your model
will perform poorly. No algorithm can compensate for missing relevant information.

Example 2: Data Quality

If 50% of customer income values are wrong (data entry errors), your model's income-
based predictions will be wrong.

Example 3: Biased Data

If your training data only has customers from Bangalore, your model won't predict well
for customers from Mumbai (different behavior patterns).

The 80/20 Rule in Data Science

- 80% of time: Data collection, cleaning, exploration

- 20% of time: Modeling and algorithms

Most beginners do it opposite and struggle. Experienced data scientists spend serious
time on data.

Project Timeline EstimationReal-world project (medium size):


Stakeholder Management

Different people care about different things:

Executive (wants to know):

- Business impact: How much money will we save?

- Timeline: When will it be ready?

- Risk: What can go wrong?

Technical Team (wants to know):

- Data requirements: What data do we need?

- Infrastructure: What systems?

- Maintenance: How will we monitor?

Business Users (wants to know):

- What actions should we take?

- How confident are predictions?

- How do we explain to customers?

Good project managers align everyone's expectations early.


---

SESSION 5: EXPLORATORY DATA ANALYSIS (EDA)

What is EDA? The Detective Work

Before you build any fancy model, you need to become a detective. You need to explore
the data, understand patterns, spot anomalies, and generate hypotheses. This is EDA.

EDA is about asking questions and letting the data answer:

- What does the distribution look like?

- Are there outliers?

- What relationships exist between variables?

- Are there patterns in time?

Single Variable Analysis: Understanding One Thing at a Time

Numerical Variables

When exploring a number like age, income, or days tenure, start with these statistics:

Location Measures:
- Mean: Average value

Example: Average customer age is 45 years

Interpretation: Central tendency

- Median: Middle value (50% above, 50% below)

Example: Median salary is ₹40 lakh

Interpretation: When mean distorted by extremes, use median

- Mode: Most frequent value

Example: Most customers buy 1 product

Spread Measures:

- Range: Difference between max and min

Example: Ages range from 18 to 80 (range = 62)

- Standard Deviation: How spread out values are

Example: If mean income is ₹50 lakh with std dev ₹10 lakh

Interpretation: Most people earn between ₹40-60 lakh

- Quartiles: Divide data into 4 equal parts

Example:

- 25th percentile: 30 lakh (25% earn less)

- 50th percentile (median): 40 lakh

- 75th percentile: 60 lakh (75% earn less)

What to look for:

- Is distribution skewed? (mean ≠ median suggests skew)

- Are there outliers? (values far from the rest)

- Is spread reasonable? (huge standard deviation is suspicious)

Categorical Variables
For categories like gender, product type, region:

Frequency Analysis:

- Count how many in each category

- Calculate percentages

Example - Product Categories:

Electronics: 450 (45%)

Clothing: 300 (30%)

Books: 150 (15%)

Sports: 100 (10%)

Total: 1000 (100%)

What to look for:

- Is one category dominating? (450 vs 100 is imbalanced)

- Are all categories well-represented? (good for modeling)

- Any unexpected distributions?

Two-Variable Analysis: Finding Relationships

The real magic of EDA is finding relationships between variables. Remember:

For two numerical variables:

Use correlation (ranges from -1 to +1)

- Correlation = +1: Perfect positive relationship (both increase together)

Example: House size and house price (bigger → more expensive)

- Correlation = -1: Perfect negative relationship (one increases, other decreases)

Example: Discount percentage and profit (more discount → less profit)

- Correlation = 0: No relationship
Example: Shoe size and intelligence (unrelated)

Interpretation Guide:

- |Correlation| > 0.7: Strong relationship

- |Correlation| 0.4-0.7: Moderate relationship

- |Correlation| < 0.4: Weak relationship

For numerical vs categorical:

Compare distributions across categories

Example - Income by Education Level:

High School: Average ₹25 lakh

Bachelor: Average ₹45 lakh

Master: Average ₹70 lakh

PhD: Average ₹90 lakh

Clear relationship: More education → Higher income

For two categorical variables:

Use cross-tabulation (count combinations)

Example - Gender vs Churn:

Churned Stayed Total

Male: 45 355 400

Female: 30 570 600

Total: 75 925 1000


Churn rate - Male: 45/400 = 11.25%

Churn rate - Female: 30/600 = 5%

Insight: Males have higher churn rate

Visualizations: A Picture is Worth 1000 Words

Good visualizations reveal patterns that numbers hide.

Common Visualizations:

1. Histogram: Shows distribution of one numerical variable

[Visual would show bars showing age distribution]

Use: See shape of distribution, find outliers

2. Box Plot: Shows quartiles and outliers

[Visual would show box with whiskers]

Use: Quickly spot outliers and spread

3. Scatter Plot: Show relationship between two numerical variables

[Visual would show dots representing data points]

Use: See if there's relationship between variables

4. Bar Chart: Compare numerical values across categories

[Visual would show bars for each category]

Use: Compare magnitudes across groups

5. Line Graph: Show trends over time

[Visual would show line going up/down]

Use: See how values change through time

Outlier Detection: Spot the Unusual

Outliers are data points that don't fit the pattern. They're either:

1. Measurement errors (data entry wrong)


2. Genuine rare events

3. Invalid data

Common Methods:

Method 1: Interquartile Range (IQR)

- Calculate Q1 (25th percentile) and Q3 (75th percentile)

- IQR = Q3 - Q1

- Outliers are values > Q3 + 1.5×IQR or < Q1 - 1.5×IQR

Example:

Q1 = 30, Q3 = 60, IQR = 30

Upper limit = 60 + 1.5×30 = 105

Lower limit = 30 - 1.5×30 = -15

Any value > 105 or < -15 is outlier

Method 2: Standard Deviation

- Values beyond 3 standard deviations are rare outliers

Decision: Keep or Remove?

Remove if:

- Clearly data entry error (age = 999)

- Not representative of your target (testing ATM machine, but it's in different country)

- Will distort model (one person with ₹500 crore income in dataset of normal salaries)

Keep if:

- Genuinely possible (1 in 1000 customers spends ₹50 lakh)

- Part of the real world you're modeling

- Captures important information


Missing Data Patterns

MCAR (Missing Completely At Random)

- Missingness is random

- Example: Survey crash lost random responses

- Impact: Minimal if not too much missing

MAR (Missing At Random)

- Missingness depends on observed variables

- Example: High earners less likely to report income

- Impact: More concerning; needs special handling

MNAR (Missing Not At Random)

- Missingness depends on the missing value itself

- Example: People with low credit scores don't fill out credit form

- Impact: Most problematic; introduces bias

Time Series Patterns

If your data has time component, look for:

Trend: Long-term direction (up or down)

Sales trend: Increasing 5% per year

Seasonality: Regular patterns repeating

Sales higher in December (holiday shopping)

Cyclicity: Longer-term patterns

Economic cycles (boom, bust, boom)

Key EDA Findings to Report

When you finish EDA, you should be able to say:

1. "Data has X rows and Y columns"


2. "Missing values are in columns: ___, ___ (total X%)"

3. "Distribution is skewed for ___"

4. "Outliers exist in ___ (e.g., income >₹500 crore)"

5. "Strong correlations are between ___ and ___"

6. "Relationship between ___ and target suggests ___"

7. "Data quality issues: ___"

---

SESSION 6: REGRESSION MODELS INTRODUCTION

What is Regression? The Basics

Regression means predicting a continuous number (not just yes/no). Examples:

- Predict house price (₹25 lakhs, ₹50 lakhs, ₹72.5 lakhs)

- Predict next month's sales (₹500 crore, ₹525 crore)


- Predict patient recovery time (10 days, 15 days, 7.5 days)

The fundamental idea is simple: Find the best-fitting line through your data.

Simple Linear Regression: The Easiest Case

Imagine you're a real estate agent and want to predict house prices based on size.

The Equation:

Price = b0 + b1 × Size

Where:

- b0 = intercept (price when size = 0, though unrealistic)

- b1 = slope (how much price changes per unit size)

- Size = house size (independent variable)

- Price = predicted price (dependent variable)

Concrete Example: Suppose we have data:

Size (sqft) → Price (₹ Lakhs)

1000 50

1200 60

1400 70

1600 75

After analysis, we find:

Price = 10 + 0.04 × Size

Interpretation:

- b0 = 10: Base price is ₹10 lakh (even 0-size house)

- b1 = 0.04: Each additional sq ft adds ₹0.04 lakh = ₹4000

Using the model to predict:

For 1500 sqft house:


Price = 10 + 0.04 × 1500 = 10 + 60 = ₹70 lakhs

Multiple Regression: When You Have Many Features

In reality, house price depends on more than just size:

Price = b0 + b1×Size + b2×Age + b3×Bedrooms + b4×Location

Example with actual coefficients:

Price = 5 + 0.035×Size - 1×Age + 15×Bedrooms + 20×Location_Score

Interpretation:

- Base price: ₹5 lakh

- Size: Each sqft adds ₹0.035 lakh = ₹3500

- Age: Each year old reduces price by ₹1 lakh

- Bedrooms: Each bedroom adds ₹15 lakh

- Location Score: High score adds ₹20 lakh per point

Prediction example:

- Size: 1500 sqft → 0.035 × 1500 = 52.5

- Age: 10 years → -1 × 10 = -10

- Bedrooms: 3 → 15 × 3 = 45

- Location Score: 8 → 20 × 8 = 160

- Total Price = 5 + 52.5 - 10 + 45 + 160 = ₹252.5 lakhs

How Does Regression Find the Best Line?

The model tries to minimize prediction errors. Here's the concept:

Error = Actual Value - Predicted Value

For each data point, we calculate error. Then we find coefficients (b0, b1, etc.) that
minimize the sum of squared errors.

This is called Least Squares Method or OLS (Ordinary Least Squares) .

Why square the errors?


- Large errors (positive or negative) get punished heavily

- Small errors matter less

- Makes math convenient

Regression Assumptions: Important for Exams!

For regression to work well, certain conditions should be met:

1. Linearity

There's linear relationship between features and target.

Good Example: Age vs. Salary shows clear upward trend

Bad Example: Income vs. Happiness is not linear (after ₹1 crore, extra money
doesn't help much)

2. Independence

Data points are independent (one customer's behavior doesn't affect another).

Good Example: Random customers' purchase data

Bad Example: Same customer's purchases on consecutive days (dependent)

3. Homoscedasticity

Error spread is consistent across all predicted values.

Good Example: Prediction errors are small and similar everywhere

Bad Example: Errors are huge for some predictions, small for others

4. Normality

Errors are normally distributed (bell curve shape).

This helps with statistical testing and confidence intervals.

5. No Multicollinearity

Features aren't highly correlated with each other.

Problem: If feature A and B are almost identical, which one is actually predictive?

Solution: Remove one of them


Performance Metrics: How Good is Your Model?

R-squared (R²): The Key Metric

Ranges from 0 to 1. Higher is better.

Interpretation:

- R² = 0.95 means model explains 95% of variance (excellent)

- R² = 0.70 means model explains 70% of variance (good)

- R² = 0.40 means model explains 40% of variance (weak)

- R² = 0.20 means model explains 20% of variance (very weak)

RMSE (Root Mean Squared Error)

Measures average prediction error in original units.

Example:

- RMSE = ₹5 lakhs means on average predictions are off by ₹5 lakhs

- Whether this is good depends on context (for ₹100 crore property, it's excellent; for ₹10
lakh property, it's poor)

MAE (Mean Absolute Error)

Average of absolute errors (another way to measure accuracy).

Typical Challenges in Regression

Challenge 1: Overfitting

Model memorizes training data, poor on new data.

Solution: Simpler model, more data, regularization

Challenge 2: Underfitting

Model too simple to capture relationships.

Solution: More complex model, add features


Challenge 3: Outliers

Few extreme values distort the line.

Solution: Investigate outliers, possibly remove

Challenge 4: Multicollinearity

Features are too similar/correlated.

Solution: Remove one of correlated features

SESSION 7: LOGISTIC REGRESSION CONCEPT

The Problem: Why Not Linear Regression?

You might think: "I learned regression last session, can't I just use that for everything?"

No, and here's why:

For binary classification (yes/no, churn/not churn), linear regression has problems:

Problem 1: Invalid Predictions


Linear regression can predict values outside [0,1].

Example:

Predicting: Will student pass? (Yes=1, No=0)

Linear regression predicts: 1.5 (what does 1.5 mean? 150% probability?)

Or predicts: -0.3 (negative probability doesn't exist!)

Problem 2: Doesn't Match Reality

Real relationships in classification aren't linear.

Example: Predicting loan default based on credit score

- Credit score 300-400: Very high default rate (80%+)

- Credit score 400-500: High default rate (50%)

- Credit score 500-600: Moderate default rate (20%)

- Credit score 600-700: Low default rate (5%)

- Credit score 700-800: Very low default rate (<1%)

This doesn't look like a straight line. It looks like an S-curve (sigmoid).

The Solution: Logistic Regression

Logistic regression takes the straight line from linear regression and wraps it in a
sigmoid function. This ensures:

- Predictions always between 0 and 1 (valid probabilities)

- S-shaped curve matches real-world relationships

The Sigmoid Function

The magical formula:

P(Y=1) = 1 / (1 + e^(-z))
Where z = b0 + b1×X1 + b2×X2 + ...

What does this do?

Input any number, get out a probability (0 to 1).

Examples:

If z = -5: P = 1/(1+e^5) ≈ 0.007 (almost 0%)

If z = 0: P = 1/(1+e^0) = 0.5 (exactly 50%)

If z = +5: P = 1/(1+e^(-5)) ≈ 0.993 (almost 100%)

This matches intuition:

- Very negative z → very low probability

- z around 0 → moderate probability

- Very positive z → very high probability

Interpreting Logistic Regression

Odds and Log-Odds

Odds = probability of success / probability of failure

Example:

If P(churn) = 0.8, then P(stay) = 0.2

Odds = 0.8/0.2 = 4

Meaning: 4 to 1 chance of churning

Log-Odds = natural logarithm of odds

The key insight: Linear regression becomes linear in log-odds!

log(odds) = b0 + b1×X1 + b2×X2 + ...


This is easier to interpret:

- b1 = 0.5 means: For every 1 unit increase in X1, log-odds increase by 0.5

- This means odds multiply by e^0.5 ≈ 1.65 (65% increase in odds)

Practical Example: Predicting Student Pass/Fail

Data:

Study Hours → Pass (1) or Fail (0)

1 0

2 0

3 0

4 1

5 1

6 1

7 1

8 1

After fitting logistic regression:

P(Pass) = 1 / (1 + e^(-(-4 + 1×Hours)))

Predictions:

1 hour study: P = 1/(1+e^(-(-4+1))) = 1/(1+e^3) ≈ 0.05 (5% chance)

3 hour study: P = 1/(1+e^(-(-4+3))) = 1/(1+e^1) ≈ 0.27 (27% chance)

5 hour study: P = 1/(1+e^(-(-4+5))) = 1/(1+e^(-1)) ≈ 0.73 (73% chance)


8 hour study: P = 1/(1+e^(-(-4+8))) = 1/(1+e^(-4)) ≈ 0.98 (98% chance)

The S-curve makes sense: As study hours increase, pass probability increases but not
linearly. The curve is steepest around 4-5 hours.

Decision Boundary: From Probability to Classification

Logistic regression gives us probability. But we need yes/no answer.

Default rule: If P > 0.5, predict 1 (pass/churn). If P ≤ 0.5, predict 0.

But sometimes business needs different threshold:

Example 1: Medical Diagnosis

If predicting rare disease, use P > 0.3

Better to have false alarms than miss actual disease.

Example 2: Fraud Detection

If cost of false positive >> cost of false negative, use P > 0.9

Don't want to block legitimate transactions.

Example 3: Customer Churn

If retention campaign is cheap, use P > 0.3

Catch more customers even if some aren't actually leaving.

Performance Metrics: Confusion Matrix

For classification, we build a confusion matrix:

Actual Positive Actual Negative

Predicted + TP (True +) FP (False +)

Predicted - FN (False -) TN (True -)

From this, we calculate:


Accuracy = (TP + TN) / Total

- What % of predictions were correct?

- Example: 85/100 correct = 85% accuracy

Precision = TP / (TP + FP)

- Of all positive predictions, how many correct?

- Example: 80 predicted churn, 70 actually churned = 70/80 = 87.5% precision

- "When I predict churn, how reliable is that?"

Recall (Sensitivity) = TP / (TP + FN)

- Of all actual positives, how many did I catch?

- Example: 100 customers actually churned, I caught 70 = 70/100 = 70% recall

- "Do I catch most of the actual churners?"

F1-Score = Harmonic mean of precision and recall

- Balances both metrics

- Single number that summarizes both

ROC Curve and AUC

This is a more sophisticated performance metric.

ROC Curve plots true positive rate vs false positive rate at different thresholds.

AUC (Area Under Curve) is the area under the ROC curve.

- AUC = 1.0: Perfect classifier

- AUC = 0.5: Random guessing

- AUC = 0.7: Good classifier

- AUC = 0.9: Excellent classifier

Why use it? Doesn't depend on specific threshold. Shows overall model quality
across all possible thresholds.
Real-World Business Application: Telecom Churn

Setup:

- Telecom company has 10,000 customers

- 2,300 churned (23%), 7,700 stayed (77%)

- Features: tenure, monthly charges, contract type, support calls

Model results:

- Accuracy: 82% (correctly classified 82%)

- Precision: 75% (when we say customer will churn, 75% actually do)

- Recall: 68% (we catch 68% of customers who will actually churn)

- AUC: 0.88 (good model)

Business decision:

- Identify top 500 customers most likely to churn

- Offer special retention package (₹5,000 discount)

- Cost: 500 × ₹5,000 = ₹25 lakh

- Expected benefit: Save 340 customers × ₹20 lakh CLV = ₹680 crore saved

- ROI: (680-0.25)/0.25 = 2,720x!

SESSION 8: ARTIFICIAL NEURAL NETWORKS BASICS

Why Neural Networks?

For many real-world problems, relationships aren't linear or simple. Neural networks can
capture complex patterns that simpler models miss.

Example:

- Linear regression: "Does income increase with age?"

- Neural networks: "What's the complex interplay of age, education, location, and
experience on income?"
Biological Inspiration

Neural networks are loosely inspired by human brain:

- Brain has neurons connected with synapses

- Neurons fire when stimulated

- Strength of connections varies

- Learning happens by adjusting connection strengths

Structure: Layers and Neurons

A neural network is organized in layers:

Input Layer → Hidden Layers → Output Layer

(Features) (Processing) (Prediction)

Example:

Input: Age, Income, Tenure (3 neurons)

Hidden 1: 5 neurons (combine inputs in various ways)

Hidden 2: 3 neurons (higher-level abstractions)

Output: Churn probability (1 neuron)

Forward Pass: How Prediction Works

Each neuron takes inputs, multiplies by weights, adds bias, applies activation function:

Neuron output = Activation( Σ(weight × input) + bias )

Example with 3 inputs and one neuron:

Inputs: Age=35, Income=50, Tenure=5

Weights: w1=0.1, w2=0.02, w3=0.5

Bias: b=0.5
Calculation:

z = 0.1×35 + 0.02×50 + 0.5×5 + 0.5

z = 3.5 + 1 + 2.5 + 0.5 = 7.5

Apply sigmoid activation:

output = 1/(1+e^(-7.5)) ≈ 0.999

Activation Functions: Adding Non-Linearity

Without activation functions, neural network is just multiple linear transformations


(equivalent to linear regression).

Activation functions add non-linearity, enabling complex pattern recognition.

Common Activation Functions:

1. Sigmoid

Output range: 0 to 1

Use: Output layer for binary classification

Shape: S-curve

2. ReLU (Rectified Linear Unit)

If x > 0: output = x

If x ≤ 0: output = 0

Use: Hidden layers (very popular, fast)

Shape: Bent line at zero

3. Tanh (Hyperbolic Tangent)

Output range: -1 to 1

Use: Hidden layers (good for mean-centered data)


Shape: S-curve

4. Softmax

Output: Probability distribution

Sum of all outputs = 1

Use: Multi-class classification (3+ categories)

Training: How Neural Network Learns

Neural networks learn by:

1. Making a prediction

2. Calculating error

3. Backpropagating error to adjust weights

4. Repeating until error is minimized

This process is called Backpropagation .

Loss Functions: What to Minimize

For Binary Classification (Churn: Yes/No): Binary Crossentropy (same as logistic


loss)

For Regression (Predicting Price): Mean Squared Error (MSE)

The optimizer (like Adam or SGD) adjusts weights to minimize this loss.

Hyperparameters: Choices We Make

1. Number of Layers:

- Too few: Underfitting (can't capture complexity)

- Too many: Overfitting (memorizes training data)


- Typical: 2-3 hidden layers for most problems

2. Number of Neurons per Layer:

- Too few: Underfitting

- Too many: Overfitting and slow training

- Common: Start with 50-100, adjust based on performance

3. Learning Rate:

- Too high: Weights bounce around, never converge

- Too low: Very slow training, might get stuck

- Typical: Start with 0.001, adjust if needed

4. Batch Size:

- Small (8-32): Noisier but faster per batch

- Large (256+): Smoother but slower per batch

5. Epochs:

- Number of times to go through entire dataset

- Too few: Underfitting

- Too many: Overfitting

Real-World Example: Customer Lifetime Value Prediction

Problem: Predict how much a customer will spend with us.

Network:

Input (5): Age, Income, Tenure, ProductCategory, Location

Hidden 1 (20): 20 neurons with ReLU

Hidden 2 (10): 10 neurons with ReLU

Output (1): Predicted CLV (continuous, so use ReLU or linear)


Training data: 10,000 customers with known CLV

Results:

- MSE on test data: ₹100 lakh² (RMSE ≈ ₹10 lakh)

- Compared to simple linear regression: RMSE ≈ ₹15 lakh

- Neural network is better!

When to Use Neural Networks?

Good for:

- Complex non-linear relationships

- Large datasets (>10,000 rows)

- Images, text, audio data

- When simpler models aren't good enough

Not good for:

- Small datasets (< 1,000 rows) - will overfit

- Need to understand why prediction was made (black box)

- Real-time predictions needed (too slow)

- When simpler models work well (Occam's razor)

Key Challenges:

Challenge 1: Overfitting

Model memorizes training data, poor on new data.

Solutions: Early stopping, dropout, L1/L2 regularization

Challenge 2: Slow Training

Might take hours or days.

Solutions: GPU acceleration, better hyperparameter tuning


Challenge 3: Need Lots of Data

Neural networks need lots of training examples.

Solutions: Data augmentation, transfer learning

---

SESSION 9: K-NEAREST NEIGHBORS AND BAYESIAN MODELS

K-Nearest Neighbors (KNN): Simple But Effective

Core Idea

KNN is based on a simple principle: Birds of a feather flock together.

If you want to predict something about a new customer, look at the K most similar
existing customers and see what happened to them.

How KNN Works

Step 1: Calculate Distance


For a new data point, calculate distance to all training points.

Common: Euclidean distance

Example:

New customer: Age=35, Income=₹50 lakh

Existing customer 1: Age=36, Income=₹51 lakh → Distance ≈ 1.4

Existing customer 2: Age=40, Income=₹45 lakh → Distance ≈ 5.1

Existing customer 3: Age=33, Income=₹52 lakh → Distance ≈ 2.0

...

Step 2: Find K Nearest

Sort by distance, pick K closest customers.

With K=3:

Closest 3 customers:

1. Customer 1 (Distance 1.4, Churned: No)

2. Customer 3 (Distance 2.0, Churned: Yes)

3. Customer 5 (Distance 3.1, Churned: No)

Step 3: Predict

For classification: Most common class among K neighbors

For regression: Average value among K neighbors

Example - Classification:

- K=3, Neighbors churned: No, Yes, No

- Majority: No

- Prediction: Won't churn


Example - Regression:

- K=3, Neighbors' CLV: ₹2 lakh, ₹5 lakh, ₹3 lakh

- Average: (2+5+3)/3 = ₹3.33 lakh

- Prediction: ₹3.33 lakh CLV

Choosing K: Critical Decision

K=1 (Only nearest neighbor):

Pros: Captures local patterns

Cons: Sensitive to noise, overfitting

K=3 or K=5:

Pros: Balanced, good default

Cons: Might miss local patterns

K=n (All training data):

Pros: Robust, no overfitting

Cons: Too general, underfitting

How to choose? Usually: K = √n where n = number of training samples

Or try different K values and pick best performance.

Advantages of KNN:

1. Simple to understand and implement

2. No training phase (just store data)

3. Good for non-linear problems

4. Good baseline model


Disadvantages of KNN:

1. Slow prediction (calculate distance to all points)

2. Needs lots of memory (store all training data)

3. Sensitive to feature scaling

4. Performs poorly in high dimensions---

Bayesian Models: Probability-Based Learning

Bayes' Theorem: The Foundation

This is the mathematical cornerstone:

P(A|B) = P(B|A) × P(A) / P(B)

Where:

- P(A|B) = Probability of A given B (what we want)

- P(B|A) = Probability of B given A

- P(A) = Prior probability of A

- P(B) = Probability of B

Practical Interpretation

Example: Predicting disease (D) based on symptom (S)

P(Disease | Symptom) = P(Symptom | Disease) × P(Disease) / P(Symptom)

In plain English:

"Probability of disease given symptom = How often symptom appears in diseased


people × Base rate of disease / How often symptom appears overall"

Naive Bayes: Simplified Version

In reality, there are many features. Naive Bayes simplifies by assuming features are
independent:

P(Class | Features) = P(Features | Class) × P(Class) / P(Features)

Assumption: Each feature is independent (unrealistic but works surprisingly well!)

Real Example: Spam Detection

Email classification: Spam or Not Spam?

Training data shows:

P(Spam) = 0.2 (20% of emails are spam)

P(Not Spam) = 0.8 (80% are legitimate)

P(Word "FREE" | Spam) = 0.8 (80% of spam contains "FREE")

P(Word "FREE" | Not Spam) = 0.1 (10% of legitimate emails contain "FREE")

New email arrives with word "FREE":

P(Spam | "FREE") = P("FREE"|Spam) × P(Spam) / P("FREE")

= 0.8 × 0.2 / [0.8×0.2 + 0.1×0.8]

= 0.16 / 0.24

≈ 0.67 (67% likely spam)

Advantages of Naive Bayes:

1. Simple and fast

2. Works well with small datasets

3. Interpretable (you see probability reasoning)

4. Good for text classification

Disadvantages:
1. Naive assumption (independence) often violated

2. Performance might be poor if assumption wrong

3. Struggles with imbalanced classes

SESSION 10: DECISION TREES AND MODEL ASSESSMENT

Decision Trees: Making Predictions Like a Flowchart

Core Idea

A decision tree makes predictions by asking a series of yes/no questions.

Example: "Will customer churn?"

Decision Tree:
Tenure?

/ \

< 12 months ≥ 12 months

/ \

Support / Contract

Calls? / Type?

/ \ / \

>5 ≤5 Month-to- 1+ year

| | month |

CHURN Support CHURN STAY

(high Calls? (high) (low)

risk) / \

>3 ≤3

| |

CHURN STAY

Growing the Tree: How Decisions Are Made

The tree finds the feature that best separates churn from non-churn.

Splitting Criterion: Information Gain

The goal is to reduce uncertainty (entropy) at each split.

Example:
Before split:

- 30 churned, 70 stayed (mixed)

After split on Tenure < 12 months:

- Left (< 12): 20 churned, 10 stayed (mostly churn - pure)

- Right (≥ 12): 10 churned, 60 stayed (mostly stay - pure)

Information gain = Improvement in purity

The tree chooses splits with high information gain.

Impurity Measures:

Gini Impurity (most common):

- Gini = 0: Pure (all same class)

- Gini = 0.5: Completely mixed

- Lower gini = better split

Entropy :

- Another measure of disorder

- High entropy = high disorder

- Low entropy = organized

Tree Size: Avoiding Overfitting

Small Tree:

- Fewer nodes

- More general

- Risk: Underfitting (missing patterns)


Large Tree:

- More nodes

- More specific

- Risk: Overfitting (memorizing noise)

Pruning:

Remove branches that don't help on test data.

Advantages of Decision Trees:

1. Interpretable (anyone can understand the reasoning)

2. No feature scaling needed

3. Handles both numerical and categorical

4. Captures non-linear relationships

Disadvantages:

1. Tends to overfit (large trees memorize)

2. Unstable (small data change, big tree change)

3. Biased toward features with many categories

Model Assessment: Measuring How Good Our Model Is

This applies to all models, so pay attention!

Train vs Test Error

Training Error: How well model fits training data

- Usually good (model learned patterns)

Test Error: How well model works on new unseen data


- What really matters!

Red Flag: If train error << test error = Overfitting!

Validation Strategies:

Method 1: Hold-Out Validation

Split data: 70% train, 30% test

Train on 70%, evaluate on 30%

Simple but loses training data.

Method 2: Cross-Validation

Split data into K folds (e.g., 5 folds)

Train on 4, test on 1

Repeat 5 times, average results

More data used for training

Method 3: Stratified Split

Ensure train and test have same class distribution.

Example: If 23% churn in total:

- Train should have ~23% churn

- Test should have ~23% churn

Key Metrics Summary

For Classification (Churn, Pass/Fail):

- Accuracy: % correct predictions

- Precision: Of positive predictions, how many right?


- Recall: Of actual positives, how many caught?

- F1-Score: Balance of precision and recall

- ROC-AUC: Overall performance across thresholds

For Regression (Predicting Price, Sales):

- MAE: Average prediction error

- RMSE: Penalizes large errors more

- R²: % of variance explained

Practical Decision Framework

If Accuracy = 95%:

- Good? Depends!

- For balanced dataset (50% churn), 95% is excellent

- For imbalanced dataset (5% churn), 95% might be just guessing "no churn"

Always report:

1. Train vs test performance (check for overfitting)

2. Class distribution (in training and test)

3. Multiple metrics (not just one)

4. Confusion matrix (for classification)

SESSION 11: BATCH APPROACH & MODEL VALIDATION

Batch Approach: Processing Data in Groups

What is Batch Processing?

Instead of predicting one customer at a time, process many together.

Batch prediction:

Every Sunday at midnight:


- Get all 50,000 customers

- Score all for churn probability

- Flag top 5,000 for retention campaign

- Update database

- Marketing team uses on Monday

vs

Real-time prediction:

Customer comes to website:

- Immediately score their churn probability

- Show personalized offer based on score

- Response in milliseconds

When to Use Batch:

Pros:

- Efficient (score many at once)

- Lower computing cost

- Can do more complex analysis

- Good for planned actions (campaigns)

Cons:

- Predictions are not instant

- Misses opportunities for real-time intervention

- Need to store results

Batch Processing Pipeline:


1. Extract: Get new customers/data from database

2. Transform: Apply same preprocessing as training

3. Score: Apply model to get predictions

4. Output: Store results

5. Action: Business team uses predictions

Critical: Use SAME preprocessing as training!

Common mistake: Forget to scale in batch, get different predictions.

Cross-Validation: The Gold Standard for Testing

Why Cross-Validation?

Single train-test split can be misleading:

- Might get lucky with one split

- Might get unlucky with one split

Cross-validation uses multiple splits, averages results.

K-Fold Cross-Validation (Most Common)

With K=5:

Dataset: 1000 customers, 10 features

Fold 1: Train on Fold 2,3,4,5 → Test on Fold 1

Fold 2: Train on Fold 1,3,4,5 → Test on Fold 2

Fold 3: Train on Fold 1,2,4,5 → Test on Fold 3

Fold 4: Train on Fold 1,2,3,5 → Test on Fold 4

Fold 5: Train on Fold 1,2,3,4 → Test on Fold 5


Average the 5 test scores to get final estimate.

Advantage: Uses all data, more reliable estimate

Time Series Cross-Validation

For time-ordered data (stock prices, sales), use special validation:

Don't randomly split!

Training: Jan-Oct 2024

Test: Nov 2024

Why? Can't use future data to predict past.

Stratified Cross-Validation

For imbalanced classification, ensure each fold has same class distribution:

If 20% churn overall:

Each fold should have ~20% churn

(not 15% in fold 1, 25% in fold 2)

---

Hyperparameter Tuning: Finding Optimal Settings

Models have many settings we choose:

- Depth of decision tree

- K in KNN

- Learning rate in neural network

- Regularization strength
Grid Search: Try All Combinations

Tree depth options: 3, 5, 7, 10

Min samples per leaf: 1, 5, 10

Test all combinations:

(3, 1), (3, 5), (3, 10)

(5, 1), (5, 5), (5, 10)

(7, 1), (7, 5), (7, 10)

(10, 1), (10, 5), (10, 10)

Total: 12 combinations tested

Pick best performing on validation data.

Random Search: Sample Randomly

When grid search is too expensive (too many combinations), randomly sample.

More efficient for large hyperparameter space

Process:

1. Define hyperparameter ranges

2. Use cross-validation to test each combo

3. Pick combo with best CV score

4. Final test on held-out test set


SESSION 12: ADVANCED REGRESSION TECHNIQUES

Polynomial Regression: Capturing Curved Relationships

When Linear Isn't Enough

Sometimes relationship is curved, not straight line.

Example: Predicting growth cost

Years 0-5: Cost increases 10% per year

Years 5-15: Cost increases 50% per year

Years 15+: Cost increases 200% per year

Linear line would miss this curve.

Adding Polynomial Terms

Instead of:

y = b0 + b1×x

Use:

y = b0 + b1×x + b2×x² + b3×x³ + ...

Example with house age predicting price:

Linear: Price = 100 - 2×Age

(price decreases linearly with age)

Polynomial: Price = 100 - 2×Age + 0.5×Age²

(price decreases fast initially, then levels off)

Risks:
Overfitting: Too many polynomial terms, fits noise

Solution: Start low (x²), add only if needed

Regularization: Preventing Overfitting

The Problem: Too Many Features

With many features, model can fit training data perfectly but fail on new data.

Regularization Solutions:

Ridge Regression (L2):

- Penalizes large coefficients

- Shrinks coefficients toward zero

- Keeps all features (but with smaller values)

Lasso Regression (L1):

- Penalizes large coefficients

- Can force some coefficients to exactly zero

- Performs feature selection (removes least important)

Elastic Net:

- Combination of Ridge and Lasso

- Best of both worlds

Strength Parameter (λ):

λ = 0: No regularization (standard regression)

λ = small: Mild regularization

λ = large: Strong regularization (underfitting risk)

Choose via cross-validation.---

Interaction Terms: When Features Matter Together


Sometimes two features have synergistic effect.

Example: Predicting sales

Marketing spend alone: Increases sales

Social media presence alone: Increases sales

Marketing × Social media together: Huge increase!

Add interaction term:

Sales = b0 + b1×Marketing + b2×Social + b3×(Marketing × Social)

SESSION 13: ENSEMBLE METHODS INTRODUCTION

The Power of Combining Models

Core Idea: Wisdom of Crowds

One model might be wrong. Multiple models averaging together are usually better.

Example: Netflix Recommendation

- Model 1: "Watch based on genre"

- Model 2: "Watch based on similar users"


- Model 3: "Watch based on ratings patterns"

Combined prediction > any single model.

Two Main Ensemble Strategies:

Strategy 1: Bagging (Bootstrap Aggregating)

Create multiple versions of dataset, train different models:

Original dataset: 1000 rows

Create dataset 1: Randomly sample 1000 rows (with replacement)

Train Model 1

Create dataset 2: Randomly sample 1000 rows (with replacement)

Train Model 2

...repeat 50 times...

Final prediction: Average of all 50 models

Advantage: Reduces overfitting (averaging smooths out noise)

Example: Random Forest (bags of decision trees)

Strategy 2: Boosting

Train models sequentially, each correcting previous model's errors:

Train Model 1 on data

Model 1 makes mistakes

Train Model 2 focusing on Model 1's mistakes

Model 2 corrects some errors

Train Model 3 focusing on remaining mistakes

Model 3 corrects more


Final prediction: Weighted combination of all models

Advantage: Reduces both bias and variance

Examples: AdaBoost, Gradient Boosting

Comparison:

| Feature | Bagging | Boosting |

|---------|---------|----------|

| Error reduction | Variance | Bias + Variance |

| Training | Parallel | Sequential |

| Speed | Faster | Slower |

| Overfitting risk | Lower | Higher if too many iterations |

| Feature importance | Less clear | Clearer |

SESSION 14: RANDOM FORESTS AND ADVANCED ENSEMBLE CONCEPTS

Random Forests: Ensemble of Decision Trees

How It Works:

1. Create B random subsets of data (bootstrap samples)

2. For each subset:

- Grow a decision tree (randomly select features at each split)

- Don't prune (grow fully)


3. Make B predictions

4. Final prediction:

- Classification: Majority vote

- Regression: Average

Why Random Features at Each Split?

Adds diversity. If all trees use same features, ensemble is just repetition.

By randomly selecting features, trees are different, capturing different patterns.

Advantages:

1. Handles both regression and classification

2. Robust to outliers (one tree's error doesn't dominate)

3. Feature importance (see which features matter)

4. No feature scaling needed

5. Captures non-linear relationships

6. Handles mixed data types

Disadvantages:

1. Black box (hard to explain why prediction was made)

2. Memory intensive (store many trees)

3. Slower prediction (need all trees)

Gradient Boosting: Superior Performance

Key Idea:

Iteratively improve model by:

1. Train weak model

2. Calculate residuals (errors)


3. Train next model to predict residuals

4. Repeat

Each model learns what previous model missed.

Example:

House price prediction:

Model 1: Predicts ₹50L, actual ₹60L

Residual: ₹10L error

Model 2: Trained on residuals

Predicts error = ₹8L

Combined: ₹50L + ₹8L = ₹58L (better!)

Model 3: New residuals = ₹2L

Predicts error = ₹1.5L

Combined: ₹58L + ₹1.5L = ₹59.5L (even better!)

Why Better Than Bagging:

- Bagging: Multiple models in parallel (each fixes random errors)

- Boosting: Sequential (each model focuses on remaining errors)

Result: Boosting converges faster, needs fewer iterations.

Popular Implementations:

- XGBoost: Fast, production-ready

- LightGBM: Memory efficient

- CatBoost: Handles categorical features well---

SESSION 15: MODEL VALIDATION AND TUNING


Comprehensive Validation Strategy

Step 1: Training vs Validation vs Test

Raw Data (100%)

Training Set (70%): Train model

Validation Set (15%): Tune hyperparameters

Test Set (15%): Final evaluation

Why three sets?

- Training: Learn patterns

- Validation: Tune settings without fooling ourselves

- Test: Honest final performance estimate

Step 2: Performance Metrics

Choose metrics based on business goal:

Churn Prediction:

Primary: Recall (don't miss actual churners)

Secondary: Precision (don't waste resources on false alarms)

Fraud Detection:

Primary: Recall (catch fraud)

Secondary: Precision (don't block legitimate transactions)

Spam Detection:

Primary: Precision (don't delete legitimate emails)

Secondary: Recall (some spam OK if no false positives)


Step 3: Sensitivity Analysis

Test how predictions change with small data changes.

Example:

- Original prediction: 70% churn probability

- Change one customer feature by 10%: Still 70%? Or 65%?

Robust model: Predictions don't wildly change

Fragile model: Small changes cause big prediction changes

Hyperparameter Optimization: Advanced

Bayesian Optimization:

Instead of testing all combinations (expensive), use smart sampling.

1. Test random hyperparameters: A, B, C

2. Learn which seem promising

3. Test near promising values

4. Repeat until converged

Finds good hyperparameters with fewer tests.

Early Stopping:

For iterative models (neural networks, gradient boosting):

Monitor validation error:

- If improving: Keep training

- If not improving: Stop (don't overfit)

Epoch 1: Train error 0.5, Val error 0.52

Epoch 2: Train error 0.45, Val error 0.48


...

Epoch 50: Train error 0.10, Val error 0.50 ← Overfitting!

Epoch 51: Train error 0.09, Val error 0.51

Stop here! Don't go beyond Epoch 50.

---

SESSION 16: DEPLOYMENT OF PREDICTIVE MODELS

From Model to Production

What is Deployment?

Taking a model from Jupyter notebook and making it operational (available to users).

Deployment Approaches:

Batch Scoring:

Sunday midnight:

- Run model on all 50,000 customers

- Store predictions in database

- Monday, marketing team uses

Pros: Efficient, uses resources well

Cons: Not real-time, predictions become stale

Real-Time API:

Customer arrives on website

→ API call to model server


→ Get prediction instantly (50ms)

→ Show personalized offer

→ API call returns

Pros: Instant, always fresh

Cons: Need fast server, constant traffic handling

On-Device:

Mobile app downloads model

→ Predictions happen on phone

→ No internet needed

→ Privacy preserved

Pros: Privacy, no network latency

Cons: Phone has limited compute, disk space

Deployment Checklist:

Before Deployment:

- [ ] Model performs well on test data

- [ ] Validation strategy documented

- [ ] Model inputs documented

- [ ] Expected output ranges defined

- [ ] Error handling defined

During Deployment:

- [ ] Model saved in standard format (.pkl, .h5, .onnx)

- [ ] Preprocessing pipeline stored too

- [ ] Version control (can revert if issues)

- [ ] Monitoring dashboard set up


After Deployment:

- [ ] Track predictions vs actual outcomes

- [ ] Alert if performance degrades

- [ ] Retrain periodically

- [ ] Update model if new patterns appear

--

Model Monitoring: The Ongoing Process

What to Monitor:

1. Performance Drift:

Model was 85% accurate at launch

After 3 months: 78% accurate

Investigation: Customer behavior changed

Action: Retrain model with new data

2. Data Drift:

Training data: 40% Premium segment

New data (production): 10% Premium segment

Impact: Model works worse for Standard/Basic

Action: Retrain with balanced distribution

3. Prediction Stability:

Same customer on Monday: 70% churn probability

Same customer on Tuesday: 72% churn probability

Normal variation? Or model unstable?

Retraining Strategy:

Option 1: Regular Retraining


Retrain every week/month regardless of performance.

Good for stable, evolving patterns.

Option 2: Performance-Based

Retrain when performance drops below threshold.

Good for stable systems.

Option 3: Trigger-Based

Retrain when business event happens (new competitor, market change).

Good for stable business.

---

SESSION 17: INDUSTRY USE CASES

Real-World Applications Across Sectors

Case 1: Banking - Credit Risk Prediction

Business Problem:

Every loan application is risky. Need to predict default probability.

Features Used:

- Income, credit score, employment history


- Debt-to-income ratio, collateral value

- Age, employment stability

Model:

Logistic regression (interpretability required for audit trail)

Results:

- Approve applicants with default probability < 5%

- Review zone: 5-20% (human review)

- Reject: > 20%

Business Impact:

- Reduce write-offs by 30%

- Process applications faster (automate low-risk)

- Comply with regulations (explainability)

Case 2: Retail - Demand Forecasting

Business Problem:

What inventory do we need next quarter?

Too much: Waste money on storage/markdowns

Too little: Lose sales

Features Used:

- Historical sales, seasonal patterns

- Marketing campaigns, competitor actions

- External factors (weather, holidays)

Model:

Time series forecasting (ARIMA or neural networks)


Results:

Forecast next 90 days sales within 10% accuracy

Business Impact:

- Reduce inventory costs 15%

- Prevent stockouts 20% improvement

- Optimize warehouse space

Case 3: Healthcare - Patient Readmission Risk

Business Problem:

Which discharged patients will return within 30 days?

Features Used:

- Age, diagnosis, comorbidities

- Hospitalization length, procedures

- Social factors (lives alone?), insurance

Model:

Random forest (handles many features, captures complexity)

Results:

Identify 70% of high-risk patients

Business Impact:

- Target follow-up calls to high-risk patients

- Prevent readmissions, reduce costs

- Improve patient outcomes

Case 4: Telecom - Network Failure Prediction

Business Problem:
Prevent network outages before they happen.

Features Used:

- Traffic load, historical patterns

- Temperature, equipment age

- Maintenance history

Model:

Gradient boosting (handles temporal patterns)

Results:

Predict failures 24 hours ahead with 85% accuracy

Business Impact:

- Proactive maintenance (prevent outages)

- Schedule repairs before impact

- Customer satisfaction improvement

---

SESSION 18: CHALLENGES IN PREDICTIVE ANALYTICS

Real-World Obstacles

Challenge 1: Data Quality

Problem: Data is dirty

- Missing values: 15-30% of fields

- Duplicates: Customers appear multiple times


- Errors: Age = 999, Prices = negative

- Inconsistency: Different spellings, formats

Solution:

- Invest in data governance

- Automated validation checks

- Manual review for critical fields

- Root cause analysis of errors

Challenge 2: Model Interpretability vs Performance

Trade-off:

- Simple models (logistic, decision trees): Explainable but less accurate

- Complex models (neural networks, random forest): Better accuracy but black box

Business Dilemma:

Bank wants to reject a loan application. Customer asks: "Why?"

- If using simple model: "Your debt-to-income > 40%"

- If using deep neural network: ??? (can't explain)

Solution:

Use SHAP values, LIME, feature importance to interpret complex models

Challenge 3: Imbalanced Classes

Problem:

- Predicting fraud: 99.9% legitimate, 0.1% fraud

- Predicting cancer: 99% healthy, 1% cancer

- Model can achieve 99% accuracy by saying "never fraud"!

Solution:

- Use appropriate metrics (recall, precision, F1, not just accuracy)


- Adjust class weights

- Stratified sampling

- SMOTE (synthetic minority oversampling)

Challenge 4: Concept Drift

Problem:

Model worked great last year, now performance dropped.

Causes:

- Customer behavior changed

- Market conditions changed

- Competitors' actions

- Seasonal shifts

Solution:

- Regular monitoring

- Trigger-based retraining

- Ensemble of diverse models

- Continuous learning systems

Challenge 5: Ethical Issues

Problem:

Model discriminates against protected groups (gender, race, etc.)

Example:

- Loan model: Rejects 80% of minority applicants

- Hiring model: Favors certain demographics


- Recidivism model: Biased against certain groups

Solution:

- Monitor fairness metrics

- Audit for bias

- Remove/neutralize sensitive features carefully

- Use fairness-aware machine learning

- Transparency about model limitations

Challenge 6: Scalability

Problem:

Model works on 100K rows, fails on 100M rows

- Training takes days

- Predictions take seconds per customer (not real-time)

- Memory exceeds available

Solution:

- Use distributed computing (Spark)

- Efficient algorithms (approximate algorithms)

- Feature reduction - Model compression

SESSION 19: ANSWERING QUESTIONS & PERSUASION TECHNIQUES

Communicating Model Results

Audience Matters!

Executive Summary (for C-suite):

"Our churn model will save ₹50 crore annually by identifying at-risk customers.
Implementation cost is ₹1 crore. ROI is 50x."

Technical Details (for data team):

"Using 5-fold cross-validation, gradient boosting achieves AUC=0.92, recall=0.78,


precision=0.82. Feature importance: tenure (0.3), support_calls (0.25)..."

Business Owner (marketing):

"You'll get a list of 1,000 customers most likely to churn. For each, here's the best offer.
Expected campaign ROI is 400%."

Key Persuasion Techniques:

1. Lead with Business Value

Don't start with metrics.

Bad: "Our model achieves 85% accuracy with AUC 0.87..."

Good: "We can save ₹25 crore by identifying and retaining at-risk customers."

2. Use Analogies

Complex concepts need simple explanations.

Bad: "We use gradient boosting with L2 regularization and early stopping..."

Good: "The model is like a smart teacher. It learns from mistakes, corrects itself, and
knows when to stop learning to avoid overthinking."

3. Show Examples

Numbers are abstract. Examples are concrete.

Bad: "The model improves fraud detection by 25%"

Good: "Previously, 3 fraudulent transactions per day slipped through. Now it's less than
1. Last month alone, we caught ₹50 lakh in fraud that would've been missed."

4. Address Concerns Proactively

Anticipate questions.

"You might be wondering: Will this replace our current process?

No. It complements it. Your team still makes final decisions. This model just helps you
focus on high-risk cases."

5. Share Uncertainties Honestly

Build trust by acknowledging limitations.


"This model is 80% accurate. That means 1 in 5 predictions might be wrong. For critical
decisions, human review is essential. We recommend using this as a first filter, not final
decision."

Common Questions You'll Face:

Q: How do I know if the model is good?

A: "We tested it on data the model has never seen before. It correctly identifies 80% of
actual churners and gives false alarms on 15% of stayers. Compare this to your current
process."

Q: What if business changes?

A: "Good question. We monitor performance monthly. If accuracy drops below 75%, we


automatically retrain. Plus, we document all changes and can see what caused
performance shift."

Q: Can we trust this for critical decisions?

A: "Don't trust any model 100%. Think of it as a recommendation system, not oracle.
High-value decisions should combine model output with human judgment."

---

SESSION 20: REVIEW AND ASSESSMENT PREPARATION

Key Concepts Summary

Foundational (Sessions 1-5):

What is Predictive Analytics?

Using historical data to forecast future events. Applications: fraud, churn, demand,
pricing.

Problem Setup:
SMART definition. Clear target variable, relevant features, measurable success metrics.

Data Types:

Nominal, ordinal, interval, ratio. Different encoding methods. Handle missing data
appropriately.

Project Lifecycle:

CRISP-DM: Business → Data → Prepare → Model → Evaluate → Deploy. Iterative


process.

EDA:

Understand distributions, relationships, outliers. Statistical analysis + visualization.

Modeling (Sessions 6-12):

Regression:

Predict continuous values. Minimize squared errors. Assumptions matter (linearity,


independence)

Logistic Regression:

Classification with probability output. Sigmoid function ensures 0-1 probabilities.


Interpret via odds ratios.

Neural Networks:

Learn complex patterns via layers of neurons. Backpropagation trains weights. More
data needed.

KNN & Bayes:

KNN: Similar neighbors predict similar outcomes.

Bayes: Probabilistic; uses class priors.

Decision Trees:

Ask questions, build flowchart. Information gain drives splits. Prone to overfitting.

Advanced Regression:

Polynomial terms for curves. Regularization (Ridge/Lasso) prevents overfitting.


Interaction terms capture synergies.

Ensembles (Sessions 13-15):

Bagging:

Train multiple models on bootstrap samples. Average predictions. Reduces variance.

Boosting:

Sequential models, each corrects previous errors. Reduces both bias and variance.

Random Forests:

Many decision trees with random features. Interpretable feature importance.

Gradient Boosting:

Iteratively fit residuals. XGBoost, LightGBM popular implementations.

Validation:

Cross-validation for reliable estimates. Hyperparameter tuning via grid search. Early
stopping prevents overfitting.

Deployment & Real-World (Sessions 16-20):

Deployment:

Batch or real-time. Monitor performance. Retrain when drift detected.

Industry Applications:

Banking (credit), Retail (demand), Healthcare (readmission), Telecom (failure).

Challenges:

Data quality, interpretability, imbalanced classes, concept drift, ethical bias, scalability.

Communication:

Lead with business value. Use examples. Address concerns. Acknowledge limitations.

---
Exam Preparation Tips

What to Study:

1. Formulas:

- Linear regression: y = b0 + b1x1 + ...

- Logistic regression: P = 1/(1+e^-z)

- Confusion matrix metrics: TP, FP, TN, FN

- R² interpretation

2. Concepts:

- When to use each algorithm

- Assumptions of each method

- Overfitting vs underfitting

- Bias-variance tradeoff

3. Applications:

- Real-world use cases

- Business impact calculations

- Data preparation steps

4. Calculations:

- Basic statistics (mean, std dev)

- Correlation

- Confusion matrix metrics (precision, recall, F1)

- Prediction examples

Sample Exam Questions:

Question 1 (Short Answer):


"Explain why we use logistic regression for classification instead of linear regression."

Answer: Linear regression can predict values outside [0,1]. Logistic regression uses
sigmoid function to constrain predictions to valid probabilities. S-shaped curve matches
real-world relationships.

Question 2 (Calculation):

"Confusion matrix shows: TP=70, FP=20, TN=850, FN=60. Calculate precision and
recall."

Precision = TP/(TP+FP) = 70/90 = 0.78 (78%)

Recall = TP/(TP+FN) = 70/130 = 0.54 (54%)

Question 3 (Analysis):

"Your model achieves 95% accuracy on test data but 98% on training data. Explain."

Overfitting. Model memorized training data, general patterns don't hold on test data.
Solutions: Simpler model, more data, regularization, cross-validation.

Question 4 (Application):

"Design a model to predict customer lifetime value for a retail company. What data
would you need? Which algorithm? How would you validate?"

Data: Demographics, purchase history, product preferences, engagement

Algorithm: Regression (predicting continuous CLV)

Validation: 70-30 train-test split, cross-validation, metrics: RMSE, R²

---

Final Reminders for Success

1. Understand, Don't Memorize: Learn the "why" behind algorithms, not just
formulas.
2. Practice on Real Data: Work through examples with actual numbers.

3. Think Business First: Always connect technical concepts to real-world impact.

4. Know Your Tools: Understand pros/cons of each algorithm.

5. Communicate Clearly: Be able to explain concepts simply.

6. Consider Edge Cases: What happens with imbalanced data? Outliers? Missing
values?

7. Ethical Awareness: Models can discriminate. Be aware of fairness issues.

8. Continuous Learning: This field evolves rapidly. Stay curious.

Final Note

Predictive analytics is both art and science. The science is the math and algorithms.
The art is knowing when to use which tool, asking the right questions, and
communicating results effectively.

Success comes from:

- 80% spending time understanding business and data

- 20% tuning algorithms

Not the other way around.

Good luck with your exams! Remember: You're not just learning algorithms. You're
learning to solve real business problems with data.

Common questions

Powered by AI

Handling missing data carefully is crucial because it affects the quality of the dataset and, consequently, the predictive accuracy of the models. Missing data can introduce bias, reduce statistical power, and affect the generalization of the model to new data. Moreover, strategies such as imputing missing values with means or predicting them using other variables ensure that all data are considered without introducing bias, although they add complexity to the process .

Data quality has a profound impact on predictive capabilities; poor data quality leads to inaccurate predictions. Manifestations include biased results stemming from erroneous or skewed data, such as predictions drawn from misrepresented income values or regionally biased datasets, leading to incorrect generalizations and poor performance in different contexts .

Batch processing is advantageous when predictions do not need to be instant and can be scheduled, such as in planned marketing campaigns or regular analysis. It allows processing many records simultaneously, reducing computing costs and enabling complex analysis, whereas real-time prediction is better for immediate customer-facing decisions requiring immediate responses .

Cross-validation provides a more reliable estimate of model performance than a single train-test split by using multiple splits and averaging the results, which reduces the influence of any single split that might be lucky or unlucky. It maximizes training data usage for each fold while ensuring robust testing, thus offering a more comprehensive assessment of the model's ability to generalize to unseen data .

Neural networks are ideal for scenarios involving complex non-linear relationships, large datasets (over 10,000 rows), and tasks like image, text, or audio processing where simpler models may not perform well. They are chosen when the problem complexity requires capturing intricate patterns which simpler models can't detect, though they require substantial computational resources and large amounts of data .

The primary objective of the CRISP-DM framework is to provide a structured approach to predictive analytics projects. It involves six phases: business understanding, data understanding, data preparation, modeling, evaluation, and deployment. This ensures a comprehensive understanding of business problems, systematic data handling, effective model creation, accurate evaluation, and successful deployment .

Min-max scaling transforms features to a standard range, such as 0-1, which prevents algorithms like those using distance metrics from being biased toward larger-scale variables. This process ensures that all features contribute equally to the prediction and are assessed based on patterns rather than magnitude differences, thereby improving the algorithm's accuracy and fairness .

The sigmoid function in logistic regression maps predicted values to a range between 0 and 1, translating the linear output into probabilities. This makes it particularly suited for binary classification tasks, as it models real-world probabilities appropriately and allows straightforward interpretation of outputs as class membership likelihoods .

Balancing training and test error rates is crucial to avoid overfitting, where a model performs well on training data but poorly on unseen data due to memorizing rather than generalizing patterns. A significant discrepancy, where training error is much lower than test error, indicates overfitting. This suggests the model's complexity exceeds what's necessary to capture underlying trends, necessitating adjustments like simplifying the model or adding regularization .

The SMART framework enhances problem definition by ensuring that goals are Specific, Measurable, Achievable, Relevant, and Time-bound. This clarity helps in accurately identifying what needs to be predicted, setting measurable targets, ensuring goals are attainable with available data, aligning predictions with business objectives, and setting clear timelines for prediction . Without this specificity, efforts can be misdirected, leading to suboptimal outcomes .

You might also like