Skip to content

Instantly share code, notes, and snippets.

@ShichengChen
Last active October 6, 2023 14:58
Show Gist options
  • Save ShichengChen/89ac08d42358f2be0e4daa346ec8539e to your computer and use it in GitHub Desktop.
Save ShichengChen/89ac08d42358f2be0e4daa346ec8539e to your computer and use it in GitHub Desktop.
xgboost regression demo
import numpy as np
# Toy dataset
X = np.array([1, 2, 3, 4, 5])
y = np.array([1, 2, 3, 4, 5])
# Initial prediction: mean of y (Predictor Function)
initial_prediction = np.mean(y)
y_pred = np.array([initial_prediction] * len(y))
# Learning rate
lr = 0.1
# Boosting rounds
for round in range(1, 3): # Two rounds for demonstration
print(f"Boosting round: {round}")
#So at each boosting round, the residuals are computed, a weak learner is fit on them, and the predictions are updated to reduce the MSE loss.
residuals = y - y_pred
# 2. Fit residuals to the input features (in our simple case, fit means directly assigning)
# Normally, this step would involve training a decision tree.
tree_output = residuals
# 3. Update predictions (Predictor Function)
y_pred += lr * tree_output
# Print updated predictions
print(f"Updated predictions: {y_pred}")
# Final predictions
print(f"Final Predictions: {y_pred}")
'''
Boosting round: 1
Updated predictions: [2.8 2.9 3. 3.1 3.2]
Boosting round: 2
Updated predictions: [2.62 2.81 3. 3.19 3.38]
Final Predictions: [2.62 2.81 3. 3.19 3.38]
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment