Conditions | 1 |
Total Lines | 54 |
Code Lines | 34 |
Lines | 54 |
Ratio | 100 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | """Quasi-Monte Carlo optimizer.""" |
||
110 | View Code Duplication | @classmethod |
|
|
|||
111 | def get_test_params(cls, parameter_set="default"): |
||
112 | """Return testing parameter settings for the optimizer.""" |
||
113 | from sklearn.datasets import load_iris |
||
114 | from sklearn.linear_model import LogisticRegression |
||
115 | |||
116 | from hyperactive.experiment.integrations import SklearnCvExperiment |
||
117 | |||
118 | # Test case 1: Halton sequence without scrambling |
||
119 | params = super().get_test_params(parameter_set) |
||
120 | params[0].update( |
||
121 | { |
||
122 | "qmc_type": "halton", |
||
123 | "scramble": False, |
||
124 | } |
||
125 | ) |
||
126 | |||
127 | # Test case 2: Sobol sequence with scrambling |
||
128 | X, y = load_iris(return_X_y=True) |
||
129 | lr_exp = SklearnCvExperiment( |
||
130 | estimator=LogisticRegression(random_state=42, max_iter=1000), X=X, y=y |
||
131 | ) |
||
132 | |||
133 | mixed_param_space = { |
||
134 | "C": (0.01, 100), # Continuous |
||
135 | "penalty": [ |
||
136 | "l1", |
||
137 | "l2", |
||
138 | ], # Categorical - removed elasticnet to avoid solver conflicts |
||
139 | "solver": ["liblinear", "saga"], # Categorical |
||
140 | } |
||
141 | |||
142 | params.append( |
||
143 | { |
||
144 | "param_space": mixed_param_space, |
||
145 | "n_trials": 16, # Power of 2 for better QMC properties |
||
146 | "experiment": lr_exp, |
||
147 | "qmc_type": "sobol", # Different sequence type |
||
148 | "scramble": True, # With scrambling for randomization |
||
149 | } |
||
150 | ) |
||
151 | |||
152 | # Test case 3: Different sampler configuration with same experiment |
||
153 | params.append( |
||
154 | { |
||
155 | "param_space": mixed_param_space, |
||
156 | "n_trials": 8, # Power of 2, good for QMC |
||
157 | "experiment": lr_exp, |
||
158 | "qmc_type": "halton", # Different QMC type |
||
159 | "scramble": False, |
||
160 | } |
||
161 | ) |
||
162 | |||
163 | return params |
||
164 |