Conditions | 1 |
Total Lines | 64 |
Code Lines | 26 |
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 | """Experiment adapter for sklearn cross-validation experiments.""" |
||
130 | @classmethod |
||
131 | def get_test_params(cls, parameter_set="default"): |
||
132 | """Return testing parameter settings for the skbase object. |
||
133 | |||
134 | ``get_test_params`` is a unified interface point to store |
||
135 | parameter settings for testing purposes. This function is also |
||
136 | used in ``create_test_instance`` and ``create_test_instances_and_names`` |
||
137 | to construct test instances. |
||
138 | |||
139 | ``get_test_params`` should return a single ``dict``, or a ``list`` of ``dict``. |
||
140 | |||
141 | Each ``dict`` is a parameter configuration for testing, |
||
142 | and can be used to construct an "interesting" test instance. |
||
143 | A call to ``cls(**params)`` should |
||
144 | be valid for all dictionaries ``params`` in the return of ``get_test_params``. |
||
145 | |||
146 | The ``get_test_params`` need not return fixed lists of dictionaries, |
||
147 | it can also return dynamic or stochastic parameter settings. |
||
148 | |||
149 | Parameters |
||
150 | ---------- |
||
151 | parameter_set : str, default="default" |
||
152 | Name of the set of test parameters to return, for use in tests. If no |
||
153 | special parameters are defined for a value, will return `"default"` set. |
||
154 | |||
155 | Returns |
||
156 | ------- |
||
157 | params : dict or list of dict, default = {} |
||
158 | Parameters to create testing instances of the class |
||
159 | Each dict are parameters to construct an "interesting" test instance, i.e., |
||
160 | `MyClass(**params)` or `MyClass(**params[i])` creates a valid test instance. |
||
161 | `create_test_instance` uses the first (or only) dictionary in `params` |
||
162 | """ |
||
163 | from sklearn.datasets import load_diabetes, load_iris |
||
164 | from sklearn.svm import SVC, SVR |
||
165 | from sklearn.metrics import accuracy_score, mean_absolute_error |
||
166 | from sklearn.model_selection import KFold |
||
167 | |||
168 | X, y = load_iris(return_X_y=True) |
||
169 | params_classif = { |
||
170 | "estimator": SVC(), |
||
171 | "scoring": accuracy_score, |
||
172 | "cv": KFold(n_splits=3, shuffle=True), |
||
173 | "X": X, |
||
174 | "y": y, |
||
175 | } |
||
176 | |||
177 | X, y = load_diabetes(return_X_y=True) |
||
178 | params_regress = { |
||
179 | "estimator": SVR(), |
||
180 | "scoring": mean_absolute_error, |
||
181 | "cv": 2, |
||
182 | "X": X, |
||
183 | "y": y, |
||
184 | } |
||
185 | |||
186 | X, y = load_diabetes(return_X_y=True) |
||
187 | params_all_default = { |
||
188 | "estimator": SVR(), |
||
189 | "X": X, |
||
190 | "y": y, |
||
191 | } |
||
192 | |||
193 | return [params_classif, params_regress, params_all_default] |
||
194 | |||
211 |