Return dict (param_name: value) of parameters that were given to estimator with non-default values.
(estimator)
| 89 | |
| 90 | |
| 91 | def _changed_params(estimator): |
| 92 | """Return dict (param_name: value) of parameters that were given to |
| 93 | estimator with non-default values.""" |
| 94 | |
| 95 | params = estimator.get_params(deep=False) |
| 96 | init_params = inspect.signature(estimator.__init__).parameters |
| 97 | init_params = {name: param.default for name, param in init_params.items()} |
| 98 | |
| 99 | def has_changed(k, v): |
| 100 | if k not in init_params: # happens if k is part of a **kwargs |
| 101 | return True |
| 102 | if init_params[k] == inspect._empty: # k has no default value |
| 103 | return True |
| 104 | # try to avoid calling repr on nested estimators |
| 105 | if isinstance(v, BaseEstimator) and v.__class__ != init_params[k].__class__: |
| 106 | return True |
| 107 | # Use repr as a last resort. It may be expensive. |
| 108 | if repr(v) != repr(init_params[k]) and not ( |
| 109 | is_scalar_nan(init_params[k]) and is_scalar_nan(v) |
| 110 | ): |
| 111 | return True |
| 112 | return False |
| 113 | |
| 114 | return {k: v for k, v in params.items() if has_changed(k, v)} |
| 115 | |
| 116 | |
| 117 | class _EstimatorPrettyPrinter(pprint.PrettyPrinter): |
no test coverage detected
searching dependent graphs…