ADIKAVI NANNAYA UNIVERSITY
V SEMESTER: BCA Honors Data Science (Major)
(for 2023-2024 admitted batch)
Course 14 B: Supervised Machine Learning with Python
UNIT I
Machine Learning Basics: What is machine learning? Key terminology, Key tasks of machine
learning, How to choose right algorithm, steps in developing a machine learning, why
python?
Getting started with NumPy library Classifying with k- Nearest Neighbors: The k-Nearest
Neighbors classification algorithm, Parsing and importing data from a text file, Creating
scatter plots with Matplotlib, Normalizing numeric values.
UNIT II
Splitting datasets one feature at a time-Decision trees: Introducing decision trees,
measuring consistency in a dataset, using recursion to construct a decision tree, plotting
trees in Matplotlib.
UNIT III
Classifying with probability theory-Naive Bayes: Using probability distributions for
classification, learning the naive Bayes classifier, Parsing data from RSS feeds, using naive
Bayes to reveal regional attitudes
UNIT IV
Logistic regression: Classification with logistic regression and the sigmoid function. Using
optimization to find the best regression coefficients, the gradient descent optimization
algorithm, Dealing with missing values in the our data
UNIT V
Support vector machines: Introducing support vector machines, using the SMO algorithm
for optimization, using kernels to "transform" data, Comparing support vector machines
with other classifiers.
1|Page
UNIT I
Machine Learning Basics
Machine Learning Basics: What is machine learning? Key terminology, Key tasks of machine
learning, How to choose right algorithm, steps in developing a machine learning, why
python?
Getting started with NumPy library Classifying with k- Nearest Neighbors: The k-Nearest
Neighbors classification algorithm, Parsing and importing data from a text file, Creating
scatter plots with Matplotlib, Normalizing numeric values.
1) What is Machine Learning? Explain the need of Machine Learning?
Machine Learning:
Machine learning is a subfield of artificial intelligence (AI) that enables
computer systems to autonomously learn and improve from data. Instead of relying
on explicitly hard-coded rules or traditional programming, it uses algorithms to
identify patterns, make decisions, and improve predictions over time. In simple
words, ML teaches systems to think and understand like humans by learning from
the data.
Need for Machine Learning
Machine Learning is important because traditional programming cannot
handle complex tasks or large amounts of data efficiently. ML overcomes by learning
from data and making predictions (estimates) without fixed rules. It is needed for
the following reasons:
1. Solving Complex Business Problems
Traditional programming struggles with tasks like language understanding and
medical diagnosis. ML learns from data and predicts outcomes easily.
Examples:
Image and speech recognition in healthcare.
Language translation and sentiment analysis.
2. Handling Large Volumes of Data
The internet generates huge amounts of data every day. Machine Learning
processes and analyzes this data quickly by providing valuable insights and real time
predictions.
Examples:
Fraud detection in financial transactions.
Personalized feed recommendations on Facebook and Instagram from billions
of interactions.
2|Page
3. Automate Repetitive Tasks
ML automates time consuming, repetitive tasks with high accuracy hence
reducing manual work and errors.
Examples:
Gmail filtering spam emails automatically.
Chatbots handling order tracking and password resets.
4. Personalized User Experience
ML enhances user experience by modifying recommendations to individual
preferences. It analyze user behavior to deliver highly related content.
Examples:
Netflix suggesting movies and TV shows based on our viewing history.
E-commerce sites recommending products we're likely to buy.
5. Self-Improvement in Performance
ML models change and improve with more data helps in making them smarter
over time. They adjust to user behavior and increase their performance.
Examples:
Voice assistants like Siri and Alexa learning our preferences.
Search engines filtering results based on user interaction.
2) Explain briefly how machines learn from data?
Machines Learn from Data
A machine learns by finding patterns in data and improving over time without
explicit programming. It adjusts with experience to make more accurate predictions.
This learning happens through the following steps:
Data Input: Machine needs data like text, images or numbers to analyze. Good
quality and enough quantity of data are important for effective learning.
Algorithms: Algorithms are mathematical methods that help the machine find
patterns in data. Different algorithms help different tasks such as classification or
return.
Model Training: During training, the machine adjusts its internal settings to better
predict outcomes. It learns by reducing the difference between its predictions and
actual results.
Feedback Loop: Machine compares its predictions with true outcomes and uses this
feedback to correct errors. Techniques like gradient descent help it update and
improve.
3|Page
Experience and Iteration: Machine repeats training many times with data helps in
refining its predictions with each pass, more data and iterations improve accuracy.
Evaluation and Generalization: Tested on new data to ensure real world performance
Data is the foundation of machine learning because models learn patterns and
make predictions from it. Good quality and diverse data help improve accuracy,
performance and real-world results.
3) Explain various key terminologies in Machine Learning?
Key Terminology in Machine Learning
Machine Learning has become an essential part of modern technology. As the
field grows rapidly, it’s essential to knowledge the foundational terms and concepts
in machine learning systems.
Data Terminology
1) Dataset: The complete collection of data points used for analysis and training.
2) Features: The individual, measurable variables or inputs used by the model to
make predictions (e.g., house size, age).
3) Label / Target: The final output or category you want the model to predict
(e.g., house price, spam/not spam).
Core Concepts & System Types
1) Algorithm: The mathematical rules or step-by-step procedures used to map
out patterns in data.
2) Model: The final mathematical representation generated after an algorithm
trains on a specific dataset.
The Training Process
1) Training: The process of feeding data into an algorithm so it can adjust
internal values and learn patterns.
2) Inference: Using a fully trained model to make predictions on brand new,
unseen data points.
3) Parameters: The internal values the model learns and fine-tunes automatically
during training to match the data.
Performance & Errors
1) Overfitting: When a model learns training data too well (including random
noise), causing it to fail on new data.
2) Under fitting: When a model is too simple to capture the underlying patterns,
resulting in poor performance everywhere.
3) Bias-Variance Trade-off: The balance between a model being too strict vs. too
sensitive.
4) Accuracy: The basic percentage of correct predictions out of the total test
examples.
4|Page
4) Write about key tasks / types of Machine Learning?
Types of Machine Learning
In machine learning, a task refers to the specific problem being solved,
categorized by the type of prediction or data processing required. The three core
tasks are Classification (predicting labels), Regression (predicting numbers), and
Clustering (grouping data).
Supervised learning
Supervised learning is a type of machine learning where a model is trained
using labeled data, meaning every training input is paired with its correct,
corresponding output. The algorithm learns a mapping function from the inputs to
the outputs, allowing it to accurately predict results or classify completely new,
unseen data.
Unsupervised learning
Unsupervised learning is a machine learning approach where algorithms
analyze unlabeled raw data to discover hidden patterns, structures, or groupings
without human intervention. Unlike supervised learning, it has no predefined
answers, forcing the model to independently interpret the underlying data.
5|Page
Reinforcement learning
Reinforcement Learning (RL) is a machine learning paradigm where an
autonomous "agent" learns to make optimal decisions through trial and error to
maximize cumulative rewards. Unlike supervised learning, it requires no labeled
datasets; instead, the agent interacts with its environment, taking actions and
receiving immediate positive or negative feedback.
5) Explain the steps in developing a Machine Learning application with
Python?
Steps in developing a Machine Learning
Developing a machine learning model follows a structured workflow known as
the Machine Learning Lifecycle. This iterative process balances strategic problem-
solving with technical data manipulation to create predictive systems.
The critical sequence of steps required to build an effective machine learning
system includes:
1. Define the Problem
Clearly understand what the model is supposed to predict or classify, ensuring
the ML task matches the actual objective.
Identify the business goal or research purpose behind building the model.
Decide whether the task is regression, classification, clustering or forecasting.
Determine the type of data required and the expected output format.
2. Collect Data
Collect all relevant and high quality data from reliable sources, as the dataset
forms the foundation of any machine learning model.
Gather data from databases, sensors, surveys, web APIs or public datasets.
Ensure the dataset is large, clean and representative of the real problem.
6|Page
Combine data from multiple sources if needed to improve coverage and
diversity.
Check early for issues like missing values, noise and imbalance to avoid
problems later.
3. Data Cleaning and Preprocessing
Prepare the raw data for modeling by fixing errors, handling missing values
and transforming it into a clean and usable form.
Remove duplicates and correct inconsistent or incorrect entries.
Handle missing values using deletion or interpolation techniques.
Normalize or scale numerical features for stable and faster model training.
4. Feature Engineering & Selection
Select the most relevant features and create new meaningful ones to enhance
model accuracy, reduce complexity and improve learning.
Remove irrelevant, redundant or noisy features that harm performance.
Use techniques like Correlation Analysis, Chi-square, ANOVA, PCA, and Mutual
Information.
Good feature engineering often improves performance more than switching
algorithms.
5. Split Data into Training and Testing Sets
Divide the dataset so the model learns from one part and is evaluated fairly on
unseen data.
Ensures the model is evaluated on unseen data, preventing overfitting.
Stratified splits are used for classification to maintain class balance.
Optionally create a validation set or use cross validation for better tuning.
6. Model Selection & Architecture Design
Pick algorithms suited to your data, like Random Forests or Neural Networks.
Evaluate tradeoffs between computational speed and prediction accuracy.
Define the mathematical loss function to measure error rates.
7. Model Training
Feed the training partition into the selected ML algorithm.
Allow the model to iteratively learn patterns and relationships.
Monitor learning paths to minimize error metrics dynamically.
7|Page
8. Evaluation & Hyper parameter Tuning
Check performance against unseen test data using metrics like F1-score or
accuracy.
Optimize hyper parameter configurations via techniques like Grid Search or
Random Search.
Tweak parameters carefully to prevent systemic over fitting or under fitting.
6) Why Python is so important for Machine Learning? Explain its
features?
Python:
Python is the preferred language for machine learning due to its simplicity and
readability, which make it accessible for beginners and efficient for experienced
programmers. The language’s clear and natural syntax allows developers to write
and debug code quickly, reducing development time and the learning curve.
Additionally, Python’s extensive ecosystem of libraries and frameworks, such as
NumPy, Pandas, Scikit-learn, TensorFlow and PyTorch provides robust tools for data
manipulation, statistical analysis, and the implementation of complex machine
learning algorithms.
Python’s ability to integrate seamlessly with other languages and
technologies, combined with its strong support for data visualization through
libraries like Matplotlib, Seaborn and Plotly further enhances its appeal.
Features of Python:
1) Ease of Learning and Use:
Python is widely considered one of the easiest programming languages to
learn and use because its design orders clean, human-readable code that closely
8|Page
mirrors natural English. By eliminating the complex syntax typical of older languages
like C++ or Java, Python allows developers to focus on writing programmatic logic
rather than managing syntax rules.
2) Extensive Libraries and Framework:
Python boasts a rich set of libraries and frameworks that simplify the
implementation of machine learning algorithms and data processing. Key libraries
include:
NumPy (Numerical Python): It is a foundational Python library for scientific
computing, data science, and machine learning. Its primary feature is the
ndarray, which allows you to efficiently store and manipulate large datasets.
Pandas: It is an open-source Python library used primarily for fast, flexible
data manipulation, cleaning, and analysis.
Scikit-learn (sklearn): It is an open-source Python library designed for machine
learning, data analysis, and predictive modeling.
TensorFlow and Keras: It work together as a powerful deep learning
ecosystem in Python, where TensorFlow serves as the high-performance
infrastructure engine and Keras acts as the user-friendly, high-level interface.
PyTorch: It is an open-source deep learning framework for Python primarily
used to build, train, and deploy artificial intelligence models like neural
networks.
3) Large Community Support
Python has a large and active community that provides extensive support
through forums, tutorials, and discussion platforms such as Stack Overflow. This
strong community ecosystem makes it easier to find solutions, learn best practices,
and resolve issues quickly.
4) Integration and Flexibility:
Python seamlessly integrates with other languages and technologies, making
it highly flexible for different stages of machine learning workflows. It can be used
for data collection (e.g., web scraping with BeautifulSoup), preprocessing (e.g., data
cleaning with Pandas), model building (e.g., Scikit-learn or TensorFlow).
5) Support for Visualization:
Data visualization is crucial in machine learning for understanding data
patterns and presenting results. Python excels in this area with libraries such as
Matplotlib, Seaborn and Plotly, which allow users to create a wide range of static and
interactive visualizations to explore data.
9|Page
7) Describe the K-Nearest Neighbors classification algorithm with an
example?
K-Nearest Neighbors (KNN) algorithm:
The K-Nearest Neighbors (KNN) algorithm is a simple, non-parametric,
supervised machine learning technique used for classification and regression tasks. It
operates on the core principle of nearness, working off the fundamental assumption
that similar data points naturally cluster close to one another in a linear space.
How the KNN Algorithm Works
KNN is often called a lazy learner because it bypasses an explicit training
phase. Instead of building an internal mathematical model, it simply stores the
entire training dataset in memory. The actual computation triggers only when a
new, unclassified query data point is introduced.
The algorithm executes the following sequence:
Step 1: Set K — choose an integer value for K, which commands how many
neighboring data points to evaluate.
Step 2: Distance calculation — Measure the distance between the new query point
and every single point in the stored dataset.
Step 3: Distance sorting — Sort the calculated distances in ascending order to
identify the K closest instances.
Step 4: Majority vote — Count the class labels among those top K neighbors. The
algorithm assigns the query point to whichever class has the highest frequency.
Distance Metrics
To determine proximity, KNN relies on geometric formulas. The most
common standard metric is Euclidean distance (straight-line distance), calculated
between two points (x1, y1) and (x2, y2) as:
Step-by-Step Practical Example
Imagine a clothing manufacturer classifying a new customer's T-Shirt Size
(either Small or Large) based on two numerical traits: Weight (kg) and Height (cm).
1. The Dataset
The company looks at its existing database of 5 categorized customers:
10 | P a g e
2. The New Query
A new customer arrives with a Weight of 62 kg and a Height of 167 cm. We
want to predict their T-Shirt size using K = 3.
3. Distance Calculations
We apply the Euclidean distance formula between the new customer (62, 167)
and all existing records:
4. Sorting & Selecting Neighbors
Sorting the distances from lowest to highest shows the top 3 closest entries:
Customer B (Distance: 2.83) Small
Customer E (Distance: 6.71) Large
Customer A (Distance: 9.90) Small
5. Majority Voting
Among the 3 closest neighbors, we tally the sizes:
Small: 2 votes (Customer B, Customer A)
Large: 1 vote (Customer E)
Because Small wins the majority vote, the algorithm predicts that the new
customer's optimal T-Shirt size is Small.
11 | P a g e
8) Explain Parsing and importing data from a text file in Python?
Parsing & importing data from a Text File:
Parsing and importing data from a text file into a Python program is a
foundational task in data preprocessing, particularly crucial for preparing data for
analysis or machine learning models. This process typically involves reading the raw
text data, parsing it to extract meaningful information, and then converting it into a
structured format such as a Pandas DataFrame. For example, consider a text file
containing comma-separated values (CSV). Using Python’s built-in file handling
capabilities, you can read the file line by line, and then use string operations to split
each line into individual data elements. This parsed data can then be organized into
a list of lists, where each sub list represents a row of data. This structure is essential
for further manipulation and ensures that the data is clean and ready for analysis.
Once the data is parsed, the next step is to import it into a Pandas DataFrame,
a powerful data structure that provides extensive functionality for data manipulation
and analysis. Using Pandas, you can quickly inspect the data for any inconsistencies,
perform statistical analysis, or prepare the data for machine learning algorithms.
Step-by-step Guide:
1. Reading data from a Text File
2. Parsing the Data
3. Loading the Data into a DataFrame
12 | P a g e
Example Scenario:
To create a text file named “stut_marks.txt” with the following content:
M1,M2,M3,M4,Name
63,52,60,45,Kiran
52,63,41,52,Vishnu
32,52,41,65,Raju
Step 1: Reading Data from a Text File
First, we need to read the data form the text file. This can be done using
Python’s built-in file handling functions.
#Open the file in read mode
with open (stu_marks.txt”,”r”) as file:
#read the content of the file
data=[Link]()
#prints the raw data
print(data)
Step 2: Parsing the Data
Next, we parse the raw data into a structured format. Since the data is I CSV
format, we can split each line by commas.
#initialze a list to hold the parsed data
parsed_data=[ ]
#Loop through each line in the raw data
for line in data:
parsed_data .append([Link]().split(“,”))
#print the parsed data
print (parsed_data)
Step 3: Loading the Data into a DataFrame
Using the parsed data, we can create a DataFrame using the Pandas library for
easier data manipulation and analysis.
import pandas as pd
#convert the parsed data into a data frame
Df=[Link](parsed_data[1:],columns=parsed_data[0])
#print the DataFrame
print(df)
13 | P a g e
9) Explain how to creating Scatter Plots with Matplotlib?
Creating Scatter Plots with Matplotlib:
Creating scatter plots with Matplotlib is a fundamental technique for
visualizing the relationship between two numerical variables. Matplotlib, a widely-
used plotting library in Python, provides an easy-to-use scatter function that allows
you to create detailed and customizable scatter plots. The process begins with
importing the necessary libraries, such as Matplotlib for plotting and Numpy for
generating or handling data. First we prepare your data, which typically involves
creating arrays of values for the x and y coordinates. By calling the [Link]()
function with these arrays, you can generate a basic scatter plot. Adding labels to
the axes and a title using [Link](), [Link]() and [Link]() enhances the plot’s
readability and provides context for the data being visualized. Finally, displaying the
plot with [Link]() renders the scatter plot, making it easy to interpret the data’s
underlying patterns and relationship.
Step-by-Step Guide:
Install Matplotlib
Import Required Libraries
Prepare data
Create scatter Plot
Customize the Plot
Show the Plot
To use a simple dataset to show how to create scatter plots.
Step 1: Install Matplotlib
Not installed Matplotlib in computer, install using pip command:
pip install Matplotlib
step 2: Import Required Libraries
Import the necessary libraries for creating scatter plots.
import [Link] as plt
import numpy as np
step 3: Prepare Data
Prepare the data that you want to plot. For this example, we will create some
random data points.
#Generate random data
[Link](0)
x=[Link](50)
y=[Link](50)
14 | P a g e
print(“X Data:”,x)
print(“Y Data:”,y)
Step 4: Create Scatter Plot
Use Matplotlib’s scatter function to create a scatter plot.
#create a scatter plot
[Link](x,y)
#add labels and titles
[Link](“X-axis Label”)
[Link](“Y-axis Label”)
[Link](“Scatter Plot Example”)
#Show the plot
[Link]()
Step 5: Customize the plot
You can customize the scatter plot by changing colors, sizes and adding more
details.
#customize the scatter plot
[Link](x,y,c=”blue”,marker=’o’,s=100,alpha=0.5, edgecolors=’w’)
#add labels and titles
[Link](“X-axis Label”)
[Link](“Y-axis Label”)
[Link](“Customize Scatter Plot Example”)
#Show the plot
[Link]()
15 | P a g e
10) Write a short note on normalization of numeric values.
Normalizing Numerical Values:
Normalizing numerical values is a critical preprocessing step in data analysis
and machine learning that involves scaling the values in a dataset to a standard
range, typically [0,1] or [-1,1]. This process ensures that each feature contributes
equally to the model’s performance and prevents features with larger magnitudes
from dominating those with smaller magnitudes. Normalization ensures that
features with different scales or units contribute equally to the model and improves
the performance of many machine learning algorithms.
There are several methods for normalizing data, but two commonly used
approaches involve manual normalizations with NumPy and automated scaling with
Scikit-learns MinMaxScaler. Using NumPy, normalization can be achieved by
calculating the minimum and maximum values of each feature and applying the
formula (data-data_min) / (data_max – data_min). Alternaively, Scikit-learn’s
MinMaxScaler offers a more streamlined approach, allowing users to fit and
transform the data in a single step.
Key Features of Normalization:
Maps the minimum and maximum of a feature to a defined range.
Preserves the relative relationships of the original data.
Useful for algorithms that rely on distance metrics such as k-Nearest
Neighbours.
Using Numpy:
import numpy as np
data=[Link]([[1,2],[3,4],[5,6],[7,8],[9,10]])
data_min=[Link](data)
data_max=[Link](data)
nor_data=(data - data_min) / (data_max - data_min)
print(f"Original Data:{data}")
print(f"Normalized Data:{nor_data}")
Original Data:
[[ 1 2]
[ 3 4]
[ 5 6]
[ 7 8]
[ 9 10]]
16 | P a g e
Normalized Data:
[[0. 0.1]
[0.2 0.3]
[0.4 0.5]
[0.6 0.7]
[0.8 1.]]
Using Scikit-learn:
import numpy as np
from [Link] import MinMaxScaler
data=[Link]([[1,2],[3,4],[5,6],[7,8],[9,10]])
scaler=MinMaxScaler() # Initialize the scaler
nor_data=scaler.fit_transform(data)
print(f"Original Data:{data}")
print(f"Normalized Data:{nor_data}")
17 | P a g e
UNIT II
Splitting Datasets One Feature at a
Time-Decision Trees
Splitting datasets one feature at a time-Decision trees: Introducing decision trees,
measuring consistency in a dataset, using recursion to construct a decision tree, plotting
trees in Matplotlib.
1) What is Decision Tree? Explain the basic structure of Decision Tree?
Decision Tree:
A decision tree is a popular, flowchart-like machine learning algorithm used
for both classification and regression problems and decision support tool. It breaks
down complex decisions into a sequence of simple "if-then" questions, using a
hierarchy of nodes and branches. It is widely used for both data classification and
predicting numerical values.
It works like a flowchart where each internal node represents a decision based
on a feature, each branch represents the outcome of that decision, and each leaf
node represents the final prediction or output.
The algorithm learns by repeatedly splitting the dataset into smaller groups
based on the feature that best separates the data.
Structure of a Decision Tree
A Decision Tree consists of the following parts:
Root Node: The topmost point that represents the entire dataset or the main
decision problem. It is the starting point from which all other data splits.
Internal (Decision) Nodes: Intermediate points where a specific feature or
attribute is tested against a condition (e.g., is income > (50,000)? or what is
the weather?).
Branches: Lines or arrows connecting the nodes that represent the outcome
of a test or a possible choice (e.g., True, False, or Sunny, Rainy).
Leaf (Terminal) Nodes: The endpoints of the tree where no further splitting
occurs.
18 | P a g e
Working Example
Dataset:
Age Income Buy Laptop
Young High No
Young Low Yes
Old High Yes
Old Low Yes
Tree:
Age
/ \
Young Old
/ \
Income Yes
/ \
High Low
No Yes
2) Discuss the method of measuring consistency in a dataset and its
importance in decision trees?
Measuring Consistency in a Dataset:
In Machine Learning, especially in Decision Tree algorithms, the quality of a
dataset is determined by how consistent or pure the data is. Before making any split,
the Decision Tree checks how similar the records in a dataset are. This process is
called measuring consistency.
19 | P a g e
The main objective of a Decision Tree is to divide the dataset into smaller
groups that contain records of the same class. The more similar the records are, the
higher the consistency and the better the prediction accuracy.
For example, if a class contains only students who passed, then the dataset is
perfectly consistent. If it contains both pass and fail records, the dataset is less
consistent.
Importance of Measuring Consistency:
Measuring consistency is important because it helps the Decision Tree:
Select the best feature for splitting.
Reduce impurity in the data.
Improve prediction accuracy.
Build a smaller and more understandable tree.
Avoid unnecessary splits.
Improve the performance of the machine learning model.
Without measuring consistency, the Decision Tree would not know which
feature produces the best split.
Method of measuring consistency in a dataset
1) Descriptive Statistics: It can provide a quick overview of the data distribution and
help identify inconsistencies.
Mean, Median and Mode: Consistent data should have these central tendency
measures closely aligned.
Standard Deviation & Variance: Low values indicate that data points are close
to the mean, suggesting consistency.
2) Data Visualization: Visual tools help in identifying patterns, outliers and
inconsistencies.
Histograms: Show the frequency distribution of data.
Box Plots: Highlight the spread and identify outliers.
Scatter Plots: Reveal relationships and patterns between variables.
3) Correlation Analysis: Measures the relationship between different variable in a
dataset. High correlation between expected related variables indicates
consistency.
Pearson Correlation Coefficient: Measures linear correlation.
Spearman Rank Correlation: Measures monotonic relationships.
4) Data Quality Rules: Implementing rules to check for consistency in data entries.
Uniqueness: Ensuring no duplicate records.
20 | P a g e
Completeness: Checking for missing values.
5) Time-Series Analysis: For datasets with a time component, consistency can be
measured by analyzing trends and patterns over time.
Moving Averages: Smooth out short-term fluctuations.
Seasonal Decomposition: Identify and analyze seasonal patterns.
6) Machine Learning Approaches:
Anomaly Detection: Algorithms can identify outliers and inconsistent data
points.
Clustering: Group similar data points and check for consistency within clusters.
3) What is meant by recursion in decision trees?
Recursion in Decision Trees
Recursion is a problem-solving technique in which a function repeatedly calls
itself to solve smaller parts of a larger problem. In decision trees, recursion is used to
build the tree by repeatedly splitting the dataset into smaller subsets until a stopping
condition is reached.
A decision tree starts with the entire dataset at the root. It selects the best
attribute to split the data, creates branches, and then repeats the same process for
each branch. Since the same steps are performed again and again on smaller subsets
of data, this process is called recursive tree construction.
Used Recursion in Decision Trees:
Divide a large dataset into smaller datasets.
Apply the same decision-making process repeatedly.
Automatically create child nodes from parent nodes.
Continue building the tree until all decisions are complete.
Simplify complex classification or prediction problems.
Steps to Build a Decision Tree using Recursion
Define a class for the tree nodes: This will store information about the feature
to split on, the threshold value, the left and right children, and the predicted
class.
Calculate impurity measures: Commonly used impurity measures are Gini
impurity or entropy.
Split the dataset: Based on the best attribute and its value.
Recursive function to build the tree: This function will be called for each node
in the tree.
Stopping criteria: Decide when to stop splitting.
21 | P a g e
4) How to generate Plotting Trees in Matplotlib?
Plotting Trees in Matplotlib
Visualizing decision trees using Matplotlib provides a flexible and customizable
way to understand the structure and decision rules of a trained model. The
approach begins by loading a dataset, such as the Iris dataset, and splitting it into
training and testing sets. A decision tree classifier is then trained on the training
data. The visualization process involves defining a recursive plot_tree function that
plots each node and the connections between them. Decision nodes and leaf nodes
are differentiated using colors: light green for decision nodes and light blue for leaf
nodes. This function uses the attributes of the nodes, such as the feature to split on
and the beginning value, to display informative labels. The recursive nature of the
function allows for the complete tree structure to be built from the root node to the
leaf nodes, creating a clear and detailed representation of the decision-making
process of the tree.
Load Data
Split Data into Training & Testing Sets
Train Decision Tree Classifier
Define plot_tree function
Define traverse_tree function
Plot Tree using plot_tree
Display plot with Matplotlib
Purpose of Tree Plotting
The main purpose of tree plotting is to convert complex hierarchical
information into a simple graphical form. It helps users to:
Understand relationships between elements.
Analyze hierarchical structures.
Visualize algorithms.
Present data clearly.
Improve learning and understanding of tree-based concepts.
22 | P a g e
23 | P a g e
24 | P a g e