0% found this document useful (0 votes)
14 views13 pages

Decision Tree Analysis of Ionosphere Data

This assignment focuses on using decision trees for binary classification of radar data from the Ionosphere dataset, aiming to classify radar returns as 'good' or 'bad'. It involves data loading, model building using the rpart method, visualization of the decision tree, and evaluation of model accuracy through a train-test split. The assignment provides practical insights into supervised learning and the functionality of decision trees in machine learning.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views13 pages

Decision Tree Analysis of Ionosphere Data

This assignment focuses on using decision trees for binary classification of radar data from the Ionosphere dataset, aiming to classify radar returns as 'good' or 'bad'. It involves data loading, model building using the rpart method, visualization of the decision tree, and evaluation of model accuracy through a train-test split. The assignment provides practical insights into supervised learning and the functionality of decision trees in machine learning.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Programming Assignment Unit # 5

Subject: Data Mining and Machine Learning

Instructor: Dr. Shabia Shabbir

Submitted By: Muhammad Abdul Rehman Khan

Date: 8- 10- 2025


Introduction:

This assignment explores the use of decision trees, a fundamental and widely-used machine

learning method, for a binary classification task using the Ionosphere dataset. The aim is to

build and visualize a decision tree model that classifies radar returns as "good" or "bad" and

then evaluate the accuracy of the model on unseen data. By following a systematic process of

data loading, model building, visualization, and performance assessment, this exercise provides

practical insight into supervised learning and the behavior of decision trees.

Data Set Information: This radar data was collected by a system in Goose Bay, Labrador. This

system consists of a phased array of 16 high-frequency antennas with a total transmitted power

on the order of 6.4 kilowatts. See the paper for more details. The targets were free electrons in

the ionosphere.
"Good" radar returns are those showing evidence of some type of structure in the ionosphere.

"Bad" returns are those that do not; their signals pass through the ionosphere.

Received signals were processed using an autocorrelation function whose arguments are the time

of a pulse and the pulse number. There were 17 pulse numbers for the Goose Bay system.

Instances in this database are described by 2 attributes per pulse number, corresponding to the

complex values returned by the function resulting from the complex electromagnetic signal.

Attribute Information:

-- All 34 are continuous


-- The 35th attribute is either "good" or "bad" according to the definition summarized above.

This is a binary classification exercise.

Download the data set:

[Link] txt

This assignment follows the programming lab in section 8.3 of the textbook closely. If you are

unsure how to carry out part of the assignment, it could be helpful to use the lab as a reference. It

might also be helpful to refer to the manual for the rpart package:

[Link]

Part 1: Print decision tree

a. We begin by setting the working directory, loading the required packages (rpart and

mlbench)

and then loading the Ionosphere dataset.

#set working directory if needed (modify path as needed)

setwd(“working directory”)

#load required libraries – rpart for classification and regression trees

library(rpart)

#mlbench for Ionosphere dataset

library(mlbench)
#load Ionosphere

data(Ionosphere)

Answer:

> setwd("C:\\Users\\DELL\\OneDrive\\Desktop")
> getwd()
[1] "C:/Users/DELL/OneDrive/Desktop"
> library(rpart)
> library(mlbench)
> data(Ionosphere)

> View(Ionosphere)
Explanation:

The first step involves setting the working environment and loading necessary libraries,

specifically rpart for modeling and mlbench for accessing the Ionosphere dataset. Loading the

dataset into R makes the data ready for analysis, which contains 34 continuous features

describing radar signals with a binary classification target.


b. Use the rpart() method to create a regression tree for the data.

rpart(Class~.,Ionosphere)

Answer:

> [Link]=rpart(Class~., Ionosphere)


> [Link]
n= 351

node), split, n, loss, yval, (yprob)


* denotes terminal node

1) root 351 126 good (0.35897436 0.64102564)


2) V5< 0.23154 77 4 bad (0.94805195 0.05194805) *
3) V5>=0.23154 274 53 good (0.19343066 0.80656934)
6) V27>=0.999945 52 13 bad (0.75000000 0.25000000)
12) V1=0 19 0 bad (1.00000000 0.00000000) *
13) V1=1 33 13 bad (0.60606061 0.39393939)
26) V3< 0.73004 8 0 bad (1.00000000 0.00000000) *
27) V3>=0.73004 25 12 good (0.48000000 0.52000000)
54) V22>=0.47714 9 1 bad (0.88888889 0.11111111) *
55) V22< 0.47714 16 4 good (0.25000000 0.75000000) *
7) V27< 0.999945 222 14 good (0.06306306 0.93693694) *

Explanation:

Using the entire dataset, a decision tree model is created with the formula Class~. , telling the
model to predict the class based on all other features. This initial tree represents how the data is
split recursively to organize the instances into groups that are as pure as possible regarding the
"good" or "bad" labels. Building the model on the full dataset helps visualize the complete
decision logic without any partitioning bias.

c. Use the plot() and text() methods to plot the decision tree.

Answer:

> plot([Link])
> text([Link],pretty=0)

Explanation:

Once the model is created, it is important to visualize the decision tree to understand how

features drive classification decisions. The plot() function draws the tree structure showing

nodes and splits, while the text() function adds labels explaining decision rules and predicted

classes at leaves.
This graphical representation breaks down the complex decision process into interpretable steps,

highlighting which features contribute most significantly to classification and how thresholds

define the splits. Visualizing the tree gives a tangible feel for the model's reasoning, which is key

for interpreting machine learning models.

Part 2: Estimate accuracy

a. Split the data a test and train subsets using the sample() method.

Answer:

> [Link]=(10)
> train=sample(1:nrow(Ionosphere),200)
> [Link]=Ionosphere[-train ,]

Explanation:

In this step, for a fair evaluation of the model’s performance, the dataset is randomly divided into

training and testing subsets. This is crucial because training accuracy alone can be misleading if

the model overfits the data.

Using the sample() function ensures that the split is random, which helps generalize the model's

ability to classify new unseen data accurately. Training on one subset and testing on another
mimics real-world use by assessing how well the model predicts data it has never encountered.

b. Use the rpart method to create a decision tree using the training data.

rpart(Class~.,Ionosphere,subset=train)

Answer

> [Link]=rpart(Class~., Ionosphere,subset=train)


> [Link]
n= 200

node), split, n, loss, yval, (yprob)


* denotes terminal node

1) root 200 64 good (0.32000000 0.68000000)


2) V5< 0.28573 42 3 bad (0.92857143 0.07142857) *
3) V5>=0.28573 158 25 good (0.15822785 0.84177215)
6) V27>=0.99921 23 6 bad (0.73913043 0.26086957) *
7) V27< 0.99921 135 8 good (0.05925926 0.94074074) *

Explanation:

With the training subset prepared, a new decision tree model is built specifically on this data
only. This ensures that the model learns patterns solely from the training set without peeking at
the test data.
The rpart() function is applied again with the training data, allowing the tree to tailor its splits
based on this dataset. This preserves the objectivity needed for performance measurement,
making the model ready for unbiased testing.

c. Use the predict method to find the predicted class labels for the testing data.

Answer:

> [Link]=predict([Link],[Link],type="class")

Explanation:

After building the model, predictions are generated for the test subset using the predict()

function, specifying a class output to get discrete labels.

Comparing these predicted labels with the actual labels forms the basis of a confusion matrix,

created using the table() function. This matrix counts true positives, true negatives, false

positives, and false negatives, summarizing how well the model classifies each category.

d. Use the table method to create a table of the predictions versus true labels and then

compute the accuracy. The accuracy is the number of correctly assigned good cases
(true positives) plus the number of correctly assigned bad cases (true negatives)

divided by the total number of testing cases.

e. > table([Link],Ionosphere$Class[-train])
f.
g. [Link] bad good
h. bad 56 11
i. good 6 78

Explanation:

In this step, accuracy is calculated by dividing the sum of correct predictions (true positives plus

true negatives) by the total number of test instances. This final measure indicates the overall

effectiveness of the decision tree in correctly identifying radar returns as "good" or "bad."

Conclusion:

The assignment demonstrates the practical application of decision trees for binary classification,

following a clear workflow of data preparation, model creation, visualization, and evaluation.

Visualizing the tree helped clarify the decision-making process, while the train-test split ensured

an honest assessment of the model’s predictive accuracy.


Reference:

James, G., Witten, D., Hastie, T., & Tibshirani, R. (2013). An introduction to statistical learning

with applications in R. New York, NY: Springer. Read Chapter 8.

Review the presentation which details how to compute entropy, information gain as part of an

algorithm to ‘learn’ a decision tree. Available from

[Link]

Common questions

Powered by AI

Overfitting in decision trees manifests when the model captures noise as if it were a signal, overly tailoring to the training data with intricate splits that do not generalize well to new data. Splitting the dataset into training and testing subsets helps to mitigate this risk by ensuring the model is evaluated on separate data it wasn't trained on. The performance on the test set serves as a check against overfitting, ensuring that the model maintains its predictive power across different instances .

The key assumptions underlying the construction of decision trees using the rpart package include the notion that the dataset has enough variation to be split effectively, the features are relevant to the class labels, and the model can balance bias-variance trade-off. These assumptions influence model accuracy by dictating the clearness of the decision boundaries and the tree's ability to generalize from training to test data. Violating these assumptions can lead to underfitting or overfitting, thereby reducing model accuracy .

The rpart package in R is used to construct decision trees by providing functional tools such as the rpart() method, which creates the regression or classification tree. For the Ionosphere dataset, the model is built using the formula Class~., indicating classification based on all attributes. Visualization is achieved using plot() to draw the tree structure and text() to annotate the nodes with decision rules. These functionalities help to understand the feature-driven decision boundaries of the tree .

Splitting the Ionosphere dataset into training and testing subsets is fundamental for unbiased model evaluation. This process is intended to prevent data leakage and ensure that the model is trained on one portion and validated on another. This method simulates a real-world scenario by testing the model's performance on unseen data, providing a realistic measure of its predictive accuracy and mitigating overfitting risks .

The set.seed() function improves the reliability of results by ensuring that the random partitioning of the Ionosphere dataset into train and test sets is reproducible. This means that each time the model is run under the same conditions, the same sample will be used, providing consistency in training and testing, and allowing for fair comparison across different runs or model tweaks .

Visualizing the decision tree model is essential because it provides an intuitive understanding of how data features contribute to classification decisions. By outlining the feature splits and corresponding decision paths, visualization helps identify which attributes are most influential in determining outcomes. Additionally, it clarifies interaction effects between features, aiding in designing more informed decision-making policies or further model refinement .

In decision tree models, continuous attributes are typically handled by determining optimal split points that maximize purity. In the Ionosphere dataset, all 34 input attributes are continuous and used in the splitting process to distinguish between 'good' and 'bad' returns via thresholding techniques like Gini impurity or entropy. Categorical attributes would be handled by partitioning according to subset distribution or splitting into binary splits for each category, ensuring all attribute types contribute effectively to decision-making .

The use of an autocorrelation function to process signals in the Ionosphere dataset standardizes signal features and extracts meaningful patterns to classify 'good' or 'bad' returns. This processing is crucial for ensuring the input data retains relevant information for classification tasks. However, if the function fails to capture meaningful variations or introduces noise, it could mislead the decision tree model, affecting performance by creating splits that do not correlate well with the actual class labels .

The effectiveness of the decision tree model is judged through the confusion matrix by comparing the predicted class labels to the actual labels of the test data. It counts true positives, true negatives, false positives, and false negatives. The overall accuracy is derived by adding the number of true positives and true negatives and dividing by the total number of instances. This matrix offers insights into the model’s classification performance and helps identify areas where it may misclassify .

The feature V5 plays a crucial role in the decision-making process of the decision tree model for the Ionosphere dataset. It is among the initial nodes used to split the data, indicating its significance in distinguishing between 'good' and 'bad' radar returns. Specifically, splits based on V5 values help separate instances into more homogeneous groups, thereby enhancing the model's predictive accuracy .

You might also like