Run lightweight feature QA checks for notebook discovery loops.
(
X: Sequence[Sequence[float]] | pl.DataFrame,
y: Sequence[float] | None = None,
*,
feature_names: Sequence[str] | None = None,
min_coverage: float = 0.95,
max_corr: float = 0.95,
)
| 707 | sample_weight : Sequence[float] or None, default None |
| 708 | Optional finite nonnegative weight per sample, with at least one positive value. |
| 709 | Each bootstrap training draw must contain positive weight; a draw with only |
| 710 | zero-weight rows raises ValueError. Zero-weight rows are otherwise allowed. |
| 711 | n_estimators : int, default 32 |
| 712 | Number of bootstrap resamples; at least 2. |
| 713 | seed : int, default 42 |
| 714 | Seed for the bootstrap draws. |
| 715 | |
| 716 | Returns |
| 717 | ------- |
| 718 | dict[str, Any] |
| 719 | `method` (`"mdi"`), `table` (polars.DataFrame with columns `feature`, `mean`, `std` |
| 720 | and `stderr` across resamples, sorted by `mean` descending), `records` (`table` as a |
| 721 | list of dicts), `viz_payload` (`openquant.viz.prepare_feature_importance_payload` of |
| 722 | `mean` with `stderr` error bars) and `meta` (`n_estimators`, `seed`). |
| 723 | |
| 724 | Raises |
| 725 | ------ |
| 726 | ValueError |
| 727 | If `X` is empty or not rectangular, `y` contains a non-finite or nonbinary label, |
| 728 | feature names are not unique, a length does not match the rows of `X`, or |
| 729 | `n_estimators < 2`. |
| 730 | """ |
| 731 | x = _as_matrix(X) |
| 732 | yv = _as_vector(y, len(x)) |
| 733 | names = _feature_names(len(x[0]), feature_names) |
| 734 | weights = _sample_weight(sample_weight, len(x)) |
| 735 | |
| 736 | if n_estimators < 2: |
| 737 | raise ValueError("n_estimators must be >= 2") |
| 738 | |
| 739 | rng = random.Random(seed) |
| 740 | per_feature: list[list[float]] = [[] for _ in names] |
| 741 | |
| 742 | for _ in range(n_estimators): |
| 743 | idx = [rng.randrange(len(x)) for _ in range(len(x))] |
| 744 | xb = [x[i] for i in idx] |
| 745 | yb = [yv[i] for i in idx] |
| 746 | wb = [weights[i] for i in idx] if weights is not None else None |
| 747 | if wb is not None and not any(w > 0.0 for w in wb): |
| 748 | raise ValueError("sample_weight bootstrap training draw must contain a positive value") |
| 749 | |
| 750 | model = _fit_linear_probability_model(xb, yb, wb) |
| 751 | raw = [abs(v) for v in model.coeffs] |
| 752 | denom = sum(raw) |
| 753 | norm = [v / denom if denom > 0 else 0.0 for v in raw] |
| 754 | for j, v in enumerate(norm): |
| 755 | per_feature[j].append(v) |
| 756 | |
| 757 | table = _importance_table(names, per_feature) |
| 758 | payload = viz.prepare_feature_importance_payload( |
| 759 | table["feature"].to_list(), table["mean"].to_list(), std=table["stderr"].to_list() |
| 760 | ) |
| 761 | return { |
| 762 | "method": "mdi", |
| 763 | "table": table, |
| 764 | "records": table.to_dicts(), |
| 765 | "viz_payload": payload, |
| 766 | "meta": {"n_estimators": n_estimators, "seed": seed}, |
nothing calls this directly
no test coverage detected