chisquare
Metric
chisquarefromscipy.stats(scipy.stats.chisquare)
When to invoke this skill
The user has predictions + ground truth and asks to evaluate with chisquare, or
mentions scipy.stats.chisquare directly, or wants the standard scipy.stats implementation.
Reference signature
from scipy.stats import chisquare
# chisquare(f_obs, f_exp=None, ddof=0, axis=0, *, sum_check=True, nan_policy='propagate', keepdims=False)
Library docstring
Perform Pearson's chi-squared test.
Pearson's chi-squared test [1]_ is a goodness-of-fit test for a multinomial
distribution with given probabilities; that is, it assesses the null hypothesis
that the observed frequencies (counts) are obtained by independent
sampling of *N* observations from a categorical distribution with given
expected frequencies.
Parameters
----------
f_obs : array_like
Observed frequencies in each category.
f_exp : array_like, optional
Expected frequencies in each category. By default, the categories are
assumed to be equally likely.
ddof : int, optional
"Delta degrees of freedom": adjustment to the degrees of freedom
for the p-value. The p-value is computed using a chi-squared
distribution with ``k - 1 - ddof`` degrees of freedom, where ``k``
is the number of categories. The default value of `ddof` is 0.
axis : int or None, default: 0
If an int, the axis of the input along which to compute the statistic.
The statistic of each axis-slice (e.g. row) of the input will appear in a
corresponding element of the output.
If ``None``, the input will be raveled before computing the statistic.
sum_check : bool, optional
Whether to perform a check that ``sum(f_obs) - sum(f_exp) == 0``. If True,
(default) raise an error when the relative difference exceeds the square root
of the precision of the data type. See Notes for rationale and possible
exceptions.
nan_policy : {'propagate', 'omit', 'raise'}
Defines how to handle input NaNs.
- ``propagate``: if a NaN is present in the axis slice (e.g. row) along
which the statistic is computed, the corresponding entry of the output
will be NaN.
- ``omit``: NaNs will be omitted when performing the calculation.
If insufficient data remains in the axis slice along which the
statistic is computed, the corresponding entry of the output will be
NaN.
- ``raise``: if a NaN is present, a ``ValueError`` will be raised.
keepdims : bool, default: False
If this is set to True, the axes which are reduced are left
in the result as dimensions with size one. With this option,
the result will broadcast correc
Quick recipe
import scipy.stats as _m
score = _m.chisquare(y_true, y_pred)
Don'ts
- Don't reimplement when the library version handles edge cases (NaN, ties, empty inputs) better than a hand-rolled formula.
- Always check the library version's argument order — sklearn is
(y_true, y_pred)while torchmetrics is(preds, target).