-
Notifications
You must be signed in to change notification settings - Fork 117
/
optbinning.py
188 lines (141 loc) · 6.86 KB
/
optbinning.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
176
177
178
179
180
181
182
183
184
185
186
187
188
import os
import sys
from optbinning import BinningProcess, Scorecard
from pandas import DataFrame
from sklearn_pandas import DataFrameMapper
from sklearn.linear_model import HuberRegressor, LinearRegression, LogisticRegression
from sklearn.tree import DecisionTreeClassifier, DecisionTreeRegressor
from sklearn2pmml.decoration import Alias
from sklearn2pmml.pipeline import PMMLPipeline
from sklearn2pmml.preprocessing import ExpressionTransformer
sys.path.append(os.path.abspath("../../../../pmml-sklearn/src/test/resources/"))
from common import *
datasets = []
if __name__ == "__main__":
if len(sys.argv) > 1:
datasets = (sys.argv[1]).split(",")
else:
datasets = ["Audit", "Auto", "Iris"]
def make_binning_process(cont_cols, cat_cols):
return BinningProcess(variable_names = (cont_cols + cat_cols), categorical_variables = cat_cols)
def build_audit(audit_df, classifier, name):
audit_X, audit_y = split_csv(audit_df)
cont_cols = ["Age", "Hours", "Income"]
cat_cols = ["Education", "Employment", "Gender", "Marital", "Occupation"]
audit_X = audit_X[cont_cols + cat_cols]
binning_process = make_binning_process(cont_cols, cat_cols)
pipeline = PMMLPipeline([
("binning_process", binning_process),
("classifier", classifier)
])
pipeline.fit(audit_X, audit_y)
store_pkl(pipeline, name)
adjusted = DataFrame(pipeline.predict(audit_X), columns = ["Adjusted"])
adjusted_proba = DataFrame(pipeline.predict_proba(audit_X), columns = ["probability(0)", "probability(1)"])
adjusted = pandas.concat((adjusted, adjusted_proba), axis = 1)
store_csv(adjusted, name)
def build_ob_audit(audit_df, classifier, name):
audit_X, audit_y = split_csv(audit_df)
mapper = DataFrameMapper([
(["Age"], [Alias(ExpressionTransformer("-999 if pandas.isnull(X[0]) else (-999 if (X[0] < 21 or X[0] > 65) else X[0])", dtype = int), name = "clean(Age)"), BinningProcess(variable_names = ["clean(Age)"], special_codes = [-999], binning_transform_params = {"clean(Age)" : {"metric" : "event_rate"}})]),
(["Hours"], BinningProcess(variable_names = ["Hours"], binning_transform_params = {"Hours" : {"metric" : "event_rate"}})),
(["Income"], BinningProcess(variable_names = ["Income"], binning_transform_params = {"Income" : {"metric" : "woe"}}))
])
pipeline = PMMLPipeline([
("mapper", mapper),
("classifier", classifier)
])
pipeline.fit(audit_X, audit_y)
store_pkl(pipeline, name)
adjusted = DataFrame(pipeline.predict(audit_X), columns = ["Adjusted"])
adjusted_proba = DataFrame(pipeline.predict_proba(audit_X), columns = ["probability(0)", "probability(1)"])
adjusted = pandas.concat((adjusted, adjusted_proba), axis = 1)
store_csv(adjusted, name)
def build_scorecard_audit(audit_df, name, **scorecard_params):
audit_X, audit_y = split_csv(audit_df)
cont_cols = ["Age", "Hours", "Income"]
cat_cols = ["Education", "Employment", "Gender", "Marital", "Occupation"]
audit_X = audit_X[cont_cols + cat_cols]
binning_process = make_binning_process(cont_cols, cat_cols)
estimator = LogisticRegression()
scorecard = Scorecard(binning_process = binning_process, estimator = estimator, **scorecard_params)
pipeline = PMMLPipeline([
("scorecard", scorecard)
])
pipeline.fit(audit_X, audit_y)
store_pkl(pipeline, name)
if scorecard.scaling_method is not None:
adjusted = DataFrame(scorecard.score(audit_X), columns = ["Adjusted"])
else:
adjusted = DataFrame(pipeline.predict(audit_X), columns = ["Adjusted"])
adjusted_proba = DataFrame(pipeline.predict_proba(audit_X), columns = ["probability(0)", "probability(1)"])
adjusted = pandas.concat((adjusted, adjusted_proba), axis = 1)
store_csv(adjusted, name)
if "Audit" in datasets:
audit_df = load_audit("Audit")
build_audit(audit_df, DecisionTreeClassifier(random_state = 13), "BinningProcessAudit")
build_ob_audit(audit_df, DecisionTreeClassifier(random_state = 13), "OptimalBinningAudit")
build_scorecard_audit(audit_df, "ScorecardAudit")
build_scorecard_audit(audit_df, "ScaledScorecardAudit", scaling_method = "pdo_odds", scaling_method_params = {"pdo" : 20, "odds" : 50, "scorecard_points" : 600})
audit_df = load_audit("AuditNA")
build_audit(audit_df, LogisticRegression(), "BinningProcessAuditNA")
build_ob_audit(audit_df, LogisticRegression(), "OptimalBinningAuditNA")
def build_iris(iris_df, classifier, name):
iris_X, iris_y = split_csv(iris_df)
cont_cols = list(iris_X.columns.values)
cat_cols = []
binning_process = make_binning_process(cont_cols, cat_cols)
pipeline = PMMLPipeline([
("binning_process", binning_process),
("classifier", classifier)
])
pipeline.fit(iris_X, iris_y)
store_pkl(pipeline, name)
species = DataFrame(pipeline.predict(iris_X), columns = ["Species"])
species_proba = DataFrame(pipeline.predict_proba(iris_X), columns = ["probability(setosa)", "probability(versicolor)", "probability(virginica)"])
species = pandas.concat((species, species_proba), axis = 1)
store_csv(species, name)
if "Iris" in datasets:
iris_df = load_iris("Iris")
build_iris(iris_df, LogisticRegression(), "BinningProcessIris")
def build_auto(auto_df, regressor, name):
auto_X, auto_y = split_csv(auto_df)
cont_cols = ["acceleration", "displacement", "horsepower", "weight"]
cat_cols = ["cylinders", "model_year", "origin"]
auto_X = auto_X[cont_cols + cat_cols]
binning_process = make_binning_process(cont_cols, cat_cols)
pipeline = PMMLPipeline([
("binning_process", binning_process),
("regressor", regressor)
])
pipeline.fit(auto_X, auto_y)
store_pkl(pipeline, name)
mpg = DataFrame(pipeline.predict(auto_X), columns = ["mpg"])
store_csv(mpg, name)
def build_scorecard_auto(auto_df, name, **scorecard_params):
auto_X, auto_y = split_csv(auto_df)
cont_cols = ["acceleration", "displacement", "horsepower", "weight"]
cat_cols = ["cylinders", "model_year", "origin"]
auto_X = auto_X[cont_cols + cat_cols]
binning_process = make_binning_process(cont_cols, cat_cols)
estimator = HuberRegressor()
scorecard = Scorecard(binning_process = binning_process, estimator = estimator, **scorecard_params)
pipeline = PMMLPipeline([
("scorecard", scorecard)
])
pipeline.fit(auto_X, auto_y)
store_pkl(pipeline, name)
if scorecard.scaling_method is not None:
mpg = DataFrame(scorecard.score(auto_X), columns = ["mpg"])
else:
mpg = DataFrame(pipeline.predict(auto_X), columns = ["mpg"])
store_csv(mpg, name)
if "Auto" in datasets:
auto_df = load_auto("Auto")
build_auto(auto_df, DecisionTreeRegressor(random_state = 13), "BinningProcessAuto")
build_scorecard_auto(auto_df, "ScorecardAuto")
build_scorecard_auto(auto_df, "ScaledScorecardAuto", scaling_method = "min_max", scaling_method_params = {"min" : 0, "max" : 100}, intercept_based = True, reverse_scorecard = True)
auto_df = load_auto("AutoNA")
build_auto(auto_df, LinearRegression(), "BinningProcessAutoNA")
build_scorecard_auto(auto_df, "ScorecardAutoNA")
build_scorecard_auto(auto_df, "ScaledScorecardAutoNA", scaling_method = "min_max", scaling_method_params = {"min" : 0, "max" : 100})