Gradient Boosting

 Gradient Boosting:

Gradient boosting is also based on sequential ensemble learning. The difference in this type of boosting it that the weights for misclassified outcomes are not incremented, instead, Gradient boosting method tries to optimize the loss function of the previous learner by adding a new model that adds weak learners in order to reduce the loss function.

Gradient boosting involves three elements:

  1. Loss function: A loss function to be optimized
  2. Weak learner: to make predictions
  3. Additive model: to add weak learners to minimize the loss function.
Gradient boosting algorithm is a greedy algorithm and can over fit, a training data set. Improve the performance of algorithm by reducing over fitting.
  • Tree constraints
  • Shrinkage
  • Random sampling
  • Penalized learning
Like Ada boost, Gradient boosting can also be used for both classification and regression problems.
  • Pros: It iteratively correct the mistakes of the weak classifier and  improve accuracy by combining weak learners.It gives better accuracy in most of the cases.
  • Cons: Hyper parameter tuning and time consuming, requires large space.
Gradient Boosting in practice:
Apply Gradient boosting algorithm on boston house price prediction data set:
import numpy as np
import pandas as pd
import data set:
from sklearn import datasets
df=datasets.load_boston()
x=pd.DataFrame(df.data, columns=df.feature_names)
y=pd.Series(df.target)
x.head()
CRIMZNINDUSCHASNOXRMAGEDISRADTAXPTRATIOBLSTAT
00.0063218.02.310.00.5386.57565.24.09001.0296.015.3396.904.98
10.027310.07.070.00.4696.42178.94.96712.0242.017.8396.909.14
20.027290.07.070.00.4697.18561.14.96712.0242.017.8392.834.03
30.032370.02.180.00.4586.99845.86.06223.0222.018.7394.632.94
40.069050.02.180.00.4587.14754.26.06223.0222.018.7396.905.33
y.head()
0    24.0
1    21.6
2    34.7
3    33.4
4    36.2
dtype: float64
Split data set into Train and Test:
from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.20)
Implementing Gradient boosting :
from sklearn.ensemble import GradientBoostingRegressor
gradient=GradientBoostingRegressor(max_depth=2,n_estimators=3,learning_rate=1.0)
model=gradient.fit(x_train,y_train)
y_pred=model.predict(x_test)
Model Evaluation:
from sklearn.metrics import r2_score
Accuracy=r2_score(y_test,y_pred)
Accuracy
0.75777778
Hyper parameter tuning:
from sklearn.model_selection import GridSearchCV
LR={'learning_rate':[0.15,0.10,0.05],'n_estimators':[100,150,200,250]}
tuning=GridSearchCV(estimator=GradientBoostingRegressor(),param_grid=LR, scoring='r2')
tuning.fit(x_train,y_train)
tuning.best_params_,tuning.best_score_
({'learning_rate': 0.15, 'n_estimators': 250}, 0.8750218539097616)



Comments