Example to demonstrate use of tree.apply() method
This example trains several tree based ensemble methods and uses
them to transform the data into a high dimensional, sparse space.
The trains a linear model on this new feature space. The idea is
taken from:
Practical Lessons from Predicting Clicks on Ads at Facebook Junfeng Pan,
He Xinran, Ou Jin, Tianbing XU, Bo Liu, Tao Xu, Yanxin Shi, Antoine
Atallah, Ralf Herbrich, Stuart Bowers, Joaquin Quiñonero Candela
International Workshop on Data Mining for Online Advertising (ADKDD)
https://www.facebook.com/publications/329190253909587/
2015-07-27 22:38:50 +08:00
|
|
|
"""
|
|
|
|
|
===============================================
|
|
|
|
|
Feature transformations with ensembles of trees
|
|
|
|
|
===============================================
|
|
|
|
|
|
|
|
|
|
Transform your features into a higher dimensional, sparse space. Then
|
|
|
|
|
train a linear model on these features.
|
|
|
|
|
|
2015-08-15 02:04:46 +08:00
|
|
|
First fit an ensemble of trees (totally random trees, a random
|
|
|
|
|
forest, or gradient boosted trees) on the training set. Then each leaf
|
|
|
|
|
of each tree in the ensemble is assigned a fixed arbitrary feature
|
|
|
|
|
index in a new feature space. These leaf indices are then encoded in a
|
|
|
|
|
one-hot fashion.
|
Example to demonstrate use of tree.apply() method
This example trains several tree based ensemble methods and uses
them to transform the data into a high dimensional, sparse space.
The trains a linear model on this new feature space. The idea is
taken from:
Practical Lessons from Predicting Clicks on Ads at Facebook Junfeng Pan,
He Xinran, Ou Jin, Tianbing XU, Bo Liu, Tao Xu, Yanxin Shi, Antoine
Atallah, Ralf Herbrich, Stuart Bowers, Joaquin Quiñonero Candela
International Workshop on Data Mining for Online Advertising (ADKDD)
https://www.facebook.com/publications/329190253909587/
2015-07-27 22:38:50 +08:00
|
|
|
|
|
|
|
|
Each sample goes through the decisions of each tree of the ensemble
|
|
|
|
|
and ends up in one leaf per tree. The sample is encoded by setting
|
|
|
|
|
feature values for these leaves to 1 and the other feature values to 0.
|
|
|
|
|
|
|
|
|
|
The resulting transformer has then learned a supervised, sparse,
|
|
|
|
|
high-dimensional categorical embedding of the data.
|
2015-08-15 02:04:46 +08:00
|
|
|
|
Example to demonstrate use of tree.apply() method
This example trains several tree based ensemble methods and uses
them to transform the data into a high dimensional, sparse space.
The trains a linear model on this new feature space. The idea is
taken from:
Practical Lessons from Predicting Clicks on Ads at Facebook Junfeng Pan,
He Xinran, Ou Jin, Tianbing XU, Bo Liu, Tao Xu, Yanxin Shi, Antoine
Atallah, Ralf Herbrich, Stuart Bowers, Joaquin Quiñonero Candela
International Workshop on Data Mining for Online Advertising (ADKDD)
https://www.facebook.com/publications/329190253909587/
2015-07-27 22:38:50 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
# Author: Tim Head <betatim@gmail.com>
|
|
|
|
|
#
|
|
|
|
|
# License: BSD 3 clause
|
|
|
|
|
|
|
|
|
|
import numpy as np
|
|
|
|
|
np.random.seed(10)
|
|
|
|
|
|
|
|
|
|
import matplotlib.pyplot as plt
|
|
|
|
|
|
|
|
|
|
from sklearn.datasets import make_classification
|
|
|
|
|
from sklearn.linear_model import LogisticRegression
|
|
|
|
|
from sklearn.ensemble import (RandomTreesEmbedding, RandomForestClassifier,
|
|
|
|
|
GradientBoostingClassifier)
|
2018-06-21 17:27:41 +08:00
|
|
|
from sklearn.preprocessing import OneHotEncoder
|
2015-09-11 02:26:39 +08:00
|
|
|
from sklearn.model_selection import train_test_split
|
Example to demonstrate use of tree.apply() method
This example trains several tree based ensemble methods and uses
them to transform the data into a high dimensional, sparse space.
The trains a linear model on this new feature space. The idea is
taken from:
Practical Lessons from Predicting Clicks on Ads at Facebook Junfeng Pan,
He Xinran, Ou Jin, Tianbing XU, Bo Liu, Tao Xu, Yanxin Shi, Antoine
Atallah, Ralf Herbrich, Stuart Bowers, Joaquin Quiñonero Candela
International Workshop on Data Mining for Online Advertising (ADKDD)
https://www.facebook.com/publications/329190253909587/
2015-07-27 22:38:50 +08:00
|
|
|
from sklearn.metrics import roc_curve
|
2015-10-16 23:56:38 +08:00
|
|
|
from sklearn.pipeline import make_pipeline
|
Example to demonstrate use of tree.apply() method
This example trains several tree based ensemble methods and uses
them to transform the data into a high dimensional, sparse space.
The trains a linear model on this new feature space. The idea is
taken from:
Practical Lessons from Predicting Clicks on Ads at Facebook Junfeng Pan,
He Xinran, Ou Jin, Tianbing XU, Bo Liu, Tao Xu, Yanxin Shi, Antoine
Atallah, Ralf Herbrich, Stuart Bowers, Joaquin Quiñonero Candela
International Workshop on Data Mining for Online Advertising (ADKDD)
https://www.facebook.com/publications/329190253909587/
2015-07-27 22:38:50 +08:00
|
|
|
|
|
|
|
|
n_estimator = 10
|
|
|
|
|
X, y = make_classification(n_samples=80000)
|
|
|
|
|
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5)
|
2018-09-25 01:22:40 +08:00
|
|
|
|
Example to demonstrate use of tree.apply() method
This example trains several tree based ensemble methods and uses
them to transform the data into a high dimensional, sparse space.
The trains a linear model on this new feature space. The idea is
taken from:
Practical Lessons from Predicting Clicks on Ads at Facebook Junfeng Pan,
He Xinran, Ou Jin, Tianbing XU, Bo Liu, Tao Xu, Yanxin Shi, Antoine
Atallah, Ralf Herbrich, Stuart Bowers, Joaquin Quiñonero Candela
International Workshop on Data Mining for Online Advertising (ADKDD)
https://www.facebook.com/publications/329190253909587/
2015-07-27 22:38:50 +08:00
|
|
|
# It is important to train the ensemble of trees on a different subset
|
|
|
|
|
# of the training data than the linear regression model to avoid
|
|
|
|
|
# overfitting, in particular if the total number of leaves is
|
|
|
|
|
# similar to the number of training samples
|
2018-09-25 01:22:40 +08:00
|
|
|
X_train, X_train_lr, y_train, y_train_lr = train_test_split(
|
|
|
|
|
X_train, y_train, test_size=0.5)
|
Example to demonstrate use of tree.apply() method
This example trains several tree based ensemble methods and uses
them to transform the data into a high dimensional, sparse space.
The trains a linear model on this new feature space. The idea is
taken from:
Practical Lessons from Predicting Clicks on Ads at Facebook Junfeng Pan,
He Xinran, Ou Jin, Tianbing XU, Bo Liu, Tao Xu, Yanxin Shi, Antoine
Atallah, Ralf Herbrich, Stuart Bowers, Joaquin Quiñonero Candela
International Workshop on Data Mining for Online Advertising (ADKDD)
https://www.facebook.com/publications/329190253909587/
2015-07-27 22:38:50 +08:00
|
|
|
|
|
|
|
|
# Unsupervised transformation based on totally random trees
|
2015-10-16 23:56:38 +08:00
|
|
|
rt = RandomTreesEmbedding(max_depth=3, n_estimators=n_estimator,
|
2018-01-05 17:01:08 +08:00
|
|
|
random_state=0)
|
Example to demonstrate use of tree.apply() method
This example trains several tree based ensemble methods and uses
them to transform the data into a high dimensional, sparse space.
The trains a linear model on this new feature space. The idea is
taken from:
Practical Lessons from Predicting Clicks on Ads at Facebook Junfeng Pan,
He Xinran, Ou Jin, Tianbing XU, Bo Liu, Tao Xu, Yanxin Shi, Antoine
Atallah, Ralf Herbrich, Stuart Bowers, Joaquin Quiñonero Candela
International Workshop on Data Mining for Online Advertising (ADKDD)
https://www.facebook.com/publications/329190253909587/
2015-07-27 22:38:50 +08:00
|
|
|
|
2019-05-08 21:31:39 +08:00
|
|
|
rt_lm = LogisticRegression(max_iter=1000)
|
2015-10-16 23:56:38 +08:00
|
|
|
pipeline = make_pipeline(rt, rt_lm)
|
|
|
|
|
pipeline.fit(X_train, y_train)
|
|
|
|
|
y_pred_rt = pipeline.predict_proba(X_test)[:, 1]
|
Example to demonstrate use of tree.apply() method
This example trains several tree based ensemble methods and uses
them to transform the data into a high dimensional, sparse space.
The trains a linear model on this new feature space. The idea is
taken from:
Practical Lessons from Predicting Clicks on Ads at Facebook Junfeng Pan,
He Xinran, Ou Jin, Tianbing XU, Bo Liu, Tao Xu, Yanxin Shi, Antoine
Atallah, Ralf Herbrich, Stuart Bowers, Joaquin Quiñonero Candela
International Workshop on Data Mining for Online Advertising (ADKDD)
https://www.facebook.com/publications/329190253909587/
2015-07-27 22:38:50 +08:00
|
|
|
fpr_rt_lm, tpr_rt_lm, _ = roc_curve(y_test, y_pred_rt)
|
|
|
|
|
|
|
|
|
|
# Supervised transformation based on random forests
|
|
|
|
|
rf = RandomForestClassifier(max_depth=3, n_estimators=n_estimator)
|
2019-05-29 19:03:30 +08:00
|
|
|
rf_enc = OneHotEncoder()
|
2019-05-08 21:31:39 +08:00
|
|
|
rf_lm = LogisticRegression(max_iter=1000)
|
Example to demonstrate use of tree.apply() method
This example trains several tree based ensemble methods and uses
them to transform the data into a high dimensional, sparse space.
The trains a linear model on this new feature space. The idea is
taken from:
Practical Lessons from Predicting Clicks on Ads at Facebook Junfeng Pan,
He Xinran, Ou Jin, Tianbing XU, Bo Liu, Tao Xu, Yanxin Shi, Antoine
Atallah, Ralf Herbrich, Stuart Bowers, Joaquin Quiñonero Candela
International Workshop on Data Mining for Online Advertising (ADKDD)
https://www.facebook.com/publications/329190253909587/
2015-07-27 22:38:50 +08:00
|
|
|
rf.fit(X_train, y_train)
|
|
|
|
|
rf_enc.fit(rf.apply(X_train))
|
|
|
|
|
rf_lm.fit(rf_enc.transform(rf.apply(X_train_lr)), y_train_lr)
|
|
|
|
|
|
|
|
|
|
y_pred_rf_lm = rf_lm.predict_proba(rf_enc.transform(rf.apply(X_test)))[:, 1]
|
|
|
|
|
fpr_rf_lm, tpr_rf_lm, _ = roc_curve(y_test, y_pred_rf_lm)
|
|
|
|
|
|
2018-09-25 01:22:40 +08:00
|
|
|
# Supervised transformation based on gradient boosted trees
|
Example to demonstrate use of tree.apply() method
This example trains several tree based ensemble methods and uses
them to transform the data into a high dimensional, sparse space.
The trains a linear model on this new feature space. The idea is
taken from:
Practical Lessons from Predicting Clicks on Ads at Facebook Junfeng Pan,
He Xinran, Ou Jin, Tianbing XU, Bo Liu, Tao Xu, Yanxin Shi, Antoine
Atallah, Ralf Herbrich, Stuart Bowers, Joaquin Quiñonero Candela
International Workshop on Data Mining for Online Advertising (ADKDD)
https://www.facebook.com/publications/329190253909587/
2015-07-27 22:38:50 +08:00
|
|
|
grd = GradientBoostingClassifier(n_estimators=n_estimator)
|
2019-05-29 19:03:30 +08:00
|
|
|
grd_enc = OneHotEncoder()
|
2019-05-08 21:31:39 +08:00
|
|
|
grd_lm = LogisticRegression(max_iter=1000)
|
Example to demonstrate use of tree.apply() method
This example trains several tree based ensemble methods and uses
them to transform the data into a high dimensional, sparse space.
The trains a linear model on this new feature space. The idea is
taken from:
Practical Lessons from Predicting Clicks on Ads at Facebook Junfeng Pan,
He Xinran, Ou Jin, Tianbing XU, Bo Liu, Tao Xu, Yanxin Shi, Antoine
Atallah, Ralf Herbrich, Stuart Bowers, Joaquin Quiñonero Candela
International Workshop on Data Mining for Online Advertising (ADKDD)
https://www.facebook.com/publications/329190253909587/
2015-07-27 22:38:50 +08:00
|
|
|
grd.fit(X_train, y_train)
|
2015-09-08 18:14:03 +08:00
|
|
|
grd_enc.fit(grd.apply(X_train)[:, :, 0])
|
|
|
|
|
grd_lm.fit(grd_enc.transform(grd.apply(X_train_lr)[:, :, 0]), y_train_lr)
|
Example to demonstrate use of tree.apply() method
This example trains several tree based ensemble methods and uses
them to transform the data into a high dimensional, sparse space.
The trains a linear model on this new feature space. The idea is
taken from:
Practical Lessons from Predicting Clicks on Ads at Facebook Junfeng Pan,
He Xinran, Ou Jin, Tianbing XU, Bo Liu, Tao Xu, Yanxin Shi, Antoine
Atallah, Ralf Herbrich, Stuart Bowers, Joaquin Quiñonero Candela
International Workshop on Data Mining for Online Advertising (ADKDD)
https://www.facebook.com/publications/329190253909587/
2015-07-27 22:38:50 +08:00
|
|
|
|
|
|
|
|
y_pred_grd_lm = grd_lm.predict_proba(
|
2015-09-08 18:14:03 +08:00
|
|
|
grd_enc.transform(grd.apply(X_test)[:, :, 0]))[:, 1]
|
Example to demonstrate use of tree.apply() method
This example trains several tree based ensemble methods and uses
them to transform the data into a high dimensional, sparse space.
The trains a linear model on this new feature space. The idea is
taken from:
Practical Lessons from Predicting Clicks on Ads at Facebook Junfeng Pan,
He Xinran, Ou Jin, Tianbing XU, Bo Liu, Tao Xu, Yanxin Shi, Antoine
Atallah, Ralf Herbrich, Stuart Bowers, Joaquin Quiñonero Candela
International Workshop on Data Mining for Online Advertising (ADKDD)
https://www.facebook.com/publications/329190253909587/
2015-07-27 22:38:50 +08:00
|
|
|
fpr_grd_lm, tpr_grd_lm, _ = roc_curve(y_test, y_pred_grd_lm)
|
|
|
|
|
|
|
|
|
|
# The gradient boosted model by itself
|
|
|
|
|
y_pred_grd = grd.predict_proba(X_test)[:, 1]
|
|
|
|
|
fpr_grd, tpr_grd, _ = roc_curve(y_test, y_pred_grd)
|
|
|
|
|
|
|
|
|
|
# The random forest model by itself
|
|
|
|
|
y_pred_rf = rf.predict_proba(X_test)[:, 1]
|
|
|
|
|
fpr_rf, tpr_rf, _ = roc_curve(y_test, y_pred_rf)
|
|
|
|
|
|
2015-09-08 18:14:03 +08:00
|
|
|
plt.figure(1)
|
Example to demonstrate use of tree.apply() method
This example trains several tree based ensemble methods and uses
them to transform the data into a high dimensional, sparse space.
The trains a linear model on this new feature space. The idea is
taken from:
Practical Lessons from Predicting Clicks on Ads at Facebook Junfeng Pan,
He Xinran, Ou Jin, Tianbing XU, Bo Liu, Tao Xu, Yanxin Shi, Antoine
Atallah, Ralf Herbrich, Stuart Bowers, Joaquin Quiñonero Candela
International Workshop on Data Mining for Online Advertising (ADKDD)
https://www.facebook.com/publications/329190253909587/
2015-07-27 22:38:50 +08:00
|
|
|
plt.plot([0, 1], [0, 1], 'k--')
|
|
|
|
|
plt.plot(fpr_rt_lm, tpr_rt_lm, label='RT + LR')
|
|
|
|
|
plt.plot(fpr_rf, tpr_rf, label='RF')
|
|
|
|
|
plt.plot(fpr_rf_lm, tpr_rf_lm, label='RF + LR')
|
|
|
|
|
plt.plot(fpr_grd, tpr_grd, label='GBT')
|
|
|
|
|
plt.plot(fpr_grd_lm, tpr_grd_lm, label='GBT + LR')
|
|
|
|
|
plt.xlabel('False positive rate')
|
|
|
|
|
plt.ylabel('True positive rate')
|
|
|
|
|
plt.title('ROC curve')
|
|
|
|
|
plt.legend(loc='best')
|
|
|
|
|
plt.show()
|
2015-09-08 18:14:03 +08:00
|
|
|
|
|
|
|
|
plt.figure(2)
|
|
|
|
|
plt.xlim(0, 0.2)
|
|
|
|
|
plt.ylim(0.8, 1)
|
|
|
|
|
plt.plot([0, 1], [0, 1], 'k--')
|
|
|
|
|
plt.plot(fpr_rt_lm, tpr_rt_lm, label='RT + LR')
|
|
|
|
|
plt.plot(fpr_rf, tpr_rf, label='RF')
|
|
|
|
|
plt.plot(fpr_rf_lm, tpr_rf_lm, label='RF + LR')
|
|
|
|
|
plt.plot(fpr_grd, tpr_grd, label='GBT')
|
|
|
|
|
plt.plot(fpr_grd_lm, tpr_grd_lm, label='GBT + LR')
|
|
|
|
|
plt.xlabel('False positive rate')
|
|
|
|
|
plt.ylabel('True positive rate')
|
|
|
|
|
plt.title('ROC curve (zoomed in at top left)')
|
|
|
|
|
plt.legend(loc='best')
|
2015-09-11 02:26:39 +08:00
|
|
|
plt.show()
|