| 56 | |
| 57 | /// Errors returned by the feature-importance functions. |
| 58 | #[derive(Debug, Clone, PartialEq, thiserror::Error)] |
| 59 | pub enum FeatureImportanceError { |
| 60 | /// [`plot_feature_importance`] could not write its CSV; carries the I/O error message. |
| 61 | #[error("failed to write output file: {0}")] |
| 62 | WriteOutput(String), |
| 63 | /// The named input is empty. |
| 64 | #[error("{0} cannot be empty")] |
| 65 | Empty(&'static str), |
| 66 | /// A per-tree importance row does not have one entry per feature name. |
| 67 | #[error("importance row length mismatch")] |
| 68 | ImportanceRowLengthMismatch, |
| 69 | /// The named input does not have one entry per feature. |
| 70 | #[error("{0} length mismatch")] |
| 71 | LengthMismatch(&'static str), |
| 72 | /// PCA feature rows do not all have the same length. |
| 73 | #[error("ragged feature rows")] |
| 74 | RaggedFeatureRows, |
| 75 | /// `x` or `y` is empty. |
| 76 | #[error("x and y cannot be empty")] |
| 77 | EmptyXy, |
| 78 | /// `x` and `y` have different numbers of rows. |
| 79 | #[error("x/y length mismatch")] |
| 80 | XyLengthMismatch, |
| 81 | /// The rows of `x` do not all have the same length. |
| 82 | #[error("ragged x rows")] |
| 83 | RaggedX, |
| 84 | /// `splits`, `sample_weight` or the model's predictions do not fit `x`: a split index is |
| 85 | /// not a row, `sample_weight` does not have one entry per row, or the model returned the |
| 86 | /// wrong number of predictions for a test fold. See the wrapped error. |
| 87 | #[error(transparent)] |
| 88 | CrossValidation(#[from] CrossValidationError), |
| 89 | } |
| 90 | |
| 91 | /// Importance of one feature. |
| 92 | #[derive(Clone, Copy, Debug, Default)] |
| 93 | pub struct ImportanceStats { |
| 94 | /// Mean importance across trees (MDI) or folds (MDA, SFI). |
| 95 | pub mean: f64, |
| 96 | /// **Standard error** of `mean` (despite the name): the deviation divided by `sqrt(n)`. |
| 97 | pub std: f64, |
| 98 | } |
| 99 | |
| 100 | /// Correlations between an importance vector and PCA loadings, from |
| 101 | /// [`feature_pca_analysis`] (AFML Snippet 8.6). Each is 0 where SciPy would return `NaN` |
| 102 | /// because an input is constant. |
| 103 | #[derive(Clone, Copy, Debug, Default)] |
| 104 | pub struct PcaCorrelation { |
| 105 | /// Pearson correlation of the (repeated) importances with `|eigenvector * eigenvalue|`. |
| 106 | pub pearson: f64, |
| 107 | /// Spearman correlation (average ranks for ties), as `scipy.stats.spearmanr`. |
| 108 | pub spearman: f64, |
| 109 | /// Kendall tau-b, as `scipy.stats.kendalltau`. |
| 110 | pub kendall: f64, |
| 111 | /// `scipy.stats.weightedtau(importance, 1 / pca_rank)` with hyperbolic weights, where |
| 112 | /// `pca_rank` ranks each feature's summed absolute loading (1 = largest, ties averaged). |