-
Notifications
You must be signed in to change notification settings - Fork 0
/
postprocessing.py
290 lines (236 loc) · 9.67 KB
/
postprocessing.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
"""Wrappers for spark.ml models implemented in scala lib
Code from pyspark.ml.feature was used as a template.
"""
from pyspark import keyword_only
from pyspark.ml.util import JavaMLReadable, JavaMLWritable
from pyspark.ml.common import inherit_doc
from pyspark.ml.wrapper import JavaModel, JavaEstimator
from pyspark.ml.param.shared import Param, Params, HasInputCol, TypeConverters
from .params import (
HasNoise,
HasLabels,
HasRandom,
HasEpsilon,
HasRankCol,
HasSampling,
HasGroupCols,
HasOutputCol,
ParamsHelper,
HasNumClasses,
HasPriorValues,
)
@inherit_doc
class ScoreEqualizeTransformer(
JavaEstimator,
HasNoise,
HasRandom,
HasEpsilon,
HasInputCol,
HasSampling,
HasOutputCol,
HasGroupCols,
ParamsHelper,
JavaMLReadable,
JavaMLWritable,
):
"""Python wrapper for Spark ML estimator, jvm class ScoreEqualizerEstimator.
Implements the fit method of python :class:`postprocessing.ScoreEqualizeTransformer`.
Use :meth:`fit` to produce trained equalizer transformer, using sample of input dataframe.
Fitting involves materialization of input data, so you should persist input dataframe before calling :meth:`fit`.
This implementation supports stratification: for different groups of records different equalizers could be fitted
using ``groupColumns`` parameter. Sample of each group will be used, according to ``sampleSize`` parameter.
``inputCol`` data is considered as invalid if column value is NULL or NaN.
On fit stage invalid data is filtered out, on transform stage invalid input produces NULL in output.
A model could not be fitted if no valid data is found in ``inputCol``.
On transform stage, an absence of fitted model is considered as invalid input.
>>> from spark_ml.postprocessing import ScoreEqualizeTransformer
>>> df = spark.createDataFrame( ... )
>>> model = ScoreEqualizeTransformer(inputCol="score_raw_train", groupColumns=["uid_type", "category"]).fit(df)
>>> out_df = model.setInputCol("score_raw").setOutputCol("score").transform(df)
"""
_fqn = "com.github.vasnake.spark.ml.estimator.ScoreEqualizerEstimator"
numBins = Param(
Params._dummy(), "numBins", "number of bins for spline interpolator", typeConverter=TypeConverters.toInt
)
@keyword_only
def __init__(
self,
numBins=None,
inputCol=None,
epsValue=None,
outputCol=None,
noiseValue=None,
sampleSize=None,
randomValue=None,
groupColumns=None,
sampleRandomSeed=None,
):
super(ScoreEqualizeTransformer, self).__init__()
self._java_obj = self._new_java_obj(self._fqn, self.uid)
self._setDefault(
numBins=10000,
epsValue=1e-3,
noiseValue=1e-4,
sampleSize=100000,
groupColumns=[],
)
self._setParams(**self._input_kwargs)
@keyword_only
def setParams(
self,
numBins=None,
inputCol=None,
epsValue=None,
outputCol=None,
noiseValue=None,
sampleSize=None,
randomValue=None,
groupColumns=None,
sampleRandomSeed=None,
):
return self._setParams(**self._input_kwargs)
def setNumBins(self, value):
return self._set(numBins=value)
def getNumBins(self):
return self.getOrDefault(self.numBins)
def _create_model(self, java_model):
return ScoreEqualizeTransformerModel(java_model)
class ScoreEqualizeTransformerModel(JavaModel, HasOutputCol, HasInputCol, HasGroupCols, JavaMLReadable, JavaMLWritable):
"""Spark ML model fitted by ScoreEqualizeTransformer.
Python wrapper for jvm class ScoreEqualizerModel with implementation of the python
:class:`postprocessing.ScoreEqualizeTransformer` transform method.
"""
_fqn = "com.github.vasnake.spark.ml.model.ScoreEqualizerModel"
@inherit_doc
class NEPriorClassProbaTransformer(
JavaEstimator,
HasInputCol,
HasSampling,
HasOutputCol,
HasGroupCols,
HasPriorValues,
ParamsHelper,
JavaMLReadable,
JavaMLWritable,
):
"""Python wrapper for Spark ML estimator, jvm class NEPriorClassProbaEstimator.
Implements the fit method of the python :class:`postprocessing.NEPriorClassProbaTransformer`.
Use :meth:`fit` to produce trained transformer, using sample of input dataframe.
Fitting involves materialization of input data, so you should persist input dataframe before calling :meth:`fit`.
This implementation supports stratification: for different groups of records, different transformers could be fitted
using ``groupColumns`` parameter. Sample of each group will be used, according to ``sampleSize`` parameter.
``inputCol`` data is considered as invalid if column value is NULL or array ``size != len(priorValues)`` or array
contains NULL or NaN or negative values, or if ``sum(array) == 0``.
On fit stage invalid data is filtered out, on transform stage invalid input produces NULL in output.
A model could not be fitted if no valid rows is found in ``inputCol``.
On transform stage, an absence of fitted model produces NULL output values.
>>> from spark_ml.postprocessing import NEPriorClassProbaTransformer
>>> df = spark.createDataFrame( ... )
>>> model = NEPriorClassProbaTransformer(inputCol="scores_raw", priorValues=[1.0, 1.0]).fit(df)
>>> out_df = model.setOutputCol("scores_trf").transform(df)
"""
_fqn = "com.github.vasnake.spark.ml.estimator.NEPriorClassProbaEstimator"
@keyword_only
def __init__(
self,
inputCol=None,
outputCol=None,
sampleSize=None,
priorValues=None,
groupColumns=None,
sampleRandomSeed=None,
):
super(NEPriorClassProbaTransformer, self).__init__()
self._java_obj = self._new_java_obj(self._fqn, self.uid)
self._setDefault(sampleSize=100000, groupColumns=[])
self._setParams(**self._input_kwargs)
@keyword_only
def setParams(
self,
inputCol=None,
outputCol=None,
sampleSize=None,
priorValues=None,
groupColumns=None,
sampleRandomSeed=None,
):
return self._setParams(**self._input_kwargs)
def _create_model(self, java_model):
return NEPriorClassProbaTransformerModel(java_model)
class NEPriorClassProbaTransformerModel(
JavaModel, HasInputCol, HasOutputCol, HasGroupCols, JavaMLReadable, JavaMLWritable
):
"""Spark ML model fitted by NEPriorClassProbaTransformer.
Python wrapper for jvm class NEPriorClassProbaModel with implementation of the python
:class:`postprocessing.NEPriorClassProbaTransformer` transform method.
"""
_fqn = "com.github.vasnake.spark.ml.model.NEPriorClassProbaModel"
@inherit_doc
class ScoreQuantileThresholdTransformer(
JavaEstimator,
HasLabels,
HasRankCol,
HasInputCol,
HasSampling,
HasGroupCols,
HasOutputCol,
HasPriorValues,
ParamsHelper,
JavaMLReadable,
JavaMLWritable,
):
"""Python wrapper for Spark ML estimator, jvm class ScoreQuantileThresholdEstimator.
Implements the fit method of python :class:`postprocessing.ScoreQuantileThresholdTransformer`.
Use :meth:`fit` to produce trained transformer, using sample of input dataframe.
Fitting involves materialization of input data, so you should persist input dataframe before calling :meth:`fit`.
This implementation supports stratification: for different groups of records different transformers could be fitted
using ``groupColumns`` parameter. Sample of each group will be used, according to ``sampleSize`` parameter.
``inputCol`` data is considered as invalid if column value is NULL or NaN.
On fit stage invalid data is filtered out, on transform stage invalid input produces NULL in output.
A model could not be fitted if no valid data is found in ``inputCol``.
On transform stage, an absence of fitted model is considered as invalid input.
>>> from spark_ml.postprocessing import ScoreQuantileThresholdTransformer
>>> df = spark.createDataFrame( ... )
>>> model = ScoreQuantileThresholdTransformer(inputCol="score_raw", priorValues=[1.0, 1.0]).fit(df)
>>> out_df = model.setOutputCol("category").setRankCol("score").transform(df)
"""
_fqn = "com.github.vasnake.spark.ml.estimator.ScoreQuantileThresholdEstimator"
@keyword_only
def __init__(
self,
labels=None,
rankCol=None,
inputCol=None,
outputCol=None,
sampleSize=None,
priorValues=None,
groupColumns=None,
sampleRandomSeed=None,
):
super(ScoreQuantileThresholdTransformer, self).__init__()
self._java_obj = self._new_java_obj(self._fqn, self.uid)
self._setDefault(sampleSize=100000, groupColumns=[])
self._setParams(**self._input_kwargs)
@keyword_only
def setParams(
self,
labels=None,
rankCol=None,
inputCol=None,
outputCol=None,
sampleSize=None,
priorValues=None,
groupColumns=None,
sampleRandomSeed=None,
):
return self._setParams(**self._input_kwargs)
def _create_model(self, java_model):
return ScoreQuantileThresholdTransformerModel(java_model)
class ScoreQuantileThresholdTransformerModel(
JavaModel, HasInputCol, HasOutputCol, HasRankCol, HasGroupCols, JavaMLReadable, JavaMLWritable
):
"""Spark ML model fitted by ScoreQuantileThresholdTransformer.
Python wrapper for jvm class ScoreQuantileThresholdModel with implementation of the python
:class:`postprocessing.ScoreQuantileThresholdTransformer` transform method.
"""
_fqn = "com.github.vasnake.spark.ml.model.ScoreQuantileThresholdModel"