0% found this document useful (0 votes)
44 views30 pages

Machine Learning Using Python Notes

The document is a comprehensive guide on Machine Learning using Python, covering foundational concepts, advantages, challenges, and applications across various industries. It includes detailed modules with theory, long-form Q&A, exam-style case studies, and Python code implementations to aid in understanding and exam preparation. The guide aims to provide a complete resource for mastering Machine Learning and successfully passing related exams.
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)
44 views30 pages

Machine Learning Using Python Notes

The document is a comprehensive guide on Machine Learning using Python, covering foundational concepts, advantages, challenges, and applications across various industries. It includes detailed modules with theory, long-form Q&A, exam-style case studies, and Python code implementations to aid in understanding and exam preparation. The guide aims to provide a complete resource for mastering Machine Learning and successfully passing related exams.
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

Summary

Machine Learning
Using Python

Comprehensive Study Notes

Based on the Complete Official Syllabus


Modules 1, 2 & 3

Summary

Featuring Inside:
• In-Depth Theory & Concepts
• 30+ Long-Form Q&A
• 12+ Detailed Exam-Style Case Studies
• Python Code Implementations

Your Complete Guide to Acing the Exam

2
Contents

1 Module 1: Foundations of Machine Learning 2


1.1 Introduction to Machine Learning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.2 Advantages of Machine Learning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.3 Challenges in Machine Learning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.4 Applications of Machine Learning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

2 Module 2: Machine Learning with Python 8


2.1 Python for Machine Learning (ML) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.2 Data Preparation for ML with Python . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.3 Data Handling with ML Python Packages . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.4 Data Treatment for ML Using Pandas . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.5 Classification in ML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.6 Cluster Analysis in ML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.7 Supervised Learning for ML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.8 Unsupervised Learning for ML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

3 Module 3: Case Project Guidelines 15


3.1 Steps for Project Evaluation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
3.2 Example Case Project: Heart Disease Prediction . . . . . . . . . . . . . . . . . . . . . 15

A Appendix: Comprehensive Reference 19


A.1 Commands Table & Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
A.2 Program Syntax Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
A.3 Example Programs (Solved) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
A.4 Unsolved Case Studies for Practice (10) . . . . . . . . . . . . . . . . . . . . . . . . . . 25
A.5 Answer Key for Unsolved Case Studies . . . . . . . . . . . . . . . . . . . . . . . . . . 27

1
Chapter 1

Module 1: Foundations of Machine


Learning

1.1 Introduction to Machine Learning


Machine Learning (ML) is a branch of Artificial Intelligence (AI). In simple terms, it is the science
of teaching computers to learn from data and improve from experience without being explicitly
programmed.
Imagine teaching a child to identify an apple. You don’t give them a mathematical formula
for an apple; you show them pictures of apples. Over time, their brain learns the pattern. Ma-
chine Learning does the same thing with algorithms and data.

Visualizing the ML Workflow

Historical Data → Machine Learning Algorithm → Predictive Model → New Predictions

Figure 1.1: The Basic Flow of Machine Learning.

1.2 Advantages of Machine Learning


Machine Learning has revolutionized how we solve problems. Here are its main advantages:

• Automation of Tasks: ML can automate repetitive, data-heavy tasks, saving human time.

• Continuous Improvement: As ML algorithms process more data, they become more ac-
curate. They learn from their mistakes.

• Handling Multi-dimensional Data: Humans struggle to find patterns in spreadsheets


with thousands of columns. ML handles multi-dimensional and varied data easily.

2
• Discovering Hidden Patterns: It can find trends that are not immediately obvious to hu-
man analysts.

1.3 Challenges in Machine Learning


Despite its power, ML is not magic. It faces several challenges:

• Data Quality: ”Garbage in, garbage out.” If the data is messy, incomplete, or biased, the
ML model will make bad predictions.

• Time and Resources: Training complex models requires a lot of time and powerful com-
puters (GPUs).

• Overfitting and Underfitting: Overfitting happens when a model learns the training data
*too* well (memorizes it) but fails on new data. Underfitting happens when the model is
too simple to learn anything useful.

• Interpretability: Some complex models (like Deep Neural Networks) act like ”black boxes.”
It is hard to explain *why* they made a certain decision.

1.4 Applications of Machine Learning


Machine Learning is everywhere in our daily lives:

1. Healthcare: Predicting diseases from X-rays and personalizing medicine.

2. Finance: Detecting fraudulent credit card transactions in real-time.

3. E-commerce & Entertainment: Netflix and Amazon recommending movies or products


based on your past behavior.

4. Transportation: Self-driving cars using computer vision to navigate roads.

3
Module 1 Summary Report

This module introduced the foundational concepts of Machine Learning.

• Core Concept: ML allows systems to learn from data automatically.

• Pros: Automation, continuous learning, and handling complex data.

• Cons: Heavy reliance on high-quality data, computational costs, and the risk of over-
fitting.

• Impact: It is actively transforming industries like healthcare, finance, and entertain-


ment through predictive modeling and pattern recognition.

Solved Questions & Answers - Module 1


Q1: Elaborate on the fundamental shift in problem-solving between Traditional Program-
ming and Machine Learning.
Answer: In traditional programming, a developer must explicitly hard-code the logic and rules
(e.g., if balance < 0 then alert). You input Data and Rules to get Answers. In Machine Learn-
ing, the paradigm shifts entirely. You input massive amounts of historical Data and the known
Answers (labels), and the ML algorithm processes them to independently figure out and output
the Rules (the predictive model).

Q2: Why is the concept of ”Overfitting” considered a major pitfall in ML?


Answer: Overfitting occurs when a model is excessively complex and essentially ”memorizes”
the noise, outliers, and exact details of the training data rather than learning the generalized
underlying patterns. As a result, while it performs with near-perfect accuracy on training data,
it fails drastically when exposed to new, unseen real-world data.

Q3: Define Underfitting and how it contrasts with Overfitting.


Answer: Underfitting is the opposite of overfitting. It happens when an ML model is too simple
(e.g., using a straight line to map highly curved data). It fails to capture the underlying trend of
the data, resulting in poor accuracy on both the training data and new data.

Q4: What role does Data Quality play in the success of an ML model?
Answer: Data Quality is the most critical factor. ML models learn purely based on the data
fed into them. If the data is biased, contains missing values, or has incorrect labels (”Garbage
In”), the resulting model will naturally produce inaccurate, biased, and unreliable predictions
(”Garbage Out”), regardless of how advanced the mathematical algorithm is.

Q5: Explain why Machine Learning has seen a massive surge in popularity in the last
decade compared to earlier years.
Answer: The surge is driven by three converging factors: 1) Big Data: The internet boom cre-
ated massive datasets required to train accurate models. 2) Computing Power: The advent of
affordable, high-performance GPUs allowed computers to process parallel matrix math quickly.
3) Algorithmic Advancements: Open-source libraries (like Scikit-Learn and TensorFlow) made
advanced algorithms highly accessible.

Q6: Is Machine Learning the optimal solution for all software engineering problems?
Justify your answer.
Answer: No. ML is resource-heavy, probabilistic (not 100% certain), and complex. For systems
with clear, deterministic, and static mathematical rules (like calculating standard income tax,

4
sorting a database, or running a payroll system), traditional programming is significantly faster,
cheaper, and 100% reliable. ML should be reserved for pattern recognition and complex predic-
tive tasks.

Q7: Describe how Machine Learning is transforming the Healthcare sector.


Answer: ML is used for predictive diagnostics (analyzing MRI and X-ray scans to detect tumors
earlier than human doctors), personalizing patient treatment plans based on genetic data, pre-
dicting patient readmission rates to optimize hospital bed availability, and drastically speeding
up the discovery of new pharmaceutical drugs.

Q8: What is meant by the ”Interpretability” challenge (The Black Box problem) in ML?
Answer: Interpretability refers to the ability to explain *why* an ML model made a specific
decision. For complex algorithms like Deep Neural Networks, the math involves millions of
hidden calculations. If an AI denies a user a loan or misdiagnoses a patient, it is incredibly
difficult for engineers to open the ”black box” and explain the exact logic behind that failure.

Q9: Differentiate between a ”Feature” and a ”Label” in the context of ML data.


Answer: A Feature (or input variable) is an independent variable used to make a prediction
(e.g., a house’s square footage, age, and number of bedrooms). A Label (or target variable) is
the final output or the ”answer” the model is trying to predict (e.g., the final sale price of that
house).

Q10: Briefly explain the concept of handling Multi-dimensional data in ML.


Answer: Humans can easily visualize and find trends in 2D or 3D data (like a scatter plot of
Age vs. Income). However, real-world data often has hundreds of dimensions (e.g., an image
has thousands of pixel values; a patient record has 50 different lab results). ML algorithms
utilize linear algebra to easily process and find mathematical correlations across thousands of
dimensions simultaneously.

5
Module 1: Exam-Style Case Studies (5-8 Marks)

Case Study 1: The Banking Loan Automation System

Scenario: A major national bank currently relies on 50 human loan officers to manually
review applicant data (Income, Credit Score, Years Employed, Existing Debt) to decide
whether to ’Approve’ or ’Reject’ personal loan applications. The process is slow and in-
consistent. The bank’s IT director wants to replace this manual review with a Machine
Learning system trained on 10 years of historical banking data.
Questions:

(a) Identify the specific type of Machine Learning problem this represents (Supervised
vs. Unsupervised) and justify your choice. (2 Marks)

(b) Identify the ’Features’ and the ’Target Label’ in this scenario. (2 Marks)

(c) If the historical data provided to the system contains a bias where officers unfairly
rejected applications from a specific postal code, what will happen to the ML model?
What is this challenge called? (4 Marks)

Detailed Answer:

(a) This represents Supervised Learning (specifically Binary Classification). It is Super-


vised because the bank possesses 10 years of historical data that already contains
the ”right answers” (the past decisions made by human officers). The model learns
from these labeled examples.

(b) Features (Inputs): Income, Credit Score, Years Employed, and Existing Debt. Target
Label (Output): The final decision (’Approve’ or ’Reject’).

(c) If the training data contains human bias, the ML model will learn and replicate that
exact bias. It will automatically start rejecting perfectly qualified applicants simply
because they live in that postal code. This highlights the Data Quality Challenge
(”Garbage In, Garbage Out”), proving that models are not inherently objective; they
simply mirror the flaws in their training data.

Case Study 2: Marketing Persona Discovery

Scenario: A new e-commerce startup has gathered a database of 500,000 registered


users. They have tracked user click rates, time spent on the site, and average cart value.
The marketing team wants to launch targeted email campaigns. However, they have ab-
solutely no predefined customer categories and do not know how their users should be
grouped.
Questions:

(a) Why is Supervised Learning inappropriate for this scenario? (2 Marks)

(b) What specific Machine Learning approach and sub-category must be used here to
help the marketing team? Explain how it will work. (4 Marks)

Detailed Answer:

(a) Supervised Learning is inappropriate because it requires a ”Target Label” (an answer
key) to train on. The startup has raw user statistics but no pre-existing categories or
labels defining what type of customer each user is.

6
(b) The startup must use Unsupervised Learning, specifically Clustering. Because
the algorithm is not given an answer key, it will autonomously analyze the multi-
dimensional data (click rates, time, cart value) and group users together based en-
tirely on mathematical similarities. For example, it might naturally cluster users into
”High-spending quick shoppers” and ”Low-spending slow browsers,” allowing the
marketing team to target them effectively.

Case Study 3: The Flawed Spam Filter

Scenario: A software engineer builds an ML model to filter out Spam emails. To train the
model quickly, the engineer downloads a dataset containing 9,900 examples of malicious
Spam emails and only 100 examples of normal, safe emails. The model is deployed to a
corporate office.
Questions:

(a) When deployed to the corporate office, what critical error is this model likely to make
repeatedly? (3 Marks)

(b) Relate this failure to a specific Machine Learning challenge discussed in Module 1.
(3 Marks)

Detailed Answer:

(a) The model is highly likely to suffer from an extreme rate of False Positives—it will
incorrectly flag and delete highly important, safe corporate emails, sending them to
the Spam folder.

(b) This failure is a classic example of poor Data Quality and Imbalance. Because 99%
of its training data was spam, the model effectively learned that ”almost everything
is spam.” It did not see enough diverse examples of normal emails to learn their
patterns. To fix this, the engineer must retrain the model with a balanced dataset
(e.g., 5,000 Spam and 5,000 Normal emails).

Case Study 4: Real Estate Pricing Engine

Scenario: Zillow wants to create a ”Zestimate” tool. When a user uploads a new house
listing with its Square Footage, Number of Bathrooms, and Age of the Property, the ML
tool instantly provides an estimated final selling price (e.g., $452,500).
Questions:

(a) Is this a Classification task or a Regression task? Provide a detailed technical justifi-
cation for your answer. (5 Marks)

Detailed Answer:

(a) This is strictly a Regression task. In Machine Learning, Classification is used when
the desired output is a discrete category or class (e.g., categorizing the house as
simply ”Cheap”, ”Average”, or ”Expensive”). Regression is used when the desired
output is a continuous numerical value. Since the Zestimate tool must output an
exact, continuous dollar amount (like $452,500) from an infinite range of possible
numbers, it utilizes Supervised Regression algorithms.

7
Chapter 2

Module 2: Machine Learning with


Python

2.1 Python for Machine Learning (ML)


Python is the undisputed king of Machine Learning. Why? Because it has a simple, readable syn-
tax that feels like human language, and it has a massive ecosystem of libraries specifically built
for data science. This allows developers to focus on solving ML problems rather than fighting
with complex code.

2.2 Data Preparation for ML with Python


Data preparation is the crucial first step before feeding data into any ML model. Raw data from
the real world is rarely ready for immediate analysis; it is often incomplete, inconsistent, or lacks
specific formatting. Data preparation involves conceptual steps such as:
• Data Cleaning: Identifying and correcting structural errors or removing duplicates.
• Feature Engineering: Creating new informative variables from existing data to help the
model learn better.
• Feature Scaling: Normalizing numerical data so that all features have an equal scale (e.g.,
scaling ages 0-100 and incomes $0-$100,000 down to a common 0.0 to 1.0 scale).
• Data Splitting: Dividing the dataset systematically into training and testing sets.

2.3 Data Handling with ML Python Packages


Before we can process Machine Learning algorithms, we need highly specialized tools to handle
the underlying math, structure the datasets, and create visualizations. Standard Python alone
is not fast enough. Therefore, the industry relies on a core stack of libraries, commonly referred
to as the ”Big Four” Python packages.
Each package handles a distinctly different part of the ML pipeline:

2.4 Data Treatment for ML Using Pandas


While ”Data Preparation” is the concept, ”Data Treatment” is the practical application of those
concepts using the Pandas library. We use Pandas DataFrames to physically clean and structure
the data.

8
Package Primary Use Case
NumPy Stands for Numerical Python. Used for fast mathematical operations on multi-
dimensional arrays and matrices.
Pandas The go-to library for data manipulation. It provides ’DataFrames’ (like Excel
tables in code) to easily read, filter, and modify data.
Matplotlib Used for creating static, animated, and interactive data visualizations (charts,
graphs).
Scikit-Learn The main library for classical Machine Learning algorithms (regression, classi-
fication, clustering).

Table 2.1: Core Python Packages for ML

Common Pandas Treatment Steps:


1. Handling Missing Values: Blank spaces in data cause algorithms to crash. We can ei-
ther drop rows with missing data using dropna() or fill them with the average value using
fillna().

2. Encoding Categorical Data: ML models only understand numbers. We must convert text
categories (like ”Red”, ”Green”, ”Blue”) into numerical columns using pd.get_dummies().

Listing 2.1: Basic Data Treatment with Pandas


import pandas as pd

# 1. Load Data
df = pd. read_csv ('dataset .csv ')

# 2. Treat Missing Values (Fill empty age fields with average age)
df['Age ']. fillna (df['Age ']. mean (), inplace =True)

# 3. Drop rows where 'Income ' is missing


df. dropna ( subset =[ 'Income '], inplace =True)

2.5 Classification in ML
Classification is a predictive modeling task where the goal is to predict a discrete category or class
for a given input. It is one of the most common applications of Machine Learning.
• Binary Classification: Predicting between exactly two categories (e.g., predicting if an
Email is ’Spam’ or ’Not Spam’).

• Multi-class Classification: Predicting between three or more categories (e.g., classifying


a medical scan as ’Healthy’, ’Benign Tumor’, or ’Malignant Tumor’).
Popular Algorithms: Logistic Regression, Decision Trees, Random Forest.

2.6 Cluster Analysis in ML


Cluster Analysis (or Clustering) is the task of grouping a set of objects in such a way that objects
in the same group (called a cluster) are more similar to each other than to those in other groups.
It is highly useful in market research and pattern recognition.

9
• Centroid-based Clustering: Organizes data into non-hierarchical clusters around central
points (e.g., K-Means).

• Density-based Clustering: Connects areas of high data density into clusters, ignoring
outliers (e.g., DBSCAN).

2.7 Supervised Learning for ML


As seen in Classification tasks, Supervised Learning is a broad learning paradigm where the
algorithm learns from a labeled dataset. This means every input data point comes with the
correct output (the ”answer key”).
The goal is for the algorithm to learn the mapping from inputs to outputs so it can accurately
predict outputs for new, unseen data. It encompasses two main types of problems:

1. Classification: Predicting a category.

2. Regression: Predicting a continuous numerical value (e.g., predicting the exact price of a
house).

2.8 Unsupervised Learning for ML


As seen in Clustering tasks, Unsupervised Learning is a paradigm where the algorithm learns
from unlabeled data. The system does not have predefined ”right answers” given by humans.
Its job is to explore the raw data and find hidden structures, natural groupings, or patterns
entirely on its own. It encompasses tasks like:

1. Clustering: Grouping similar data points together.

2. Dimensionality Reduction: Reducing the number of variables to simplify data while re-
taining essential information (e.g., Principal Component Analysis - PCA).

10
Module 2 Summary Report

This module explored the practical implementation of ML using Python.

• Ecosystem: Python is dominant due to powerful libraries like Pandas (data manip-
ulation), NumPy (math), and Scikit-Learn (algorithms).

• Data Prep & Treatment: Raw data must be prepared conceptually and treated prac-
tically (handling missing values, encoding text to numbers) before modeling.

• Learning Types:

– Supervised: Uses labeled data (includes Classification for categories and Regres-
sion for continuous numbers).
– Unsupervised: Uses unlabeled data to find hidden patterns (includes Clustering
to group data).

Solved Questions & Answers - Module 2


Q1: Why is Python the industry standard language for Machine Learning?
Answer: Python combines an incredibly simple, highly readable syntax with an unmatched
ecosystem of powerful open-source libraries (NumPy, Pandas, Scikit-Learn) written in C/C++ for
speed, allowing developers to focus on ML logic rather than complex coding syntax.

Q2: Contrast the primary functions of NumPy and Pandas.


Answer: NumPy handles heavy mathematical computations and multi-dimensional numerical
arrays. Pandas is built on top of NumPy and provides ”DataFrames” (similar to SQL tables or
Excel sheets), which allow for the easy importing, filtering, merging, and cleaning of structured
data.

Q3: Why is dealing with Missing Values a mandatory step in Data Treatment?
Answer: Standard ML algorithms in Scikit-Learn rely on complete mathematical matrices to
calculate gradients and distances. If a dataset contains blank spaces or NaN (Not a Number)
values, the algorithm cannot compute the equations and will throw a fatal error.

Q4: What is Categorical Encoding and why is pd.get_dummies() used?


Answer: ML algorithms only understand numbers, not text. Categorical Encoding converts text
labels (e.g., ”Male”, ”Female”) into numbers. pd.get_dummies() performs ”One-Hot Encoding”,
creating separate binary columns containing 0s and 1s for each category to prevent the model
from assuming a false numerical hierarchy.

Q5: Explain the difference between dropna() and fillna() in Pandas.


Answer: dropna() completely deletes any row or column that contains a missing value, which
can result in data loss. fillna() performs ”Imputation,” keeping the row by replacing the miss-
ing blank space with a logical value, such as the column’s statistical mean or median.

Q6: What is the absolute necessity of the train_test_split function in Scikit-Learn?


Answer: It divides the raw dataset into a training subset (usually 80%) to teach the model, and
a strictly sequestered testing subset (20%) to evaluate it. Without this split, you would evaluate
the model on the exact data it learned from, guaranteeing overfitting and providing a falsely
perfect accuracy score.

Q7: Classify the following problem: Predicting whether an applicant will default on a

11
credit card (Yes/No).
Answer: This is a Supervised Binary Classification problem. It is supervised because histori-
cal default statuses are required, and it is binary classification because the target variable has
exactly two discrete outcomes.

Q8: Briefly describe how Centroid-based Clustering (like K-Means) works.


Answer: The algorithm randomly places ’K’ number of central points (centroids) into the data
space. It assigns every data point to its nearest centroid. It then recalculates the center of those
groups and moves the centroid, repeating the process until the clusters are perfectly segregated
and stable.

Q9: What is Dimensionality Reduction and why is it categorized as Unsupervised Learn-


ing?
Answer: Dimensionality Reduction (like PCA) simplifies a dataset by mathematically compress-
ing hundreds of features down to the most important ones, reducing computational load and
noise. It is Unsupervised because it looks at the structure of the inputs independently, without
requiring target labels.

Q10: What role does Matplotlib play in the ML pipeline?


Answer: Before modeling, Matplotlib is used in Exploratory Data Analysis (EDA) to generate
scatter plots, histograms, and heatmaps. This allows engineers to visually detect outliers, data
distribution, and feature correlations that inform how the data should be treated.

12
Module 2: Exam-Style Case Studies (5-8 Marks)

Case Study 1: The Messy Medical Dataset

Scenario: You are tasked with building a breast cancer prediction model. You load a hos-
pital CSV file into a Pandas DataFrame. Upon running [Link](), you discover that out
of 50,000 patient records, 10,000 are missing the ”Tumor Size” numerical value. Further-
more, the ”Hospital Branch” column contains text strings (”North”, ”South”, ”East”).
Questions:

(a) Explain the risk of using [Link]() to handle the missing ”Tumor Size” values in
this specific scenario. What Pandas method is safer? (4 Marks)

(b) Describe the exact Pandas treatment required for the ”Hospital Branch” column be-
fore feeding the data to a Scikit-Learn algorithm. (4 Marks)

Detailed Answer:

(a) If you use [Link](), you will instantly delete 10,000 patient records (20% of your
dataset). This is a massive loss of valuable information; those 10,000 rows still con-
tain vital data like age, genetics, and cancer outcomes. A safer method is Impu-
tation using df['Tumor Size'].fillna(df['Tumor Size'].mean()), which fills the
blanks with the average size, preserving the rows.

(b) Scikit-Learn algorithms rely on matrix mathematics and cannot process text strings
like ”North”. The ”Hospital Branch” column must undergo Categorical Encod-
ing. You would use the Pandas pd.get_dummies() function to perform One-Hot En-
coding, which deletes the text column and creates three new numerical columns
(Branch_North, Branch_South, Branch_East) containing binary 0s and 1s.

Case Study 2: The Overconfident Student Model

Scenario: A university student builds a Decision Tree model to classify whether images
contain a cat or a dog. The dataset consists of 5,000 images. The student writes the
code: [Link](X, y) using all 5,000 images, and then evaluates the accuracy using
[Link](X) on those exact same 5,000 images. The model reports an incredible
99.8% accuracy. The student deploys the app, but users complain it guesses incorrectly
50% of the time.
Questions:
(a) What fundamental Data Preparation step did the student omit from their Python
pipeline? (3 Marks)

(b) Explain technically why the model reported 99.8% accuracy in the lab but failed in
the real world. (4 Marks)
Detailed Answer:
(a) The student critically omitted Data Splitting using the Scikit-Learn
train_test_split function. They should have separated the data into a train-
ing set (e.g., 4,000 images) and a testing set (1,000 images).

(b) Because the student evaluated the model on the exact same data it was trained on,
the model suffered from extreme Overfitting. A Decision Tree has high variance
and essentially memorized the pixel patterns of those specific 5,000 images. The

13
99.8% accuracy was a false positive. Because the model memorized the data rather
than learning generalized features (like ear shape or fur texture), it completely failed
when exposed to new, unseen images uploaded by users.

Case Study 3: Supermarket Algorithm Selection

Scenario: A global supermarket chain wants to optimize its physical store layouts. They
have a massive database of 10 million receipt transactions showing exactly which items
were bought together (e.g., Transaction 1: Bread, Milk, Diapers). They want to redesign
the aisles to place frequently co-purchased items next to each other. They have no labels
defining what constitutes a ”good” layout.
Questions:

(a) Should the data scientists use Supervised or Unsupervised learning to solve this
problem? Justify your answer. (3 Marks)

(b) Name a specific category of algorithm (Classification, Regression, or Clustering) that


applies here and explain its goal. (3 Marks)

Detailed Answer:

(a) They must use Unsupervised Learning. Supervised learning requires a target la-
bel (a right answer) to train on, but the receipt data is purely raw input showing
transactions; there is no target variable indicating layout success.

(b) The applicable category is Clustering (or Association Rule Learning). The goal of
the algorithm is to autonomously scan the unlabelled transactions and find hidden
structures and natural groupings based purely on statistical frequency, discovering
clusters like ”Customers who buy diapers are 80% likely to also buy baby wipes.”

14
Chapter 3

Module 3: Case Project Guidelines

As per the syllabus, students must select a case project and evaluate it using the methods
learned in Modules 1 and 2.

3.1 Steps for Project Evaluation


1. Problem Definition: Clearly state what you are trying to solve. Is it a Classification prob-
lem or a Clustering problem?
2. Data Collection: Source a relevant dataset (e.g., from Kaggle or UCI Machine Learning
Repository).
3. Data Preparation (Module 2 applied): Use Pandas to handle missing values, drop unnec-
essary columns, and encode text.
4. Model Selection: Choose an appropriate algorithm from Scikit-Learn based on your prob-
lem definition.
5. Training and Evaluation: Split your data into training and testing sets. Train your model
and evaluate its accuracy using metrics like the Confusion Matrix.

3.2 Example Case Project: Heart Disease Prediction


To understand how these steps come together, let’s look at a hypothetical case project outline:
• 1. Problem: Predict whether a patient has heart disease based on their medical metrics.
This is a Binary Classification problem.
• 2. Data: We download the ”UCI Heart Disease” dataset containing columns like Age, Blood
Pressure, Cholesterol, and a target column ”Target” (1 = Disease, 0 = No Disease).
• 3. Preparation: Using Pandas, we check [Link]().sum(). We find some missing Choles-
terol values and replace them with the median using fillna(). We encode the ”Gender”
text column into 0s and 1s.
• 4. Model: We select a RandomForestClassifier from Scikit-Learn because it handles med-
ical data robustly.
• 5. Evaluation: We use train_test_split, fit the model on 80% of the data, and test on
the remaining 20%. We evaluate its performance using an Accuracy Score and a Confusion
Matrix to track false negatives.

15
Module 3 Summary Report

This module synthesizes the theoretical foundations (Module 1) and Python practical skills
(Module 2) into a cohesive project workflow.

• The Pipeline: A successful project requires a strict, sequential pipeline: Define →


Collect → Prepare → Model → Evaluate.

• Evaluation Focus: The true success of an ML project isn’t just writing the Python
code; it’s proving that the model generalizes well to new data through rigorous train-
ing/testing evaluation and appropriate metric selection.

Solved Questions & Answers - Module 3


Q1: Why is ”Problem Definition” the most critical first step of any ML project?
Answer: Defining the problem dictates every subsequent step. If you do not explicitly define
whether you are predicting a category (Classification), a continuous number (Regression), or
looking for hidden groups (Clustering), you cannot choose the correct dataset, the right Pandas
treatment, or the correct Scikit-Learn algorithm.

Q2: Name two reputable sources for finding Data Collections for academic ML projects.
Answer: 1) Kaggle (a data science community platform with thousands of CSVs). 2) The UCI
Machine Learning Repository (a classic academic repository containing standard datasets like
the Iris or Heart Disease datasets).

Q3: During Model Selection, why might a Data Scientist choose a simple Logistic Re-
gression over a complex Deep Neural Network for a medical project?
Answer: The primary reason is Interpretability. While a Neural Network might be slightly
more accurate, it is a ”Black Box.” Logistic Regression is highly interpretable; doctors can look at
the math and understand exactly *why* the algorithm diagnosed the patient with the disease.

Q4: What is a Confusion Matrix, and what phase of the pipeline uses it?
Answer: A Confusion Matrix is an evaluation tool used in the final ”Training and Evaluation”
phase. It is a 2x2 grid that breaks down classification predictions into True Positives, True Neg-
atives, False Positives, and False Negatives, giving a much deeper insight into model perfor-
mance than raw accuracy.

Q5: Explain the danger of skipping Step 3 (Data Preparation) and moving directly from
Data Collection to Model Selection.
Answer: Skipping preparation results in fatal Python errors. Raw datasets almost always con-
tain NaN values and text strings. If these are not cleaned and encoded using Pandas, the Scikit-
Learn algorithms (which require pure numerical matrices) will crash immediately upon calling
the fit() function.

Q6: In a Binary Classification project evaluating cancer (1=Cancer, 0=Healthy), which is


more dangerous: a False Positive or a False Negative?
Answer: A False Negative is infinitely more dangerous. A False Positive (diagnosing a healthy
person with cancer) leads to unnecessary stress and further testing. A False Negative (diagnos-
ing a sick person as healthy) means the cancer goes untreated, which can be fatal.

Q7: What is the purpose of the random_state parameter when splitting data in an ML
project?

16
Answer: The train_test_split function shuffles data randomly. Setting a random_state (e.g.,
42) locks the random number generator so that the data is split the exact same way every time
the script is run. This ensures the project evaluation is reproducible.

Q8: If your project evaluation shows excellent training accuracy but terrible testing
accuracy, what adjustment should you make to the pipeline?
Answer: The model is Overfitting. You should return to the Model Selection/Training phase and
either choose a simpler algorithm, increase the amount of training data, or apply regularization
techniques to stop the model from memorizing noise.

Q9: Define ”Feature Engineering” in the context of project preparation.


Answer: Feature Engineering is the creative process of using domain knowledge to extract or
create new, more useful variables (features) from raw data. For example, if you have a ”Date
of Birth” column, engineering a new ”Current Age” column makes the data much easier for the
algorithm to interpret.

Q10: Why is ”Accuracy” sometimes a deeply misleading evaluation metric for ML projects?
Answer: In highly imbalanced datasets, accuracy is useless. If an anti-fraud project dataset has
99,000 normal transactions and 1,000 fraud cases, a broken model that just guesses ”Normal”
for everything will still score 99% Accuracy, despite failing its entire purpose of finding fraud.

17
Module 3: Exam-Style Case Studies (5-8 Marks)

Case Study 1: The Credit Card Fraud Evaluation Trap

Scenario: A fintech startup completes an ML project pipeline to detect credit card fraud.
Out of 100,000 transactions in their dataset, only 50 are fraudulent. The engineering team
trains a Logistic Regression model. During the Evaluation phase, they calculate the raw
”Accuracy Score” using Scikit-Learn, which outputs 99.95%. The CEO is thrilled and wants
to deploy it immediately.
Questions:

(a) Explain technically why the CEO should NOT be thrilled, and why 99.95% accuracy is
a highly misleading metric for this specific problem. (4 Marks)

(b) What specific evaluation tool and alternative metrics should the team use in Step 5
(Evaluation) to get a true picture of the model’s success? (3 Marks)

Detailed Answer:

(a) The CEO should not be thrilled because the dataset is extremely imbalanced (99,950
normal vs. 50 fraud). An ML model could achieve 99.95% accuracy simply by being
lazy and guessing ”Not Fraud” for every single transaction it ever sees. While tech-
nically accurate, the model is completely useless at its actual job of finding the 50
fraudulent transactions.

(b) Instead of raw accuracy, the team must generate a Confusion Matrix. This will
allow them to see the exact number of False Negatives (fraudulent transactions the
model missed). They should evaluate the project based on Recall (the percentage of
actual fraud cases the model successfully caught) and the F1-Score, which are built
for imbalanced data.

Case Study 2: The E-Commerce Pipeline Crash

Scenario: A student team is building a project to predict whether a website visitor will
purchase a product. They define the problem, download a raw Kaggle dataset, isolate the
features (Browser Type, Time on Site, Click Count), and import a DecisionTreeClassifier.
They immediately run [Link](X_train, y_train), but the Python execution instantly
halts with a ValueError: could not convert string to float: 'Chrome'.
Questions:
(a) Identify the exact pipeline step the team completely skipped. (2 Marks)
(b) What is causing the specific Python ValueError, and what two Pandas actions must
the team perform to fix it? (5 Marks)
Detailed Answer:
(a) The team completely skipped Step 3: Data Preparation and Treatment.
(b) The ValueError is caused because Scikit-Learn algorithms are purely mathematical;
they cannot process text strings like ”Chrome” or ”Safari” present in the ”Browser
Type” feature. To fix this, the team must use Pandas to perform Categorical Encod-
ing. Specifically, they must use pd.get_dummies(df) to convert the browser strings
into binary numerical columns. Additionally, they must ensure they use [Link]()
or [Link]() to handle any missing values in the raw dataset before passing it to
the model.

18
Appendix A

Appendix: Comprehensive Reference

A.1 Commands Table & Functions

Library Command Syntax Description / Function

Pandas pd.read_csv('[Link]') Reads a CSV file into a DataFrame.


Pandas [Link]() Displays the first 5 rows of the dataset.
Pandas [Link]() Generates summary statistics (mean,
min, max) of columns.
Pandas [Link]() Shows column data types and non-
null counts.
Pandas [Link]().sum() Counts the number of missing values
in each column.
Pandas [Link]() Removes any row that contains miss-
ing data.
Pandas pd.get_dummies(df['Col']) Converts categorical text into numeri-
cal dummy variables.
Scikit-Learn train_test_split(X, y) Splits data into random train and test
subsets.
Scikit-Learn [Link](X_train, y_train) Trains the machine learning model on
the data.
Scikit-Learn [Link](X_test) Uses the trained model to make pre-
dictions.
Scikit-Learn accuracy_score(y_test, preds) Calculates the accuracy percentage of
classification models.
Matplotlib [Link](x, y) Creates a scatter plot to visualize data
relationships.

Table A.1: Essential Python ML Commands

19
A.2 Program Syntax Templates
1. Standard ML Pipeline Syntax:
# 1. Import libraries
import pandas as pd
from sklearn . model_selection import train_test_split
from sklearn . algorithm_name import AlgorithmClass

# 2. Load and Prepare Data


df = pd. read_csv ('[Link] ')
X = df[[ 'Feature1 ', 'Feature2 ']] # Inputs
y = df['Target '] # Output

# 3. Split Data
X_train , X_test , y_train , y_test = train_test_split (X, y, test_size =0.2)

# 4. Initialize and Train Model


model = AlgorithmClass ()
[Link](X_train , y_train )

# 5. Predict
predictions = model. predict ( X_test )

2. Model Evaluation Syntax:


from sklearn . metrics import accuracy_score , confusion_matrix

# Compare actual answers ( y_test ) to model 's guesses ( predictions )


acc = accuracy_score (y_test , predictions )
print (" Accuracy :", acc * 100, "%")

# View false positives / negatives


cm = confusion_matrix (y_test , predictions )
print (" Confusion Matrix :\n", cm)

20
A.3 Example Programs (Solved)
Example 1: Classification using Decision Tree Goal: Predict if a student passes based on hours
studied.
import pandas as pd
from sklearn .tree import DecisionTreeClassifier

# Mock Data setup


data = {'Hours_Studied ': [2, 3, 5, 8, 9],
'Pass ': [0, 0, 1, 1, 1]} # 0=Fail , 1= Pass
df = pd. DataFrame (data)

# Features (X) and Target (y)


X = df[[ 'Hours_Studied ']]
y = df['Pass ']

# Train Model
classifier = DecisionTreeClassifier ()
classifier .fit(X, y)

# Predict for a student who studied 6 hours


prediction = classifier . predict ([[6]])
print (" Prediction (1= Pass , 0= Fail):", prediction [0])

Example 2: Unsupervised Clustering using K-Means Goal: Group customers based on age
and spending score.
import pandas as pd
from sklearn . cluster import KMeans

# Mock Data
data = {'Age ': [25, 26, 60, 65],
'Spending_Score ': [80, 85, 20, 15]}
df = pd. DataFrame (data)

# We only have inputs (X), no labels (y)


X = df[[ 'Age ', 'Spending_Score ']]

# Create 2 clusters
kmeans = KMeans ( n_clusters =2, random_state =0)
kmeans .fit(X)

# See which group each customer belongs to


print (" Cluster Assignments :", kmeans . labels_ )

Example 3: Linear Regression (Predicting continuous values) Goal: Predict house price
based on square footage.
import pandas as pd
from sklearn . linear_model import LinearRegression

# Mock Data
data = {'SqFt ': [1000 , 1500 , 2000 , 2500] ,
'Price ': [150000 , 220000 , 310000 , 400000]}
df = pd. DataFrame (data)

21
X = df[[ 'SqFt ']]
y = df['Price ']

# Train Model
regressor = LinearRegression ()
regressor .fit(X, y)

# Predict price for a 1800 sqft house


predicted_price = regressor . predict ([[1800]])
print (" Estimated Price : $", round ( predicted_price [0], 2))

22
Example 4: Logistic Regression (Binary Classification) Goal: Predict if a customer buys a
product based on Age and Salary.
import pandas as pd
from sklearn . linear_model import LogisticRegression

# Mock Data
data = {'Age ': [22, 25, 47, 52, 46],
'Salary ': [20000 , 30000 , 80000 , 90000 , 85000] ,
'Purchased ': [0, 0, 1, 1, 1]} # 0 = No , 1 = Yes
df = pd. DataFrame (data)

X = df[[ 'Age ', 'Salary ']]


y = df['Purchased ']

# Train Model
model = LogisticRegression ()
[Link](X, y)

# Predict for a 30 year old earning 40000


pred = model. predict ([[30 , 40000]])
print (" Purchased ? (1= Yes , 0=No):", pred [0])

Example 5: Full Evaluation with Accuracy Score Goal: Train and check the accuracy of a
Random Forest.
import pandas as pd
from sklearn . ensemble import RandomForestClassifier
from sklearn . model_selection import train_test_split
from sklearn . metrics import accuracy_score

# Mock Data
data = {'Feature1 ': [1, 2, 3, 4, 5, 6, 7, 8],
'Feature2 ': [10, 20, 10, 20, 10, 20, 10, 20],
'Target ': [0, 0, 0, 0, 1, 1, 1, 1]}
df = pd. DataFrame (data)

X = df[[ 'Feature1 ', 'Feature2 ']]


y = df['Target ']

# Split data 75% train , 25% test


X_train , X_test , y_train , y_test = train_test_split (X, y, test_size =0.25 ,
random_state =42)

model = RandomForestClassifier ( random_state =42)


[Link](X_train , y_train )

# Check Accuracy
preds = model. predict ( X_test )
accuracy = accuracy_score (y_test , preds)
print (" Model Accuracy :", accuracy * 100, "%")

Example 6: Data Cleaning Pipeline Goal: Clean missing data and encode text to numbers.
import pandas as pd
import numpy as np

23
# Mock Messy Data
data = {'Name ': ['Alice ', 'Bob ', 'Charlie ', 'David '],
'Age ': [25, [Link] , 35, 40],
'City ': ['NY', 'LA', 'NY ', 'SF ']}
df = pd. DataFrame (data)
print (" --- Raw Data ---\n", df)

# 1. Fill missing age with the mean


df['Age ']. fillna (df['Age ']. mean (), inplace =True)

# 2. Encode the 'City ' text column to numbers


df_encoded = pd. get_dummies (df , columns =[ 'City '])

print ("\n--- Cleaned & Encoded Data ---\n", df_encoded )

24
A.4 Unsolved Case Studies for Practice (10)

Practice Case Study 1: Missing Data Imputation

Problem Statement: You are a data analyst preparing a dataset for an HR prediction
model. You load a dataset named [Link] into a Pandas DataFrame df. Upon
inspection, you realize the ’Salary’ column has several blank entries that will crash your
model.
Task: Write the exact Python/Pandas code required to find all missing values in the ’Salary’
column and replace them with the median salary of the dataset, ensuring the changes are
saved to the DataFrame permanently.

Practice Case Study 2: Logistic Regression Initialization

Problem Statement: A hospital wants to deploy a simple diagnostic tool. You are pro-
vided with a clean DataFrame df containing patient features ['Age', 'Weight'] and a
binary target column ['Diabetes'] (where 1 indicates disease, 0 indicates healthy).
Task: Write the complete Scikit-Learn code to isolate the features and target, initialize a
LogisticRegression model, fit it to the data, and predict the diabetes status for a new
patient who is 45 years old and weighs 80kg.

Practice Case Study 3: The Train/Test Split Pipeline

Problem Statement: To prevent overfitting, standard ML practice requires segregating


data. You have loaded [Link]. The input features are ['Bedrooms', 'Bathrooms',
'Area'], and the target to predict is ['Price'].
Task: Write a Python script to isolate X and y, and utilize the train_test_split function
to divide the data into a 70% training set and a 30% testing set. Set the random_state to
42 for reproducibility.

Practice Case Study 4: Unsupervised Customer Grouping

Problem Statement: A retail mall has provided you with a dataset [Link] tracking
’Annual_Income’ and ’Spending_Score’. Because there are no predefined categories, you
must use unsupervised learning to find natural marketing segments.
Task: Write the code to extract the features, initialize the KMeans clustering algorithm to
create exactly 4 customer clusters, fit the algorithm to the data, and print the assigned
cluster labels for each customer.

Practice Case Study 5: Random Forest Classification

Problem Statement: You are building a complex spam filter. You have a clean dataset
[Link]. The input features are ['WordCount', 'LinkCount'] and the target label is
['IsSpam']. You want to use an ensemble decision tree method for higher accuracy.
Task: Write the code to import, initialize, and train a RandomForestClassifier on the pro-
vided data.

25
Practice Case Study 6: Aggressive Row Deletion

Problem Statement: You load medical_scans.csv into a Pandas DataFrame df. The
dataset has thousands of rows, but some imaging files were corrupted, resulting in NaN
values across various columns. Imputing medical data is deemed too risky for this spe-
cific project.
Task: Write the single Pandas command required to aggressively delete every single row
in the entire DataFrame that contains at least one missing (NaN) value, updating the
DataFrame in place.

Practice Case Study 7: Categorical ’Dummy’ Encoding

Problem Statement: You are analyzing a web traffic DataFrame df. It contains a text
column called DeviceType with categorical string values like ”Mobile”, ”Desktop”, and
”Tablet”. Scikit-Learn throws an error when trying to process these strings.
Task: Write the Pandas code to perform One-Hot Encoding, converting the DeviceType
text column into distinct numerical dummy variables.

Practice Case Study 8: Decision Tree Inference

Problem Statement: You are working with a pre-split financial dataset with features X
(CreditScore, Debt) and target y (LoanApproved).
Task: Write the Python syntax to import a DecisionTreeClassifier, train it on the existing
X and y variables, and make a prediction for a new applicant with a CreditScore of 700 and
Debt of 5000. Print the result.

Practice Case Study 9: Initial Data Inspection

Problem Statement: As a data scientist, the very first step upon loading a new dataset
[Link] into a DataFrame df is to understand its structure and cleanliness.
Task: Write the two Pandas commands you would execute sequentially to: 1) display the
first 5 rows of the dataset to verify column headers, and 2) print a sum count of all missing
values present in every column.

Practice Case Study 10: Model Accuracy Evaluation

Problem Statement: You have successfully navigated the ML pipeline. You trained
a model on X_train and generated an array of guesses called preds by running
[Link](X_test). You now possess the model’s guesses (preds) and the actual true
answers (y_test).
Task: Write the code to import the standard classification evaluation metric from Scikit-
Learn, compare your preds against y_test, and print the final accuracy percentage of your
model.

26
A.5 Answer Key for Unsolved Case Studies
Answer Key 1: Missing Data Imputation
import pandas as pd
df = pd. read_csv ('employees .csv ')
# Calculate median and fill
median_sal = df['Salary ']. median ()
df['Salary ']. fillna (median_sal , inplace =True)

Answer Key 2: Logistic Regression Initialization


from sklearn . linear_model import LogisticRegression

X = df[[ 'Age ', 'Weight ']]


y = df['Diabetes ']

model = LogisticRegression ()
[Link](X, y)
prediction = model. predict ([[45 , 80]])
print ( prediction )

Answer Key 3: The Train/Test Split Pipeline


import pandas as pd
from sklearn . model_selection import train_test_split

df = pd. read_csv ('housing .csv ')


X = df[[ 'Bedrooms ', 'Bathrooms ', 'Area ']]
y = df['Price ']

X_train , X_test , y_train , y_test = train_test_split (X, y, test_size =0.3 ,


random_state =42)

Answer Key 4: Unsupervised Customer Grouping


import pandas as pd
from sklearn . cluster import KMeans

df = pd. read_csv ('customers .csv ')


X = df[[ 'Annual_Income ', 'Spending_Score ']]

model = KMeans ( n_clusters =4, random_state =42)


[Link](X)
print (model. labels_ )

Answer Key 5: Spam Detection Forest


import pandas as pd
from sklearn . ensemble import RandomForestClassifier

df = pd. read_csv ('emails .csv ')


X = df[[ 'WordCount ', 'LinkCount ']]
y = df['IsSpam ']

model = RandomForestClassifier ( random_state =42)


[Link](X, y)

Answer Key 6: Aggressive Row Deletion


# Drops any row with NaN and saves it permanently to df
df. dropna ( inplace =True)

27
Answer Key 7: Categorical ’Dummy’ Encoding
import pandas as pd

# Converts text to binary columns


df_encoded = pd. get_dummies (df , columns =[ 'DeviceType '])

Answer Key 8: Decision Tree Inference


from sklearn .tree import DecisionTreeClassifier

model = DecisionTreeClassifier ( random_state =42)


[Link](X, y)

new_applicant = [[700 , 5000]]


pred = model. predict ( new_applicant )
print (" Decision :", pred)

Answer Key 9: Initial Data Inspection


# View first 5 rows
print ([Link] ())

# View count of missing data per column


print (df. isnull ().sum ())

Answer Key 10: Model Accuracy Evaluation


from sklearn . metrics import accuracy_score

# Compare true answers to model predictions


acc = accuracy_score (y_test , preds)
print (" Model Accuracy :", acc * 100, "%")

28

You might also like