Introduction
Gradient Boosting helps us build strong prediction models by combining many simple models step-by-step to fix mistakes and improve accuracy.
Jump into concepts and practice - no test required
from sklearn.ensemble import GradientBoostingClassifier model = GradientBoostingClassifier(n_estimators=100, learning_rate=0.1, max_depth=3, random_state=42) model.fit(X_train, y_train) predictions = model.predict(X_test)
from sklearn.ensemble import GradientBoostingClassifier model = GradientBoostingClassifier(n_estimators=50, learning_rate=0.2, random_state=42) model.fit(X_train, y_train)
from sklearn.ensemble import GradientBoostingRegressor model = GradientBoostingRegressor(n_estimators=200, max_depth=4, random_state=42) model.fit(X_train, y_train)
from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split from sklearn.ensemble import GradientBoostingClassifier from sklearn.metrics import accuracy_score # Load data iris = load_iris() X, y = iris.data, iris.target # Split data X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42) # Create model model = GradientBoostingClassifier(n_estimators=100, learning_rate=0.1, max_depth=3, random_state=42) # Train model model.fit(X_train, y_train) # Predict predictions = model.predict(X_test) # Check accuracy accuracy = accuracy_score(y_test, predictions) print(f"Accuracy: {accuracy:.2f}")
from sklearn.ensemble import GradientBoostingRegressor X = [[1], [2], [3], [4]] y = [2, 4, 6, 8] gbm = GradientBoostingRegressor(n_estimators=100, learning_rate=0.1) gbm.fit(X, y) pred = gbm.predict([[5]]) print(round(pred[0], 1))
from sklearn.ensemble import GradientBoostingClassifier X = [[0], [1], [2]] y = [0, 1, 0] gbm = GradientBoostingClassifier(n_estimators='100') gbm.fit(X, y)