Check a normality assumption
Validate a small numeric sample before applying a method whose assumptions include normality.
SciPy · Function
Performs the Shapiro-Wilk test for normality. It returns the test statistic and p-value, with options for axis-wise computation, NaN handling, and dimension preservation.
shapiro evaluates whether sample data are consistent with a normal distribution by returning the Shapiro-Wilk statistic and its associated p-value. It can reduce an array along a chosen axis, control NaN handling, and preserve reduced dimensions when requested.
scipy.stats.shapiro(x, *, axis=None, nan_policy='propagate', keepdims=False)| Name | Type | Description |
|---|---|---|
x | array_like | Array of sample data; it must contain at least three observations. |
axis | int or None, default None | Axis along which to compute the statistic; None ravels the input. |
nan_policy | {'propagate', 'omit', 'raise'}, default 'propagate' | NaN behavior: propagate, omit, or raise. |
keepdims | bool, default False | Whether reduced axes remain as dimensions of size one. |
A result containing statistic, the Shapiro-Wilk test statistic, and pvalue, the hypothesis-test p-value.
import numpy as np
from scipy.stats import shapiro
sample = np.array([1.2, 1.8, 2.0, 2.4, 2.7, 3.1])
result = shapiro(sample)
print(result.statistic, result.pvalue)Runs the normality test on a one-dimensional sample and prints its statistic and p-value.
Validate a small numeric sample before applying a method whose assumptions include normality.
Analyze matrix data by testing each row or column independently.
Process arrays containing missing observations with an explicit NaN policy.
It tests the null hypothesis that the data was drawn from a normal distribution. The result contains the test statistic and p-value.
Follow-up: What does a small p-value indicate? It provides evidence against the normality null hypothesis, subject to the chosen significance level.
With axis=None, the input is flattened before computation. With an integer axis, the test is computed independently for each slice along that axis.
Follow-up: How can output dimensions be retained? Set keepdims=True so reduced axes remain with length one.
nan_policy controls NaNs: propagate returns NaN for affected slices, omit excludes NaNs, and raise raises ValueError when a NaN is present.
Follow-up: When is omit potentially problematic? If omission leaves too few observations, the slice may not provide a valid test.
scipy.stats.normaltestscipy.stats.andersonscipy.stats.mstats.normaltest