Skip to content

[Task] Implement Isolation Forest on the Shuttle Dataset#24

Open
ankitkumar25-rk wants to merge 2 commits into
Tasksfrom
task-ankit
Open

[Task] Implement Isolation Forest on the Shuttle Dataset#24
ankitkumar25-rk wants to merge 2 commits into
Tasksfrom
task-ankit

Conversation

@ankitkumar25-rk

@ankitkumar25-rk ankitkumar25-rk commented Jul 13, 2026

Copy link
Copy Markdown
Collaborator

Pull Request

Related Issue

Closes #18


Task Summary

I used Isolation Forest algorithm to find anomalies in the Shuttle dataset. This dataset had 7
classes in it, but I needed only 2 classes (normal and anomaly) for this task, so I changed it
myself. I made class 1 and 4 as normal, and the rest of the classes as anomaly because they had
very less count. After that I trained the Isolation Forest model (this model does not need labels
to train, which is a good thing) and checked how good it is doing by comparing with the real
labels. I also changed the contamination value a few times to see what happens to the results.


Dataset

  • Mammography
  • Shuttle

Dataset Source: I got the dataset from OpenML in .arff file format.


Preprocessing

  • I checked for missing values using isnull().sum() and there was no missing value anywhere
  • I used scipy.io.arff to open the file because normal read_csv was not working on arff files
  • The class column was coming like b'1' (in bytes form), so I used .str.decode('utf-8') to
    fix it and then changed it to int
  • I made a new label column: class 1 and 4 = normal (0), class 2,3,5,6,7 = anomaly (1). This
    gives around 6.05% anomaly rate which looks realistic
  • I took the 9 columns (A1 to A9) as X and label as y (y is only used to check results later, not
    used while training)
  • I split the data into train (70%) and test (30%) parts, and used stratify so the anomaly ratio
    stays same in both parts
  • I did not do any scaling or encoding because all 9 features were already numbers

Model Configuration

Hyperparameter Value
n_estimators 100
contamination 0.0605
max_samples 256
max_features 1.0 (default, used all features)
random_state 42

Evaluation Results

Metric Value
Precision 0.943
Recall 0.937
F1-score 0.940
ROC-AUC (Optional) 0.996

Visualizations

image Screenshot from 2026-07-13 23-45-34

Key Observations

  • The model worked really good even without using labels while training. It just finds points
    that are easy to separate from others and calls them anomaly
  • Contamination value made the biggest difference in the results because it decides where the
    model draws the line between normal and anomaly
    • When contamination was very low (0.01), precision was perfect (1.000) but recall was very bad
      (0.146), because model became too strict
    • When contamination was high (0.2), recall became very good (0.992) but precision became bad
      (0.298), because model started calling too many points as anomaly
    • Best result came at contamination = 0.0605, which is same as the real anomaly rate in data
  • I did not change n_estimators and max_samples much because they were already giving good result
    at default values (100 and 256)
  • Challenges I faced: Understanding the .arff file and fixing the bytes format class column took some time; deciding
    how to split the 7 classes into 2 classes was confusing at first but looking at the class count
    helped me decide

Checklist

  • Code runs successfully
  • Notebook (.ipynb) included
  • Code is well-commented
  • README/documentation updated
  • At least 2 plots included
  • PR is linked to the corresponding issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant