IIT Madras Zanzibar · Teaching

← Z3003 Data Mining

Tutorial 3: Ensemble Methods

sklearn · AdaBoost · RandomForest · GradientBoosting  ·  Submit as dm03t[RR].py

Submission

Name your file dm03t[RR].py where RR = last two digits of your roll number. All tasks in one file with # Task N comments. Submit by the deadline announced in class.

Background

Ensemble methods combine many weak classifiers to form a strong one. AdaBoost trains classifiers sequentially, each focusing on the mistakes of the previous one. Random Forest trains many decision trees on random subsets of features and data in parallel, then takes a majority vote. Gradient Boosting builds trees sequentially to minimise a loss function. All three are workhorses of real-world data mining.

We use the Breast Cancer Wisconsin dataset (30 features, 2 classes, 569 samples) for this tutorial.

Tasks

Task 1 Load Data and Create Train/Test Split

  1. Import: numpy as np, from sklearn.datasets import load_breast_cancer, from sklearn.model_selection import train_test_split, from sklearn.preprocessing import StandardScaler
  2. Load: data = load_breast_cancer(). Store features as X, labels as y
  3. Split into 80% train / 20% test with random_state=42
  4. Standardise X using StandardScaler (fit on train, transform both train and test)
  5. Print the shapes of X_train, X_test, y_train, y_test and the class distribution in y_train
X_train: (455, 30) X_test: (114, 30) Class distribution in y_train: {0: 170, 1: 285}

Task 2 AdaBoost Classifier

  1. Import: from sklearn.ensemble import AdaBoostClassifier, from sklearn.metrics import accuracy_score, classification_report
  2. Create: ada = AdaBoostClassifier(n_estimators=100, random_state=42)
  3. Fit on training data and predict on test data
  4. Print the accuracy score
  5. Print the full classification report (precision, recall, F1 per class)
AdaBoost Accuracy: 0.9737 Classification Report: precision recall f1-score support 0 0.98 0.95 0.96 43 1 0.97 0.99 0.98 71 accuracy 0.97 114

Task 3 Random Forest Classifier

  1. Import: from sklearn.ensemble import RandomForestClassifier
  2. Create: rf = RandomForestClassifier(n_estimators=100, random_state=42)
  3. Fit and predict
  4. Print accuracy and classification report
  5. Print the top 5 most important features: rf.feature_importances_: use np.argsort() and data.feature_names
Random Forest Accuracy: 0.9649 Top 5 features by importance: worst concave points : 0.1423 worst radius : 0.1211 worst perimeter : 0.1058 mean concave points : 0.0987 worst area : 0.0912
Random Forest gives you feature importance for free: a huge advantage for high-dimensional mining tasks.

Task 4 Gradient Boosting Classifier

  1. Import: from sklearn.ensemble import GradientBoostingClassifier
  2. Create: gb = GradientBoostingClassifier(n_estimators=100, learning_rate=0.1, random_state=42)
  3. Fit and predict
  4. Print accuracy and classification report
Gradient Boosting Accuracy: 0.9561

Task 5 Comparison Table

Print a summary table comparing the three classifiers. Use formatted print statements.

Classifier Accuracy ---------------------------------- AdaBoost 0.9737 Random Forest 0.9649 Gradient Boosting 0.9561

Then answer as a comment in your code: which classifier performs best on this dataset, and why might AdaBoost outperform Random Forest here?

Consider: AdaBoost focuses on hard examples sequentially. Random Forest averages many independent trees. On clean, well-standardised data, which strategy might have an edge?

Task 6 Extension: Effect of Number of Estimators

For Random Forest, train with n_estimators = 10, 50, 100, 200 and record the test accuracy for each. Print the results. At what point does adding more trees stop improving accuracy? Write your observation as a comment.