Skip to content

Add Isolation Forest notebook#25

Open
khyati-1711 wants to merge 1 commit into
devlup-labs:Tasksfrom
khyati-1711:task1/khyati
Open

Add Isolation Forest notebook#25
khyati-1711 wants to merge 1 commit into
devlup-labs:Tasksfrom
khyati-1711:task1/khyati

Conversation

@khyati-1711

Copy link
Copy Markdown
Collaborator

Pull Request

Related Issue

Closes #14


Task Summary

Implemented an anomaly detection pipeline using Isolation Forest on the Mammography dataset to identify (anomalous) cases .

  • Loaded the Mammography dataset via fetch_openml, cleaned it (nulls, duplicates), and mapped the target to a binary scheme (0 = normal, 1 = anomaly/malignant).
  • Split the data into train/test sets (stratified) and scaled features with StandardScaler.
  • Trained an IsolationForest model on the scaled training data and used it to score/predict anomalies on the full deduplicated dataset.
  • Evaluated performance using precision, recall, F1, ROC-AUC, a classification report, and a confusion matrix.
  • Approach followed: standard unsupervised outlier-detection workflow (EDA → cleaning → scaling → Isolation Forest fit → scoring → threshold-based prediction → evaluation against ground-truth labels).

Dataset

  • Mammography
  • Shuttle

Dataset Source: sklearn.datasets.fetch_openml(name="mammography", version=1, as_frame=True)


Preprocessing

  • Missing values: Checked with isnull().sum() (none found), and dropna() applied .
  • Duplicates: 3,334 duplicate rows identified and dropped (dataset went from 11,183 → 7,849 rows).
  • Feature scaling: StandardScaler fit on training features and applied to both train and test sets.
  • Encoding: Target labels mapped from {-1, 1} to {0, 1} (0 = normal, 1 = anomaly).
  • Feature selection: None performed — all 6 original attributes (attr1attr6) retained.
  • Train/test split: 80/20 split, stratified on the target class, random_state=42.

Model Configuration

Hyperparameter Value
n_estimators 300
contamination 0.03
max_samples auto
max_features 1.0
random_state 42

Evaluation Results

Metric Value
Precision 0.189
Recall 0.256
F1-score 0.217
ROC-AUC 0.821

Confusion Matrix:

Classification Report (per class):

Class Precision Recall F1-score Support
0 (Normal) 0.98 0.96 0.97 7595
1 (Anomaly) 0.20 0.27 0.23 254
Accuracy 0.94 7849

Visualizations

Attach at least 2 plots from your analysis.

  1. Class Distribution bar plot
class_distribution
  1. Anomaly Score Scatter Plot
anomaly_score_scatter
  1. Confusion Matrix heatmap
confusion_matrix (1)

Key Observations

  • What worked well: The pipeline runs end-to-end cleanly — data loading, cleaning, scaling, model fitting, and evaluation all execute without errors. ROC-AUC of ~0.82 shows the model has reasonable ranking ability between normal and anomalous points despite no label supervision during training.
  • Biggest impact hyperparameter: contamination (set to 0.03) directly controls the anomaly threshold and therefore has an effect on precision/recall trade-off — since the true anomaly rate (~3.2%) is close to this value.
  • Challenges faced: Significant duplicate rows (3,334 of 11,183) needed removal before training to avoid biasing the fitted distribution. Care was also needed to keep X/y alignment consistent after deduplication and label remapping across -1/1 and 0/1 conventions.

Checklist

  • Code runs successfully
  • Notebook (.ipynb) included
  • Code is well-commented (minimal comments — consider adding more before merge)
  • 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.

2 participants