-
Notifications
You must be signed in to change notification settings - Fork 0
/
import_lgbm.py
74 lines (64 loc) · 2.13 KB
/
import_lgbm.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
71
72
73
74
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
import lightgbm as lgb
from sasctl import Session
from import_sklearn_mutimodels import import_sklearn_classification
# read LightGBM native model from file
bst = lgb.Booster(model_file='test_lgm.txt')
# bst.feature_name()
# score_X = np.array([[1, 2, 3]])
# bst.predict(score_X)
# make pesudo train/test data
n_col = bst.num_feature()
n_samp = 100
X = pd.DataFrame(np.random.normal(size=(n_samp, n_col)),
columns=bst.feature_name())
y = pd.Series(np.random.binomial(1, 0.5, size=n_samp), name='y')
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.3, random_state=123)
# convert LightGBM native model to sklearn model
lgbm = lgb.LGBMClassifier()
lgbm._Booster = bst
lgbm.fitted_ = True
lgbm._n_classes = 2
lgbm._n_features = bst.num_feature()
lgbm._le = lgb.sklearn._LGBMLabelEncoder().fit(y_train)
lgbm._class_map = dict(
zip(lgbm._le.classes_, lgbm._le.transform(lgbm._le.classes_)))
# compare native model predictions with sklearn model predictions
bst_pred = bst.predict(X_test, raw_score=True)
lgbm_pred = lgbm.predict(X_test, raw_score=True)
np.testing.assert_equal(bst_pred, lgbm_pred)
# Viya connection
user = 'sasdemo'
pwd = 'sas123'
host = '172.26.38.244'
viya_sess = Session(host, user, pwd, protocol='http')
viya_conn = viya_sess.as_swat()
# proj / model metadata
project_name = 'Python LightGBM Test 1022v2'
model_name = 'LightGBM1022v2'
model_description = 'A LightGBM model imported from file.'
model_algorithm = 'LightGBM'
model_owner = 'Ryan Ma'
target_event = 1
model_folder = 'model/LightGBM'
# import into MM
import_sklearn_classification(viya_conn,
model_folder,
project_name,
lgbm,
model_name,
model_description,
model_algorithm,
model_owner,
target_event,
X_train,
y_train,
X_test,
y_test)
# import pickle
# with open('model/LightGBM/LightGBM.pickle', 'rb') as pFile:
# clf = pickle.load(pFile)
# clf.predict_proba([[1,2,3]])