Classes
Biplot
Combined scores + loadings biplot for PCA, LDA, or OPLS-DA models.
Overlays sample score scatter (coloured by group label) with loading arrows so that both scores and loadings can be interpreted together on a single Plotly figure.
Args:
scoresDataFrame of shape (n_samples, >=2) containing score values(e.g. columns ``["PC1", "PC2"]``).
loadingsDataFrame of shape (n_features, >=2) containing loadingvalues aligned to the same components as ``scores``.
labelGroup label for each sample used to colour the score scatter.May be a Series or list of length n_samples. If ``None`` all points share one colour.
color_dictOptional mapping ``{group_name: colour_string}`` tooverride default Plotly colour cycle.
features_nameFeature labels for loading annotations. If ``None````loadings.index`` is used.
Examples: >>> import pandas as pd >>> import numpy as np >>> from metbit.viz.interpretation import Biplot >>> scores = pd.DataFrame(np.random.rand(40, 2), columns=["PC1", "PC2"]) >>> loadings = pd.DataFrame(np.random.rand(20, 2), columns=["PC1", "PC2"]) >>> label = pd.Series(["A"] * 20 + ["B"] * 20) >>> bp = Biplot(scores, loadings, label=label) >>> fig = bp.plot() >>> fig.show() # doctest: +SKIP
Methods
__init__(self, scores: pd.DataFrame, loadings: pd.DataFrame, label: Union[pd.Series, list, None]=None, color_dict: Optional[dict]=None, features_name: Optional[list]=None)
plot(self, pc: Optional[list[str]]=None, scale_loadings: float=0.7, top_n_loadings: int=20, marker_size: int=10, fig_height: int=700, fig_width: int=900, font_size: int=14, title: Optional[str]=None)
Render the biplot.
Args:
pcTwo component column names to plot, e.g. ``["PC1", "PC2"]``.Defaults to the first two columns of ``scores``.
scale_loadingsFraction of the score range used to scale loadingarrows so they fit within the score cloud. Values in (0, 1].
top_n_loadingsNumber of loadings (highest L2 magnitude) toannotate with text labels.
marker_sizeDiameter of score scatter markers in pixels.fig_heightFigure height in pixels.fig_widthFigure width in pixels.font_sizeGlobal font size in points.titleOptional figure title. If ``None`` a default title isconstructed from the component names.
Returns: A Plotly ``go.Figure`` object with score scatter traces and loading arrow annotations.
Raises:
ValueErrorIf a requested component name is not found in thescores or loadings DataFrames.
CoefficientPlot
Horizontal bar chart of model coefficients or loadings.
Useful for interpreting OPLS-DA coefficients, PLS regression coefficients, or any signed per-feature statistic.
Args:
coefCoefficient / loading values as a Series (with feature names asindex) or a 1-D NumPy array.
features_nameFeature labels. If ``None``, uses ``coef.index`` when``coef`` is a Series, or integer indices otherwise.
ci_lowerOptional lower bound of confidence intervals aligned to``coef``.
ci_upperOptional upper bound of confidence intervals aligned to``coef``.
Examples: >>> import pandas as pd >>> import numpy as np >>> from metbit.viz.interpretation import CoefficientPlot >>> rng = np.random.default_rng(0) >>> coef = pd.Series(rng.normal(size=50), name="coefficient") >>> cp = CoefficientPlot(coef) >>> fig = cp.plot(top_n=20) >>> fig.show() # doctest: +SKIP
Methods
__init__(self, coef: Union[pd.Series, np.ndarray], features_name: Optional[list]=None, ci_lower: Union[pd.Series, np.ndarray, None]=None, ci_upper: Union[pd.Series, np.ndarray, None]=None)
plot(self, top_n: int=30, sort_by: str='magnitude', color_positive: str='#2563eb', color_negative: str='#ef4444', fig_height: int=700, fig_width: int=800, font_size: int=13, title: Optional[str]=None)
Render the coefficient bar chart.
Args:
top_nMaximum number of features to display, selected by highestabsolute coefficient value before sorting.
sort_byOrdering of bars. ``"magnitude"`` sorts by absolutevalue (largest at top); ``"value"`` sorts by signed value (most positive at top).
color_positiveFill colour for positive bars.color_negativeFill colour for negative bars.fig_heightFigure height in pixels.fig_widthFigure width in pixels.font_sizeGlobal font size in points.titleOptional figure title.Returns: A Plotly ``go.Figure`` with a horizontal bar chart.
Raises:
ValueErrorIf ``sort_by`` is not ``"magnitude"`` or ``"value"``.FeatureImportancePlot
Unified feature importance visualisation for multiple model types.
Supports VIP scores, random-forest ``feature_importances_``, SHAP-style values, and loading magnitudes. When ``importance`` is a DataFrame with multiple columns, grouped bars allow side-by-side comparison of metrics.
Args:
importanceFeature importances as:- ``pd.Series`` - one score per feature (index = feature names). - ``pd.DataFrame`` - multiple importance metrics as columns (index = feature names, columns = metric names). - ``np.ndarray`` - 1-D array treated as a Series with integer index.
features_nameOverride for feature labels. If ``None``, uses``importance.index`` (Series / DataFrame) or integer indices (ndarray).
Examples: >>> import pandas as pd >>> import numpy as np >>> from metbit.viz.interpretation import FeatureImportancePlot >>> rng = np.random.default_rng(1) >>> vip = pd.Series(rng.exponential(size=60), name="VIP") >>> fip = FeatureImportancePlot(vip) >>> fig = fip.plot(top_n=20, threshold=1.0) >>> fig.show() # doctest: +SKIP >>> fig_cum = fip.plot_cumulative() >>> fig_cum.show() # doctest: +SKIP
Methods
__init__(self, importance: Union[pd.Series, pd.DataFrame, np.ndarray], features_name: Optional[list]=None)
plot(self, top_n: int=30, fig_height: int=700, fig_width: int=900, font_size: int=13, title: Optional[str]=None, threshold: Optional[float]=None)
Horizontal bar chart of top-n features by importance.
Args:
top_nNumber of top features to display.fig_heightFigure height in pixels.fig_widthFigure width in pixels.font_sizeGlobal font size in points.titleOptional figure title.thresholdIf provided, a vertical dashed line is drawn at thisvalue (e.g. ``threshold=1.0`` for the VIP >= 1 rule).
Returns: A Plotly ``go.Figure`` with horizontal bar(s).
plot_cumulative(self, fig_height: int=400, fig_width: int=700, font_size: int=13)
Cumulative importance curve sorted in descending order.
The "elbow" point where cumulative importance first reaches 90% of the total is highlighted with a vertical dashed line.
Args:
fig_heightFigure height in pixels.fig_widthFigure width in pixels.font_sizeGlobal font size in points.Returns: A Plotly ``go.Figure`` with a line chart of cumulative importance and an elbow marker.