Skip to content

Commit a12a00b

Browse files
author
xuming06
committed
add btree. xuming 20180112
1 parent 203c3d2 commit a12a00b

1 file changed

Lines changed: 79 additions & 0 deletions

File tree

11scikit-learn/btree.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# -*- coding: utf-8 -*-
2+
# Author: XuMing <[email protected]>
3+
# Data: 18/1/12
4+
# Brief:
5+
6+
#!/usr/bin/python
7+
'''
8+
Created on 1 Apr 2015
9+
@author: Jamie Hall
10+
'''
11+
import pickle
12+
import xgboost as xgb
13+
14+
import numpy as np
15+
from sklearn.model_selection import KFold, train_test_split, GridSearchCV
16+
from sklearn.metrics import confusion_matrix, mean_squared_error
17+
from sklearn.datasets import load_iris, load_digits, load_boston
18+
19+
rng = np.random.RandomState(31337)
20+
21+
print("Zeros and Ones from the Digits dataset: binary classification")
22+
digits = load_digits(2)
23+
y = digits['target']
24+
X = digits['data']
25+
kf = KFold(n_splits=2, shuffle=True, random_state=rng)
26+
for train_index, test_index in kf.split(X):
27+
xgb_model = xgb.XGBClassifier().fit(X[train_index], y[train_index])
28+
predictions = xgb_model.predict(X[test_index])
29+
actuals = y[test_index]
30+
print(confusion_matrix(actuals, predictions))
31+
32+
print("Iris: multiclass classification")
33+
iris = load_iris()
34+
y = iris['target']
35+
X = iris['data']
36+
kf = KFold(n_splits=2, shuffle=True, random_state=rng)
37+
for train_index, test_index in kf.split(X):
38+
xgb_model = xgb.XGBClassifier().fit(X[train_index], y[train_index])
39+
predictions = xgb_model.predict(X[test_index])
40+
actuals = y[test_index]
41+
print(confusion_matrix(actuals, predictions))
42+
43+
print("Boston Housing: regression")
44+
boston = load_boston()
45+
y = boston['target']
46+
X = boston['data']
47+
kf = KFold(n_splits=2, shuffle=True, random_state=rng)
48+
for train_index, test_index in kf.split(X):
49+
xgb_model = xgb.XGBRegressor().fit(X[train_index], y[train_index])
50+
predictions = xgb_model.predict(X[test_index])
51+
actuals = y[test_index]
52+
print(mean_squared_error(actuals, predictions))
53+
54+
print("Parameter optimization")
55+
y = boston['target']
56+
X = boston['data']
57+
xgb_model = xgb.XGBRegressor()
58+
clf = GridSearchCV(xgb_model,
59+
{'max_depth': [2,4,6],
60+
'n_estimators': [50,100,200]}, verbose=1)
61+
clf.fit(X,y)
62+
print(clf.best_score_)
63+
print(clf.best_params_)
64+
65+
# The sklearn API models are picklable
66+
print("Pickling sklearn API models")
67+
# must open in binary format to pickle
68+
pickle.dump(clf, open("best_boston.pkl", "wb"))
69+
clf2 = pickle.load(open("best_boston.pkl", "rb"))
70+
print(np.allclose(clf.predict(X), clf2.predict(X)))
71+
72+
# Early-stopping
73+
74+
X = digits['data']
75+
y = digits['target']
76+
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
77+
clf = xgb.XGBClassifier()
78+
clf.fit(X_train, y_train, early_stopping_rounds=10, eval_metric="auc",
79+
eval_set=[(X_test, y_test)])

0 commit comments

Comments
 (0)