-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtrain.py
More file actions
67 lines (51 loc) · 1.99 KB
/
train.py
File metadata and controls
67 lines (51 loc) · 1.99 KB
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
import argparse
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, log_loss
import xgboost as xgb
import matplotlib as mpl
import mlflow
import mlflow.xgboost
mpl.use('Agg')
def parse_args():
parser = argparse.ArgumentParser(description='XGBoost example')
parser.add_argument('--learning-rate', type=float, default=0.3,
help='learning rate to update step size at each boosting step (default: 0.3)')
parser.add_argument('--colsample-bytree', type=float, default=1.0,
help='subsample ratio of columns when constructing each tree (default: 1.0)')
parser.add_argument('--subsample', type=float, default=1.0,
help='subsample ratio of the training instances (default: 1.0)')
return parser.parse_args()
def main():
# parse command-line arguments
args = parse_args()
# prepare train and test data
iris = datasets.load_iris()
X = iris.data
y = iris.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
dtrain = xgb.DMatrix(X_train, label=y_train)
dtest = xgb.DMatrix(X_test, label=y_test)
# enable auto logging
mlflow.xgboost.autolog()
with mlflow.start_run():
# train model
params = {
'objective': 'multi:softprob',
'num_class': 3,
'learning_rate': args.learning_rate,
'eval_metric': 'mlogloss',
'colsample_bytree': args.colsample_bytree,
'subsample': args.subsample,
'seed': 42,
}
model = xgb.train(params, dtrain, evals=[(dtrain, 'train')])
# evaluate model
y_proba = model.predict(dtest)
y_pred = y_proba.argmax(axis=1)
loss = log_loss(y_test, y_proba)
acc = accuracy_score(y_test, y_pred)
# log metrics
mlflow.log_metrics({'log_loss': loss, 'accuracy': acc})
if __name__ == '__main__':
main()