Conditions | 1 |
Total Lines | 75 |
Code Lines | 49 |
Lines | 0 |
Ratio | 0 % |
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 | """TPE (Tree-structured Parzen Estimator) optimizer.""" |
||
117 | @classmethod |
||
118 | def get_test_params(cls, parameter_set="default"): |
||
119 | """Return testing parameter settings for the optimizer.""" |
||
120 | from sklearn.datasets import load_wine |
||
121 | from sklearn.ensemble import RandomForestClassifier |
||
122 | from sklearn.svm import SVC |
||
123 | |||
124 | from hyperactive.experiment.integrations import SklearnCvExperiment |
||
125 | |||
126 | # Test case 1: Basic TPE with standard parameters |
||
127 | params = super().get_test_params(parameter_set) |
||
128 | params[0].update( |
||
129 | { |
||
130 | "n_startup_trials": 5, |
||
131 | "n_ei_candidates": 12, |
||
132 | } |
||
133 | ) |
||
134 | |||
135 | # Test case 2: Mixed parameter types with warm start |
||
136 | X, y = load_wine(return_X_y=True) |
||
137 | rf_exp = SklearnCvExperiment( |
||
138 | estimator=RandomForestClassifier(random_state=42), X=X, y=y |
||
139 | ) |
||
140 | |||
141 | mixed_param_space = { |
||
142 | "n_estimators": (10, 100), # Continuous integer |
||
143 | "max_depth": [3, 5, 7, 10, None], # Mixed discrete/None |
||
144 | "criterion": ["gini", "entropy"], # Categorical |
||
145 | "min_samples_split": (2, 20), # Continuous integer |
||
146 | "bootstrap": [True, False], # Boolean |
||
147 | } |
||
148 | |||
149 | # Warm start with known good configuration |
||
150 | warm_start_points = [ |
||
151 | { |
||
152 | "n_estimators": 50, |
||
153 | "max_depth": 5, |
||
154 | "criterion": "gini", |
||
155 | "min_samples_split": 2, |
||
156 | "bootstrap": True, |
||
157 | } |
||
158 | ] |
||
159 | |||
160 | params.append( |
||
161 | { |
||
162 | "param_space": mixed_param_space, |
||
163 | "n_trials": 20, |
||
164 | "experiment": rf_exp, |
||
165 | "n_startup_trials": 3, # Fewer random trials before TPE |
||
166 | "n_ei_candidates": 24, # More EI candidates for better optimization |
||
167 | "initialize": {"warm_start": warm_start_points}, |
||
168 | } |
||
169 | ) |
||
170 | |||
171 | # Test case 3: High-dimensional continuous space (TPE strength) |
||
172 | svm_exp = SklearnCvExperiment(estimator=SVC(), X=X, y=y) |
||
173 | high_dim_space = { |
||
174 | "C": (0.01, 100), |
||
175 | "gamma": (1e-6, 1e2), |
||
176 | "coef0": (0.0, 10.0), |
||
177 | "degree": (2, 5), |
||
178 | "tol": (1e-5, 1e-2), |
||
179 | } |
||
180 | |||
181 | params.append( |
||
182 | { |
||
183 | "param_space": high_dim_space, |
||
184 | "n_trials": 25, |
||
185 | "experiment": svm_exp, |
||
186 | "n_startup_trials": 8, # More startup for exploration |
||
187 | "n_ei_candidates": 32, # More candidates for complex space |
||
188 | } |
||
189 | ) |
||
190 | |||
191 | return params |
||
192 |