Gradient Boosting in Machine Learning
Machine Learning courses with 100+ Real-time projects Start Now!!
Program 1
# Step 1: Import libraries
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.ensemble import GradientBoostingRegressor
from sklearn.metrics import mean_squared_error, r2_score
# Step 2: Load dataset from CSV
df = pd.read_csv("D://scikit_data/gbm/mock_california_housing.csv") # Make sure this file is in your working directory
# Step 3: Separate features and target
X = df.drop("MedHouseValue", axis=1) # Features
y = df["MedHouseValue"] # Target
# Step 4: Split the data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Step 5: Initialize and train the Gradient Boosting model
model = GradientBoostingRegressor(n_estimators=100,learning_rate=0.1,max_depth=3,random_state=42)
model.fit(X_train, y_train)
# Step 6: Predict on test set
y_pred = model.predict(X_test)
# Step 7: Evaluate the model
rmse = mean_squared_error(y_test, y_pred, squared=False)
r2 = r2_score(y_test, y_pred)
print("✅ Gradient Boosting Regressor Results:")
print(f"RMSE: {rmse:.2f}")
print(f"R² Score: {r2:.2f}")
Did you like this article? If Yes, please give DataFlair 5 Stars on Google

