|
18 | 18 |
|
19 | 19 | LAYER_HEADER_RE = re.compile("^([^\\(]+)\\(([^\\)]+)\\)$") |
20 | 20 |
|
| 21 | +# Helper functions |
| 22 | +# ============================================================================= |
| 23 | + |
| 24 | + |
| 25 | +# sklearn's default baseline model for scoring the fit i.e., measuring R² is |
| 26 | +# "predict the mean" which is not the proper model for our regressions since |
| 27 | +# both R and C are through-origin fits - the R² computation doesn't behave |
| 28 | +# well for var(y) ≈ 0 - so we compute R² manually with a "predict zero" |
| 29 | +# baseline model. |
| 30 | +def compute_through_origin_fit_score(model, inputs, observed): |
| 31 | + sum_squared_observed = (observed**2).sum() |
| 32 | + if sum_squared_observed == 0: |
| 33 | + raise ValueError("Cannot score fit: all observed values are zero.") |
| 34 | + return 1.0 - ((observed - model.predict(inputs)) ** 2).sum() / sum_squared_observed |
| 35 | + |
| 36 | + |
21 | 37 | # Parse and validate arguments |
22 | 38 | # ============================================================================= |
23 | 39 |
|
@@ -410,8 +426,8 @@ def generic_rc_fit(type_sieve): |
410 | 426 | resistances, |
411 | 427 | capacitances_ff, |
412 | 428 | ) in layer_models.items(): |
413 | | - r_sq_res = res_model.score(lengths, resistances) |
414 | | - r_sq_cap = cap_model.score(lengths, capacitances_ff) |
| 429 | + r_sq_res = compute_through_origin_fit_score(res_model, lengths, resistances) |
| 430 | + r_sq_cap = compute_through_origin_fit_score(cap_model, lengths, capacitances_ff) |
415 | 431 | print("{:<12s} | {:>8.4f} | {:>8.4f}".format(layer_name, r_sq_res, r_sq_cap)) |
416 | 432 | print("-" * 34) |
417 | 433 | print("") |
|
0 commit comments