PythonPlaza - Python & AI

Supervised Machine Learning Algorithms

Gradient Boosting


A machine learning method called Gradient Boosting creates an ensemble by combining several weak prediction models. Decision trees, which are sequentially trained to reduce errors and increase accuracy, are commonly used as these weak models. Gradient boosting can efficiently capture intricate correlations between features by combining several decision tree regressors or decision tree classifiers.

Gradient boosting's capacity to iteratively minimize the loss function is one of its main advantages. One loss function used to assess how well a machine learning model matches actual data is Mean Squared Error (MSE). MSE determines the mean of the squared discrepancies between the observed and expected values.


MAE (Mean Absolute Error) quantifies the average magnitude of errors for Gradient Boosting Regression.

Mean Absolute Error
It calculates the average discrepancy between a dataset's actual and forecasted values. Without taking direction into account, it displays the deviation between predicted and actual values.
1. Determined by utilizing absolute differences
2. Easy to calculate and understand
3. Handles every mistake equally
4. Not as susceptible to significant errors as MSE
5. Frequently employed to assess regression models





Gradient Boosting Classification

Complete Loan Default Prediction Example

10 Loans • 3 Independent Variables • 3 Decision Trees • Logistic Loss

η = 0.5Simple gradient/residual leaf valueF₀ computed from class proportion

1. Dataset

LoanIncomeDebt RatioCredit ScoreDefault Y
1$30k0.805801
2$35k0.756001
3$40k0.706201
4$45k0.656401
5$50k0.606600
6$55k0.506800
7$60k0.457000
8$65k0.407200
9$70k0.357400
10$80k0.307600
Y = 1 means Default; Y = 0 means No Default.

2. Calculate the Initial F₀ Correctly

There are 4 defaults among 10 loans, so the initial class probability is:

p₀ = Ŷ = 4 / 10 = 0.40

For logistic boosting, convert this probability to initial log-odds:

F₀ = ln(p₀ / (1 − p₀))
F₀ = ln(0.40 / 0.60) = ln(2/3) ≈ -0.4055

Check the conversion back to probability:

p₀ = 1 / (1 + e−F₀) = 1 / (1 + e0.4055) ≈ 0.4000
Important: We use this principled F₀, but from Tree 1 onward we use the requested simple leaf value γ = Σ(Y − p) / Nleaf.

3. Tree 1 — Credit Score Split

Residuals are the negative gradients for logistic loss:

rᵢ = Yᵢ − pᵢ

Initially p₀ = 0.4 for every loan.

LoansYp₀Residual Y − p₀
1–410.40+0.60
5–1000.40−0.40

Suppose Tree 1 splits on Credit Score ≤ 650.

Credit Score ≤ 650?
YES: Loans 1–4
γ₁ = +0.6000
NO: Loans 5–10
γ₂ = −0.4000
Leaf 1
(0.6 + 0.6 + 0.6 + 0.6) / 4 = 0.6000
Leaf 2
(−0.4 × 6) / 6 = −0.4000

Apply η = 0.5

F₁ = F₀ + ηγ
Loans 1–4: F₁ = -0.4055 + (0.5)(0.6) = -0.1055
Loans 5–10: F₁ = -0.4055 + (0.5)(−0.4) = -0.6055
GroupF₁p₁
Loans 1–4-0.10550.4737
Loans 5–10-0.60550.3531

4. Tree 2 — Debt Ratio Split

Now Tree 2 learns from the remaining residuals:

r₂ = Y − p₁
GroupYp₁Residual
Loans 1–410.4737+0.5263
Loans 5–1000.3531-0.3531

Suppose Tree 2 splits on Debt Ratio > 0.625.

Debt Ratio > 0.625?
YES: Loans 1–4
γ = 0.5263
NO: Loans 5–10
γ = -0.3531
Leaf 1
(4 × 0.5263) / 4 = 0.5263
Leaf 2
(6 × -0.3531) / 6 = -0.3531

Apply Tree 2

Loans 1–4: F₂ = -0.1055 + (0.5)(0.5263) = 0.1577
p₂ = 1/(1 + e−0.1577) = 0.5393
Loans 5–10: F₂ = -0.6055 + (0.5)(-0.3531) = -0.7820
p₂ = 1/(1 + e−(-0.7820)) = 0.3139

5. Tree 3 — Income Split

r₃ = Y − p₂
GroupYp₂Residual
Loans 1–410.5393+0.4607
Loans 5–1000.3139-0.3139

Suppose Tree 3 splits on Income ≤ $47.5k.

Income ≤ $47.5k?
YES: Loans 1–4
γ = 0.4607
NO: Loans 5–10
γ = -0.3139
Leaf 1
(4 × 0.4607) / 4 = 0.4607
Leaf 2
(6 × -0.3139) / 6 = -0.3139

Apply Tree 3

Loans 1–4: F₃ = 0.1577 + (0.5)(0.4607) = 0.3880
p₃ = 1/(1 + e−0.3880) = 0.5958
Loans 5–10: F₃ = -0.7820 + (0.5)(-0.3139) = -0.9390
p₃ = 1/(1 + e−(-0.9390)) = 0.2811

6. Final Results

LoanYF₀F₁F₂F₃Final pPrediction
11-0.4055-0.10550.15770.38800.5958Default
21-0.4055-0.10550.15770.38800.5958Default
31-0.4055-0.10550.15770.38800.5958Default
41-0.4055-0.10550.15770.38800.5958Default
50-0.4055-0.6055-0.7820-0.93900.2811No Default
60-0.4055-0.6055-0.7820-0.93900.2811No Default
70-0.4055-0.6055-0.7820-0.93900.2811No Default
80-0.4055-0.6055-0.7820-0.93900.2811No Default
90-0.4055-0.6055-0.7820-0.93900.2811No Default
100-0.4055-0.6055-0.7820-0.93900.2811No Default
Classification rule: p ≥ 0.5 → Default; p < 0.5 → No Default.
For this deliberately simple dataset and the selected splits, the 3-tree model classifies all 10 training loans correctly.

7. Algorithm Summary

Step 1: p₀ = number of defaults / total loans = 0.40
Step 2: F₀ = ln(p₀/(1−p₀)) = -0.4055
Step 3: rᵢ = Yᵢ − pᵢ
Step 4: γleaf = Σ(Y − p) / Nleaf
Step 5: Ft = Ft−1 + ηγ,   η = 0.5
Step 6: pt = 1/(1 + e−Ft)
Step 7: Repeat for Tree 2, Tree 3, …
Key distinction: F₀ is calculated using the logistic log-odds formula. The later leaf values are intentionally the simple average residual, not the Newton/Hessian leaf formula.

Gradient Boosting Classification • Loan Default Worked Example • Simple Gradient/Residual Leaf Values

USE CASE 1: Use Gradient Boosting with scikit-learn to predict whether a loan will default. Dependent variable: Default (0 = No Default, 1 = Default) Independent variables (3): Income (monthly income, e.g., 1000–10000) CreditScore (300–850) LoanAmount (1000–50000).


import numpy as np import pandas as pd from sklearn.model_selection import train_test_split from sklearn.ensemble import GradientBoostingClassifier from sklearn.metrics import accuracy_score, confusion_matrix, classification_report, roc_auc_score # ----------------------------------- # 1. Load data from Excel # ----------------------------------- data = pd.read_excel("loan_data.xlsx") df = pd.DataFrame(data) print("Dataset Preview:") print(data.head()) # ----------------------------------- # 2. Define features and target # ----------------------------------- X = df[["Income", "CreditScore", "LoanAmount"]] y = df["Default"] # ----------------------------------- # 3. Split into training and testing # ----------------------------------- X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.3, random_state=42 ) gb_model = GradientBoostingClassifier( n_estimators=100, # number of trees learning_rate=0.1, # step size max_depth=3, # tree depth random_state=42 ) gb_model.fit(X_train, y_train) y_pred = gb_model.predict(X_test) y_prob = gb_model.predict_proba(X_test)[:, 1] print("Accuracy:", accuracy_score(y_test, y_pred)) print("\nConfusion Matrix:\n", confusion_matrix(y_test, y_pred)) print("\nClassification Report:\n", classification_report(y_test, y_pred)) print("\nROC-AUC Score:", roc_auc_score(y_test, y_prob)) Step 9: Predict default for a new customer new_customer = [[4500, 620, 16000]] # Income, CreditScore, LoanAmount default_prediction = gb_model.predict(new_customer) default_probability =gb_model.predict_proba(new_customer)[0][1] print("Default Prediction:", default_prediction[0]) print("Probability of Default:", default_probability)

USE CASE 2: Customer Churn example using Gradient Boosting with scikit-learn in Python. We’ll assume 4 independent variables, for example: Tenure (months with company) - 1–60 months MonthlyCharges (amount billed per month) - 30–120 ContractType (0=Month-to-month, 1=One-year, 2=Two-year) SupportCalls (number of calls to support) 0–10 The dependent variable is Churn (0=Stay, 1=Churn)..





import numpy as np import pandas as pd from sklearn.model_selection import train_test_split from sklearn.ensemble import GradientBoostingClassifier from sklearn.metrics import accuracy_score, confusion_matrix, classification_report, roc_auc_score # ----------------------------------- # 1. Load data from Excel # ----------------------------------- #sample data can be exported to #excel from the URL # https://www.pythonplaza.com/categorical_customer_churn_1_or_0.html data = pd.read_excel("customer_data.xlsx") print("Dataset Preview:") print(data.head()) # ----------------------------------- # 2. Define features and target # ----------------------------------- X = df[["Tenure", "MonthlyCharges", "ContractType", "SupportCalls"]] y = df["Churn"] # ----------------------------------- # 3. Split into training and testing # ----------------------------------- X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.3, random_state=42 ) gb_model = GradientBoostingClassifier( n_estimators=100, # number of trees learning_rate=0.1, # step size max_depth=3, # tree depth random_state=42 ) gb_model.fit(X_train, y_train) y_pred = gb_model.predict(X_test) y_prob = gb_model.predict_proba(X_test)[:, 1] print("Accuracy:", accuracy_score(y_test, y_pred)) print("\nConfusion Matrix:\n", confusion_matrix(y_test, y_pred)) print("\nClassification Report:\n", classification_report(y_test, y_pred)) print("\nROC-AUC Score:", roc_auc_score(y_test, y_prob)) #Predict churn for a new customer new_customer = [[8, 92, 0, 5]] # Tenure, MonthlyCharges, ContractType, SupportCalls churn_prediction = gb_model.predict(new_customer) churn_probability = gb_model.predict_proba(new_customer)[0][1] print("Churn Prediction:", churn_prediction[0]) print("Probability of Churn:", churn_probability) #Interpreting the results (business view) 1 → High risk of churn ⚠️ 0 → Likely to stay ✅ Use probability (e.g., churn > 0.6) to trigger retention offers

USE CASE 3: Use Gradient Boosting to determine what learning style a student prefers -
Visual, Auditory, Reading/Writing, Kinesthetic (Dependent Variable)

Independent variables (How a student prefers to learn)

prefers_diagrams – How much a student likes diagrams (1-5)
prefers_lectures – How much a student likes lectures (1-5)
prefers_notes – How much a student likes reading/writing notes (1-5)
prefers_hands_on – How much a student likes hands-on activities (1-5)





import numpy as np import pandas as pd from sklearn.model_selection import train_test_split from sklearn.ensemble import GradientBoostingClassifier from sklearn.metrics import accuracy_score, confusion_matrix, classification_report, roc_auc_score # ----------------------------------- # 1. Load data from Excel # ----------------------------------- #sample data can be exported to #excel from the URL Get the Categorical learning Styles data in Excel data = pd.read_excel("Categorical_learning_Styles.xlsx") print("Dataset Preview:") print(data.head()) # ----------------------------------- # 2. Define the data # ----------------------------------- X = df[['prefers_diagrams', 'prefers_lectures', 'prefers_notes', 'prefers_hands_on']] y = df['learning_style'] # Encode categorical target labels le = LabelEncoder() y_encoded = le.fit_transform(y) # ----------------------------------- # 3. Split into training and testing # ----------------------------------- X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.3, random_state=42 ) gb_model = GradientBoostingClassifier( n_estimators=100, # number of trees learning_rate=0.1, # step size max_depth=3, # tree depth random_state=42 ) gb_model.fit(X_train, y_train) y_pred = gb_model.predict(X_test) y_prob = gb_model.predict_proba(X_test)[:, 1] print("Accuracy:", accuracy_score(y_test, y_pred)) print("\nConfusion Matrix:\n", confusion_matrix(y_test, y_pred)) print("\nClassification Report:\n", classification_report(y_test, y_pred)) print("\nROC-AUC Score:", roc_auc_score(y_test, y_prob)) #Predict with sample data new_students = np.array([ [5, 1, 2, 1], # Likely Visual [1, 5, 3, 2], # Likely Auditory [2, 1, 5, 2], # Likely Reading/Writing [1, 2, 1, 5] # Likely Kinesthetic ]) # Predict encoded labels predictions_encoded = gb_model.predict(new_students) # Convert numeric predictions back to original labels predictions = le.inverse_transform(predictions_encoded) print("Predicted Learning Styles:") print(predictions)

USE CASE 4: Use Gradient Boosting to predict if a person has a disease. Age, BloodPressure,Cholesterol,FamilyHistory, are independent variables and Disease is a dependent variable.





import numpy as np import pandas as pd from sklearn.model_selection import train_test_split from sklearn.ensemble import GradientBoostingClassifier from sklearn.metrics import accuracy_score, confusion_matrix, classification_report, roc_auc_score # ----------------------------------- # 1. Load data from Excel # ----------------------------------- #sample data can be exported to #excel from the URL Get Disease Classification in Excel data = pd.read_excel("patient_dosage_response.xlsx") print("Dataset Preview:") print(data.head()) df = pd.DataFrame(data) # ---------------------------------- # 2. Separate Features and Target # ---------------------------------- X = df[['Age', 'BloodPressure', 'Cholesterol', 'FamilyHistory']] y = df['Disease'] # ----------------------------------- # 3. Split into training and testing # ----------------------------------- X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.3, random_state=42 ) gb_model = GradientBoostingClassifier( n_estimators=100, # number of trees learning_rate=0.1, # step size max_depth=3, # tree depth random_state=42 ) gb_model.fit(X_train, y_train) y_pred = gb_model.predict(X_test) y_prob = gb_model.predict_proba(X_test)[:, 1] print("Accuracy:", accuracy_score(y_test, y_pred)) print("\nConfusion Matrix:\n", confusion_matrix(y_test, y_pred)) print("\nClassification Report:\n", classification_report(y_test, y_pred)) print("\nROC-AUC Score:", roc_auc_score(y_test, y_prob)) new_patients = np.array([ [45, 150, 230, 1], # High risk [28, 118, 175, 0] # Low risk ]) predictions = gb_model.predict(new_patients) print("Disease Predictions:") print(predictions)


About Us  | Contact Us | Sitemap  | Privacy Policy



About Us  | Contact Us | Sitemap  | Privacy Policy