-
Notifications
You must be signed in to change notification settings - Fork 0
/
gradientBoostingMachines.py
29 lines (20 loc) · 1.18 KB
/
gradientBoostingMachines.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_breast_cancer
# Gradient boosted decision trees on breast cancer dataset
cancer = load_breast_cancer()
X_train, X_test, y_train, y_test = train_test_split(cancer.data, cancer.target,random_state=0)
grbt = GradientBoostingClassifier(random_state=0)
grbt.fit(X_train, y_train)
print("Training set accuracy: {:.2f}".format(grbt.score(X_train, y_train)))
print("Test set accuracy: {:.2f}".format(grbt.score(X_test, y_test)))
# 100% accuracy on training set: Overfitting - We can lower the learning rate to reduce overfitting or use stronger
# pre-pruning (limit max depth)
grbt1 = GradientBoostingClassifier(random_state=0, max_depth=1)
grbt1.fit(X_train, y_train)
print("Training set accuracy: {:.2f}".format(grbt1.score(X_train, y_train)))
print("Test set accuracy: {:.2f}".format(grbt1.score(X_test, y_test)))
grbt2 = GradientBoostingClassifier(random_state=0, learning_rate=0.01)
grbt2.fit(X_train, y_train)
print("Training set accuracy: {:.2f}".format(grbt2.score(X_train, y_train)))
print("Test set accuracy: {:.2f}".format(grbt2.score(X_test, y_test)))