-
Notifications
You must be signed in to change notification settings - Fork 2k
/
pyunit_metric_json_check.py
178 lines (170 loc) · 10.2 KB
/
pyunit_metric_json_check.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
# The purpose of this test is to detect a change in the _metric_json of MetricsBase objects. Many of the metric
# accessors require _metric_json to have a particular form.
import sys
sys.path.insert(1, "../../")
import h2o
def metric_json_check(ip, port):
h2o.init(ip, port)
df = h2o.import_frame(path=h2o.locate("smalldata/logreg/prostate.csv"))
# Regression metric json
reg_mod = h2o.gbm(y=df["CAPSULE"], x=df[3:], training_frame=df, distribution="gaussian")
reg_met = reg_mod.model_performance()
reg_metric_json_keys_have = reg_met._metric_json.keys()
reg_metric_json_keys_desired = [u'model_category',
u'description',
u'r2',
u'frame',
u'model_checksum',
u'MSE',
u'__meta',
u'scoring_time',
u'predictions',
u'model',
u'duration_in_ms',
u'frame_checksum',
u'mean_residual_deviance']
reg_metric_diff = list(set(reg_metric_json_keys_have) - set(reg_metric_json_keys_desired))
assert not reg_metric_diff, "There's a difference between the current ({0}) and the desired ({1}) regression " \
"metric json. The difference is {2}".format(reg_metric_json_keys_have,
reg_metric_json_keys_desired,
reg_metric_diff)
# Regression metric json (GLM)
reg_mod = h2o.glm(y=df["CAPSULE"], x=df[3:], training_frame=df, family="gaussian")
reg_met = reg_mod.model_performance()
reg_metric_json_keys_have = reg_met._metric_json.keys()
reg_metric_json_keys_desired = [u'model_category',
u'description',
u'r2',
u'residual_degrees_of_freedom',
u'frame',
u'model_checksum',
u'MSE',
u'__meta',
u'null_deviance',
u'scoring_time',
u'null_degrees_of_freedom',
u'predictions',
u'AIC',
u'model',
u'duration_in_ms',
u'frame_checksum',
u'residual_deviance',
u'mean_residual_deviance']
reg_metric_diff = list(set(reg_metric_json_keys_have) - set(reg_metric_json_keys_desired))
assert not reg_metric_diff, "There's a difference between the current ({0}) and the desired ({1}) glm-regression " \
"metric json. The difference is {2}".format(reg_metric_json_keys_have,
reg_metric_json_keys_desired,
reg_metric_diff)
# Binomial metric json
bin_mod = h2o.gbm(y=df["CAPSULE"].asfactor(), x=df[3:], training_frame=df, distribution="bernoulli")
bin_met = bin_mod.model_performance()
bin_metric_json_keys_have = bin_met._metric_json.keys()
bin_metric_json_keys_desired = [u'AUC',
u'Gini',
u'model_category',
u'description',
u'r2',
u'frame',
u'model_checksum',
u'MSE',
u'__meta',
u'logloss',
u'scoring_time',
u'thresholds_and_metric_scores',
u'predictions',
u'max_criteria_and_metric_scores',
u'model',
u'duration_in_ms',
u'frame_checksum',
u'domain']
bin_metric_diff = list(set(bin_metric_json_keys_have) - set(bin_metric_json_keys_desired))
assert not bin_metric_diff, "There's a difference between the current ({0}) and the desired ({1}) binomial " \
"metric json. The difference is {2}".format(bin_metric_json_keys_have,
bin_metric_json_keys_desired,
bin_metric_diff)
# Binomial metric json (GLM)
bin_mod = h2o.glm(y=df["CAPSULE"].asfactor(), x=df[3:], training_frame=df, family="binomial")
bin_met = bin_mod.model_performance()
bin_metric_json_keys_have = bin_met._metric_json.keys()
bin_metric_json_keys_desired = [u'frame',
u'residual_deviance',
u'max_criteria_and_metric_scores',
u'MSE',
u'frame_checksum',
u'AIC',
u'logloss',
u'Gini',
u'predictions',
u'AUC',
u'description',
u'model_checksum',
u'duration_in_ms',
u'model_category',
u'r2',
u'residual_degrees_of_freedom',
u'__meta',
u'null_deviance',
u'scoring_time',
u'null_degrees_of_freedom',
u'model',
u'thresholds_and_metric_scores',
u'domain']
bin_metric_diff = list(set(bin_metric_json_keys_have) - set(bin_metric_json_keys_desired))
assert not bin_metric_diff, "There's a difference between the current ({0}) and the desired ({1}) glm-binomial " \
"metric json. The difference is {2}".format(bin_metric_json_keys_have,
bin_metric_json_keys_desired,
bin_metric_diff)
# Multinomial metric json
df = h2o.import_frame(path=h2o.locate("smalldata/airlines/AirlinesTrain.csv.zip"))
myX = ["Origin", "Dest", "IsDepDelayed", "UniqueCarrier", "Distance", "fDayofMonth", "fDayOfWeek"]
myY = "fYear"
mul_mod = h2o.gbm(x=df[myX], y=df[myY], training_frame=df, distribution="multinomial")
mul_met = mul_mod.model_performance()
mul_metric_json_keys_have = mul_met._metric_json.keys()
mul_metric_json_keys_desired = [u'cm',
u'model_category',
u'description',
u'r2',
u'frame',
u'model_checksum',
u'MSE',
u'__meta',
u'logloss',
u'scoring_time',
u'predictions',
u'hit_ratio_table',
u'model',
u'duration_in_ms',
u'frame_checksum']
mul_metric_diff = list(set(mul_metric_json_keys_have) - set(mul_metric_json_keys_desired))
assert not mul_metric_diff, "There's a difference between the current ({0}) and the desired ({1}) multinomial " \
"metric json. The difference is {2}".format(mul_metric_json_keys_have,
mul_metric_json_keys_desired,
mul_metric_diff)
# Clustering metric json
df = h2o.import_frame(path=h2o.locate("smalldata/iris/iris.csv"))
clus_mod = h2o.kmeans(x=df[0:4], k=3, standardize=False)
clus_met = clus_mod.model_performance()
clus_metric_json_keys_have = clus_met._metric_json.keys()
clus_metric_json_keys_desired = [u'tot_withinss',
u'model_category',
u'description',
u'frame',
u'model_checksum',
u'MSE',
u'__meta',
u'scoring_time',
u'betweenss',
u'predictions',
u'totss',
u'model',
u'duration_in_ms',
u'frame_checksum',
u'centroid_stats']
clus_metric_diff = list(set(clus_metric_json_keys_have) - set(clus_metric_json_keys_desired))
assert not clus_metric_diff, "There's a difference between the current ({0}) and the desired ({1}) clustering " \
"metric json. The difference is {2}".format(clus_metric_json_keys_have,
clus_metric_json_keys_desired,
clus_metric_diff)
if __name__ == "__main__":
h2o.run_test(sys.argv, metric_json_check)