-
Notifications
You must be signed in to change notification settings - Fork 117
/
sklego.py
178 lines (147 loc) · 8.04 KB
/
sklego.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
import os
import sys
from mlxtend.preprocessing import DenseTransformer
from pandas import DataFrame
from sklearn.cluster import KMeans
from sklearn.compose import ColumnTransformer
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.ensemble import IsolationForest, RandomForestRegressor
from sklearn.linear_model import LinearRegression, LogisticRegression, PoissonRegressor
from sklearn.pipeline import _name_estimators
from sklearn.pipeline import FeatureUnion, Pipeline
from sklearn.preprocessing import OneHotEncoder
from sklearn.svm import LinearSVC, OneClassSVM
from sklearn.tree import DecisionTreeClassifier, DecisionTreeRegressor
from sklearn2pmml.decoration import CategoricalDomain, ContinuousDomain
from sklearn2pmml.pipeline import PMMLPipeline
from sklearn2pmml.util import Reshaper
from sklego.meta import EstimatorTransformer, OrdinalClassifier
from sklego.pipeline import DebugPipeline
from sklego.preprocessing import IdentityTransformer
sys.path.append(os.path.abspath("../../../../pmml-sklearn/src/test/resources/"))
from common import *
from main import *
def make_debug_pipeline(*steps):
return DebugPipeline(_name_estimators(steps))
def make_estimator_transformer(estimator, pmml_name, predict_func = "predict"):
estimator.pmml_name_ = pmml_name
return EstimatorTransformer(estimator, predict_func = predict_func)
def make_enhancer(estimator_transformer):
return FeatureUnion([
("identity", IdentityTransformer()),
("estimator_transformer", estimator_transformer)
])
def make_estimatortransformer_pipeline(cat_cols, cont_cols, transformer_estimator, final_estimator):
cat_encoder = Pipeline([
("encoder", OneHotEncoder()),
("formatter", DenseTransformer()),
("transformer", make_estimator_transformer(transformer_estimator, "transformer"))
])
cont_encoder = IdentityTransformer()
pipeline = PMMLPipeline([
("mapper", ColumnTransformer([
("cat", cat_encoder, cat_cols),
("cont", cont_encoder, cont_cols)
])),
("estimator", final_estimator)
])
return pipeline
def build_estimatortransformer_wheat(name):
wheat_df = load_wheat("Wheat")
wheat_X, wheat_y = split_csv(wheat_df)
pipeline = PMMLPipeline([
("decorator", ContinuousDomain()),
("cluster_labeler", make_enhancer(make_debug_pipeline(make_estimator_transformer(KMeans(n_clusters = 5, random_state = 13), "clusterLabeler"), OneHotEncoder(sparse_output = False)))),
("classifier", LogisticRegression(random_state = 13))
])
pipeline.fit(wheat_X, wheat_y)
store_pkl(pipeline, name)
variety = DataFrame(pipeline.predict(wheat_X), columns = ["Variety"])
variety_proba = DataFrame(pipeline.predict_proba(wheat_X), columns = ["probability(1)", "probability(2)", "probability(3)"])
variety = pandas.concat((variety, variety_proba), axis = 1)
store_csv(variety, name)
build_estimatortransformer_wheat("EstimatorTransformerWheat")
def build_estimatortransformer_audit(name):
audit_df = load_audit("Audit")
audit_X, audit_y = split_csv(audit_df)
pipeline = make_estimatortransformer_pipeline(["Employment", "Education", "Marital", "Occupation", "Gender"], ["Age", "Income", "Hours"], LogisticRegression(random_state = 13), DecisionTreeClassifier(min_samples_split = 10, random_state = 13))
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)
build_estimatortransformer_audit("EstimatorTransformerAudit")
def build_estimatortransformer_versicolor(outlier_detector_estimator, final_estimator, name):
versicolor_df = load_versicolor("Versicolor")
versicolor_X, versicolor_y = split_csv(versicolor_df)
pipeline = PMMLPipeline([
("decorator", ContinuousDomain()),
("outlier_detector", make_enhancer(make_debug_pipeline(make_estimator_transformer(outlier_detector_estimator, "outlierDetector"), OneHotEncoder(sparse_output = False)))),
("estimator", final_estimator)
])
pipeline.fit(versicolor_X, versicolor_y)
store_pkl(pipeline, name)
species = DataFrame(pipeline.predict(versicolor_X), columns = ["Species"])
species_proba = DataFrame(pipeline.predict_proba(versicolor_X), columns = ["probability(0)", "probability(1)"])
species = pandas.concat((species, species_proba), axis = 1)
store_csv(species, name)
build_estimatortransformer_versicolor(OneClassSVM(), LinearDiscriminantAnalysis(), "EstimatorTransformerVersicolor")
def build_estimatortransformer_iris(outlier_detector_estimator, final_estimator, name):
iris_df = load_iris("Iris")
iris_X, iris_y = split_csv(iris_df)
pipeline = PMMLPipeline([
("decorator", ContinuousDomain()),
("outlier_detector", make_enhancer(make_estimator_transformer(outlier_detector_estimator, "outlierDetector", predict_func = "decision_function"))),
("proba_estimator", make_enhancer(make_debug_pipeline(make_estimator_transformer(DecisionTreeClassifier(max_depth = 2, random_state = 13), "probaClassifier", predict_func = "predict_proba"), Reshaper(newshape = (150, 3))))),
("estimator", final_estimator)
])
pipeline.fit(iris_X, iris_y)
store_pkl(pipeline, name)
species = DataFrame(pipeline.predict(iris_X), columns = ["Species"])
store_csv(species, name)
build_estimatortransformer_iris(IsolationForest(n_estimators = 3, max_features = 2, contamination = 0.03, random_state = 13), LinearSVC(random_state = 13), "EstimatorTransformerIris")
def build_estimatortransformer_auto(name):
auto_df = load_auto("Auto")
auto_X, auto_y = split_csv(auto_df)
pipeline = make_estimatortransformer_pipeline(["cylinders", "model_year", "origin"], ["displacement", "horsepower", "weight", "acceleration"], LinearRegression(), DecisionTreeRegressor(min_samples_split = 25, random_state = 13))
pipeline.fit(auto_X, auto_y)
store_pkl(pipeline, name)
mpg = DataFrame(pipeline.predict(auto_X), columns = ["mpg"])
store_csv(mpg, name)
build_estimatortransformer_auto("EstimatorTransformerAuto")
def build_estimatortransformer_housing(name):
housing_df = load_housing("Housing")
housing_X, housing_y = split_csv(housing_df)
pipeline = PMMLPipeline([
("decorator", ContinuousDomain()),
("leaf_labeler", make_enhancer(make_debug_pipeline(make_estimator_transformer(DecisionTreeRegressor(max_depth = 3, min_samples_leaf = 20, random_state = 13), "leafLabeler", predict_func = "apply"), OneHotEncoder(sparse_output = False)))),
("estimator", LinearRegression())
])
pipeline.fit(housing_X, housing_y)
store_pkl(pipeline, name)
medv = DataFrame(pipeline.predict(housing_X), columns = ["MEDV"])
store_csv(medv, name)
build_estimatortransformer_housing("EstimatorTransformerHousing")
def build_estimatortransformer_visit(name):
visit_df = load_visit("Visit")
visit_X, visit_y = split_csv(visit_df)
pipeline = PMMLPipeline([
("mapper", ColumnTransformer([
("cat", OneHotEncoder(sparse_output = False), ["edlevel", "outwork", "female", "married", "kids", "self"]),
("cont", "passthrough", ["age", "hhninc", "educ"])
])),
("leaf_labeler", make_enhancer(make_debug_pipeline(make_estimator_transformer(RandomForestRegressor(n_estimators = 7, max_depth = 3, min_samples_leaf = 20, random_state = 13), "leafLabeler", predict_func = "apply"), Reshaper(newshape = (-1, 7)), OneHotEncoder(sparse_output = False)))),
("estimator", PoissonRegressor())
])
pipeline.fit(visit_X, visit_y)
store_pkl(pipeline, name)
docvis = DataFrame(pipeline.predict(visit_X), columns = ["docvis"])
store_csv(docvis, name)
build_estimatortransformer_visit("EstimatorTransformerVisit")
def build_ordinal_auto(classifier, name, **calibration_kwargs):
auto_df = load_auto("Auto")
build_auto_ordinal(auto_df, OrdinalClassifier(classifier, use_calibration = (len(calibration_kwargs) > 0), **calibration_kwargs), name, with_str_labels = False)
build_ordinal_auto(LogisticRegression(), "OrdinalClassifierAuto")
build_ordinal_auto(LogisticRegression(), "OrdinalClassifierIsotonicAuto", method = "isotonic", ensemble = False)
build_ordinal_auto(LogisticRegression(), "OrdinalClassifierSigmoidAuto", method = "sigmoid", ensemble = True)