|
| 1 | +# |
| 2 | +# Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | +# contributor license agreements. See the NOTICE file distributed with |
| 4 | +# this work for additional information regarding copyright ownership. |
| 5 | +# The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | +# (the "License"); you may not use this file except in compliance with |
| 7 | +# the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +# |
| 17 | + |
| 18 | +import itertools |
| 19 | + |
| 20 | +__all__ = ['ParamGridBuilder'] |
| 21 | + |
| 22 | + |
| 23 | +class ParamGridBuilder(object): |
| 24 | + """ |
| 25 | + Builder for a param grid used in grid search-based model selection. |
| 26 | +
|
| 27 | + >>> from classification import LogisticRegression |
| 28 | + >>> lr = LogisticRegression() |
| 29 | + >>> output = ParamGridBuilder().baseOn({lr.labelCol: 'l'}) \ |
| 30 | + .baseOn([lr.predictionCol, 'p']) \ |
| 31 | + .addGrid(lr.regParam, [1.0, 2.0, 3.0]) \ |
| 32 | + .addGrid(lr.maxIter, [1, 5]) \ |
| 33 | + .addGrid(lr.featuresCol, ['f']) \ |
| 34 | + .build() |
| 35 | + >>> expected = [ \ |
| 36 | +{lr.regParam: 1.0, lr.featuresCol: 'f', lr.maxIter: 1, lr.labelCol: 'l', lr.predictionCol: 'p'}, \ |
| 37 | +{lr.regParam: 2.0, lr.featuresCol: 'f', lr.maxIter: 1, lr.labelCol: 'l', lr.predictionCol: 'p'}, \ |
| 38 | +{lr.regParam: 3.0, lr.featuresCol: 'f', lr.maxIter: 1, lr.labelCol: 'l', lr.predictionCol: 'p'}, \ |
| 39 | +{lr.regParam: 1.0, lr.featuresCol: 'f', lr.maxIter: 5, lr.labelCol: 'l', lr.predictionCol: 'p'}, \ |
| 40 | +{lr.regParam: 2.0, lr.featuresCol: 'f', lr.maxIter: 5, lr.labelCol: 'l', lr.predictionCol: 'p'}, \ |
| 41 | +{lr.regParam: 3.0, lr.featuresCol: 'f', lr.maxIter: 5, lr.labelCol: 'l', lr.predictionCol: 'p'}] |
| 42 | + >>> len(output) == len(expected) |
| 43 | + True |
| 44 | + >>> all([m in expected for m in output]) |
| 45 | + True |
| 46 | + """ |
| 47 | + |
| 48 | + def __init__(self): |
| 49 | + self._param_grid = {} |
| 50 | + |
| 51 | + def addGrid(self, param, values): |
| 52 | + """ |
| 53 | + Sets the given parameters in this grid to fixed values. |
| 54 | + """ |
| 55 | + self._param_grid[param] = values |
| 56 | + |
| 57 | + return self |
| 58 | + |
| 59 | + def baseOn(self, *args): |
| 60 | + """ |
| 61 | + Sets the given parameters in this grid to fixed values. |
| 62 | + Accepts either a parameter dictionary or a list of (parameter, value) pairs. |
| 63 | + """ |
| 64 | + if isinstance(args[0], dict): |
| 65 | + self.baseOn(*args[0].items()) |
| 66 | + else: |
| 67 | + for (param, value) in args: |
| 68 | + self.addGrid(param, [value]) |
| 69 | + |
| 70 | + return self |
| 71 | + |
| 72 | + def build(self): |
| 73 | + """ |
| 74 | + Builds and returns all combinations of parameters specified |
| 75 | + by the param grid. |
| 76 | + """ |
| 77 | + keys = self._param_grid.keys() |
| 78 | + grid_values = self._param_grid.values() |
| 79 | + return [dict(zip(keys, prod)) for prod in itertools.product(*grid_values)] |
| 80 | + |
| 81 | + |
| 82 | +if __name__ == "__main__": |
| 83 | + import doctest |
| 84 | + doctest.testmod() |
0 commit comments