sklearn-docs/GradientBoostingClassifier
1
1import gradio as gr2import numpy as np3import pandas as pd4import matplotlib5import matplotlib.pyplot as plt6 7from datasets import load_dataset8from sklearn.ensemble import GradientBoostingClassifier9from sklearn.model_selection import train_test_split10from sklearn.metrics import accuracy_score, confusion_matrix11 12matplotlib.use('Agg')13 14################################################################################15# SUGGESTED_DATASETS: These must actually exist on huggingface.co/datasets16#17# "scikit-learn/iris" -> A small, classic Iris dataset with a "train" split18# "uci/wine" -> Another small dataset with a "train" split19# "SKIP/ENTER_CUSTOM" -> Placeholder to let the user enter a custom dataset ID20################################################################################21SUGGESTED_DATASETS = [22 "scikit-learn/iris",23 "uci/wine",24 "SKIP/ENTER_CUSTOM"25]26 27def update_columns(dataset_id, custom_dataset_id):28 """29 After the user chooses a dataset from the dropdown or enters their own,30 this function loads the dataset's "train" split, converts it to a DataFrame,31 and returns the columns. These columns are used to populate the Label and32 Feature selectors in the UI.33 """34 if dataset_id != "SKIP/ENTER_CUSTOM":35 final_id = dataset_id36 else:37 final_id = custom_dataset_id.strip()38 39 try:40 ds = load_dataset(final_id, split="train")41 df = pd.DataFrame(ds)42 cols = df.columns.tolist()43 44 message = (45 f"**Loaded dataset**: `{final_id}`\n\n"46 f"**Columns found**: {cols}"47 )48 return (49 gr.update(choices=cols, value=None), # label_col dropdown50 gr.update(choices=cols, value=[]), # feature_cols checkbox group51 message52 )53 except Exception as e:54 err_msg = f"**Error loading** `{final_id}`: {e}"55 return (56 gr.update(choices=[], value=None),57 gr.update(choices=[], value=[]),58 err_msg59 )60 61def train_model(dataset_id, custom_dataset_id, label_column, feature_columns,62 learning_rate, n_estimators, max_depth, test_size):63 """64 1. Decide which dataset ID to load (from dropdown or custom).65 2. Load that dataset's 'train' split, turn into DataFrame, extract X (features) and y (label).66 3. Train a GradientBoostingClassifier on X_train, y_train.67 4. Compute accuracy and confusion matrix on X_test, y_test.68 5. Plot and return feature importances + confusion matrix heatmap + textual summary.69 """70 # Resolve final dataset ID71 if dataset_id != "SKIP/ENTER_CUSTOM":72 final_id = dataset_id73 else:74 final_id = custom_dataset_id.strip()75 76 # Load dataset -> df77 ds = load_dataset(final_id, split="train")78 df = pd.DataFrame(ds)79 80 # Validate columns81 if label_column not in df.columns:82 raise ValueError(f"Label column '{label_column}' not found in dataset columns.")83 for fc in feature_columns:84 if fc not in df.columns:85 raise ValueError(f"Feature column '{fc}' not found in dataset columns.")86 87 # Convert to NumPy arrays88 X = df[feature_columns].values89 y = df[label_column].values90 91 # Train/test split92 X_train, X_test, y_train, y_test = train_test_split(93 X, y, test_size=test_size, random_state=4294 )95 96 # Instantiate and train GradientBoostingClassifier97 clf = GradientBoostingClassifier(98 learning_rate=learning_rate,99 n_estimators=int(n_estimators),100 max_depth=int(max_depth),101 random_state=42102 )103 clf.fit(X_train, y_train)104 105 # Evaluate106 y_pred = clf.predict(X_test)107 accuracy = accuracy_score(y_test, y_pred)108 cm = confusion_matrix(y_test, y_pred)109 110 # Create Matplotlib figure with feature importances + confusion matrix111 fig, axs = plt.subplots(1, 2, figsize=(10, 4))112 113 # Subplot 1: Feature Importances114 importances = clf.feature_importances_115 axs[0].barh(range(len(feature_columns)), importances, color='skyblue')116 axs[0].set_yticks(range(len(feature_columns)))117 axs[0].set_yticklabels(feature_columns)118 axs[0].set_xlabel("Importance")119 axs[0].set_title("Feature Importances")120 121 # Subplot 2: Confusion Matrix Heatmap122 im = axs[1].imshow(cm, interpolation='nearest', cmap=plt.cm.Blues)123 axs[1].set_title("Confusion Matrix")124 plt.colorbar(im, ax=axs[1])125 axs[1].set_xlabel("Predicted")126 axs[1].set_ylabel("True")127 128 # Optionally annotate each cell with numeric counts129 thresh = cm.max() / 2.0130 for i in range(cm.shape[0]):131 for j in range(cm.shape[1]):132 color = "white" if cm[i, j] > thresh else "black"133 axs[1].text(j, i, str(cm[i, j]), ha="center", va="center", color=color)134 135 plt.tight_layout()136 137 # Textual summary138 text_summary = (139 f"**Dataset used**: `{final_id}`\n\n"140 f"**Label column**: `{label_column}`\n\n"141 f"**Feature columns**: `{feature_columns}`\n\n"142 f"**Accuracy**: {accuracy:.3f}\n\n"143 )144 145 return text_summary, fig146 147###############################################################################148# Gradio UI149###############################################################################150with gr.Blocks() as demo:151 152 # High-level title and description153 gr.Markdown(154 """155 # Introduction to Gradient Boosting156 157 This Space demonstrates how to train a [GradientBoostingClassifier](https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.GradientBoostingClassifier.html#gradientboostingclassifier) from **scikit-learn** on **tabular datasets** hosted on the [Hugging Face Hub](https://huggingface.co/datasets).158 159 Gradient Boosting is an ensemble machine learning technique that combines many weak learners (usually small decision trees) in an iterative, stage-wise fashion to create a stronger overall model. 160 In each step, the algorithm fits a new weak learner to the current errors of the combined ensemble, effectively allowing the model to focus on the hardest-to-predict data points. 161 By repeatedly adding these specialized trees, Gradient Boosting can capture complex patterns and deliver high predictive accuracy, especially on tabular data.162 163 **Put simply, Gradient Boosting makes a big deal out of small anomolies!**164 165 **Purpose**:166 - Easily explore hyperparameters (_learning_rate, n_estimators, max_depth_) and quickly train an ML model on real data.167 - Visualise model performance via confusion matrix heatmap and a feature importance plot.168 169 **Notes**:170 - The dataset must have a **"train"** split with tabular columns (i.e., no nested structures).171 - Large datasets may take time to download/train.172 - The confusion matrix helps you see how predictions compare to ground-truth labels. The diagonal cells show correct predictions; off-diagonal cells indicate misclassifications.173 - The feature importance plot shows which features the model relies on the most for its predictions.174 175 ---176 177 **Usage**:178 1. Select one of the suggested datasets from the dropdown _or_ enter any valid dataset from the [Hugging Face Hub](https://huggingface.co/datasets).179 2. Click **Load Columns** to retrieve the column names from the dataset's **train** split.180 3. Choose exactly _one_ **Label column** (the target) and one or more **Feature columns** (the inputs).181 4. Adjust hyperparameters (learning_rate, n_estimators, max_depth, test_size).182 5. Click **Train & Evaluate** to train a Gradient Boosting model and see its accuracy, feature importances, and confusion matrix.183 184 You are now a machine learning engineer, congratulations 🤗 185 186 ---187 """188 )189 190 with gr.Row():191 dataset_dropdown = gr.Dropdown(192 label="Choose suggested dataset",193 choices=SUGGESTED_DATASETS,194 value=SUGGESTED_DATASETS[0]195 )196 custom_dataset_id = gr.Textbox(197 label="Or enter a custom dataset ID",198 placeholder="e.g. user/my_custom_dataset"199 )200 201 load_cols_btn = gr.Button("Load Columns")202 load_cols_info = gr.Markdown()203 204 with gr.Row():205 label_col = gr.Dropdown(choices=[], label="Label column (choose 1)")206 feature_cols = gr.CheckboxGroup(choices=[], label="Feature columns (choose 1 or more)")207 208 # Model Hyperparameters209 learning_rate_slider = gr.Slider(210 minimum=0.01, maximum=1.0, value=0.1, step=0.01, 211 label="learning_rate"212 )213 n_estimators_slider = gr.Slider(214 minimum=50, maximum=300, value=100, step=50, 215 label="n_estimators"216 )217 max_depth_slider = gr.Slider(218 minimum=1, maximum=10, value=3, step=1, 219 label="max_depth"220 )221 test_size_slider = gr.Slider(222 minimum=0.1, maximum=0.9, value=0.3, step=0.1, 223 label="test_size fraction (0.1-0.9)"224 )225 226 train_button = gr.Button("Train & Evaluate")227 228 output_text = gr.Markdown()229 output_plot = gr.Plot()230 231 # Link the "Load Columns" button -> update_columns function232 load_cols_btn.click(233 fn=update_columns,234 inputs=[dataset_dropdown, custom_dataset_id],235 outputs=[label_col, feature_cols, load_cols_info],236 )237 238 # Link "Train & Evaluate" -> train_model function239 train_button.click(240 fn=train_model,241 inputs=[242 dataset_dropdown,243 custom_dataset_id,244 label_col,245 feature_cols,246 learning_rate_slider,247 n_estimators_slider,248 max_depth_slider,249 test_size_slider250 ],251 outputs=[output_text, output_plot],252 )253 254demo.launch()255 