-
Notifications
You must be signed in to change notification settings - Fork 0
/
import_sklearn_mutimodels.py
175 lines (151 loc) · 7.37 KB
/
import_sklearn_mutimodels.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# -*-coding:utf-8 -*-
'''
@File : import_sklearn.py
@Time : 2022/02/28 16:47
@Author : Ryan Ma
@Version : 1.0
@Contact : [email protected]
@Desc : None
'''
import os
from pathlib import Path
import pandas as pd
from sasctl import pzmm
def import_sklearn_classification(project_name,
model_object,
model_name,
model_description,
model_algorithm,
model_owner,
target_event,
X_train,
y_train,
model_folder,
X_test=None,
y_test=None):
Path(model_folder).mkdir(parents=True, exist_ok=True)
files = os.listdir(model_folder)
for f in files:
if f.endswith(('.json', '.sas', '.py', '.pickle', '.zip')):
os.remove(os.path.join(model_folder, f))
# generate model pickle file
pzmm.PickleModel.pickle_trained_model(model_prefix=model_name,
trained_model=model_object,
pickle_path=model_folder,
is_h2o_model=False)
# Write input variable mapping to a json file
pzmm.JSONFiles.write_var_json(input_data=X_train,
is_input=True,
json_path=model_folder)
# Set output variables and assign an event threshold, then write output variable mapping
score_metrics = ["EM_CLASSIFICATION", "EM_EVENTPROBABILITY"]
output_df = pd.DataFrame(columns=score_metrics)
output_df[score_metrics[0]] = y_train.astype('str').unique()
output_df[score_metrics[1]] = 0.5 # Event threshold
pzmm.JSONFiles.write_var_json(input_data=output_df,
is_input=False,
json_path=model_folder)
# Write model properties to a json file
pzmm.JSONFiles.write_model_properties_json(model_name=model_name,
target_variable=y_train.name,
target_values=list(
y_train.unique()),
json_path=model_folder,
model_desc=model_description,
model_algorithm=model_algorithm,
modeler=model_owner)
# Write model metadata to a json file
pzmm.JSONFiles.write_file_metadata_json(model_prefix=model_name,
json_path=model_folder,
is_h2o_model=False)
# Calculate train predictions
if X_test is not None and y_test is not None:
train_proba = model_object.predict_proba(X_train)
test_proba = model_object.predict_proba(X_test)
train_res = pd.concat([y_train.reset_index(drop=True),
pd.Series(train_proba[:, 1])], axis=1)
test_res = pd.concat([y_test.reset_index(drop=True),
pd.Series(test_proba[:, 1])], axis=1)
# Calculate the model statistics and write to json files
pzmm.JSONFiles.calculate_model_statistics(target_value=target_event,
prob_value=0.5,
train_data=train_res,
test_data=test_res,
json_path=model_folder)
pzmm.ImportModel.import_model(model_files=model_folder,
model_prefix=model_name,
project=project_name,
input_data=X_train,
target_values=list(y_train.unique()),
score_metrics=score_metrics,
model_file_name=model_name + '.pickle',
predict_method=[
model_object.predict_proba, [float, float]],
overwrite_model=True)
pzmm.ScoreCode.score_code = ""
if __name__ == '__main__':
# import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.neural_network import MLPClassifier
# from lightgbm import LGBMClassifier
# import scikitplot as skplt
from sasctl import Session
raw = pd.read_csv('data/hmeq.csv')
col_y = 'BAD'
col_X = raw.drop(col_y, axis=1).columns
X = raw[col_X]
y = raw[col_y]
col_cat = X.columns[X.dtypes == 'O']
col_num = X.columns[X.dtypes != 'O']
X.loc[:, col_cat] = X[col_cat].fillna('X')
X.loc[:, col_num] = X[col_num].fillna(0)
le = LabelEncoder()
for c in col_cat:
X.loc[:, c] = le.fit_transform(X[c])
X = X.astype('float')
pd.concat([X, y], axis=1).to_csv('data/hmeq_imp_enc.csv', index=False)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.3, random_state=123)
dt = DecisionTreeClassifier()
dt.fit(X_train, y_train)
lr = LogisticRegression()
lr.fit(X_train, y_train)
rf = RandomForestClassifier()
rf.fit(X_train, y_train)
nn = MLPClassifier()
nn.fit(X_train, y_train)
# lgbm = LGBMClassifier()
# lgbm.fit(X_train, y_train)
viya_user = 'sasdemo1'
viya_pwd = 'Orion123'
viya_host = 'viya01'
viya_session = Session(viya_host, viya_user, viya_pwd, protocol='http').as_swat()
model_objects = [dt, lr, rf, nn]
model_names = ['DecisionTree', 'LogisticRegression',
'RandomForest', 'NeuralNetwork']
model_descriptions = ['Description for the ' +
m + ' model' for m in model_names]
model_algorithms = ['Decision Tree', 'Logistic Regression',
'Random Forest', 'Neural Network']
model_folders = ['model/' + m for m in model_names]
model_owner = 'Ryan Ma'
target_event = 1
project_name = 'HMEQ(Python) v20230626'
for (model_object, model_name, model_description, model_algorithm, model_folder) in zip(model_objects, model_names, model_descriptions, model_algorithms, model_folders):
import_sklearn_classification(project_name=project_name,
model_object=model_object,
model_name=model_name,
model_description=model_description,
model_algorithm=model_algorithm,
model_owner=model_owner,
target_event=target_event,
X_train=X_train,
y_train=y_train,
model_folder=model_folder,
X_test=X_test,
y_test=y_test)