This repository has been archived by the owner on Dec 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Neptune-XGBoost.py
70 lines (50 loc) · 1.76 KB
/
Neptune-XGBoost.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# XGBoost + Neptune integration
# Before you start
## Install dependencies
import neptune
import pandas as pd
import xgboost as xgb
from sklearn.datasets import load_boston
from sklearn.model_selection import train_test_split
from neptunecontrib.monitoring.xgboost import neptune_callback
# Set project
neptune.init('shared/XGBoost-integration',
api_token='ANONYMOUS')
# Prepare data for XGBoost training
boston = load_boston()
data = pd.DataFrame(boston.data)
data.columns = boston.feature_names
data['PRICE'] = boston.target
X, y = data.iloc[:,:-1], data.iloc[:,-1]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=102030)
dtrain = xgb.DMatrix(X_train, label=y_train)
dtest = xgb.DMatrix(X_test, label=y_test)
# Prepare params
params = {'max_depth': 5,
'eta': 0.5,
'gamma': 0.1,
'subsample': 1,
'lambda': 1,
'alpha': 0.35,
'objective': 'reg:squarederror',
'eval_metric': ['mae', 'rmse']}
watchlist = [(dtest, 'eval'), (dtrain, 'train')]
num_round = 20
# Train model using `xgb.train()`
neptune.create_experiment(name='xgb', tags=['train'], params=params)
xgb.train(params, dtrain, num_round, watchlist,
callbacks=[neptune_callback()])
neptune.stop()
# Train model using `xgb.cv()`
neptune.create_experiment(name='xgb', tags=['cv'], params=params)
xgb.cv(params, dtrain, num_boost_round=num_round, nfold=7,
callbacks=[neptune_callback()])
neptune.stop()
# Train model using `sklearn` API
neptune.create_experiment(name='xgb', tags=['sklearn'], params=params)
reg = xgb.XGBRegressor(**params)
reg.fit(X_train, y_train,
eval_metric=['mae', 'rmse'],
eval_set=[(X_test, y_test)],
callbacks=[neptune_callback()])
neptune.stop()