9951 explained code solutions for 126 technologies


python-scipyHow do I calculate a p-value using Python and SciPy?


The p-value is a measure of the probability of observing a test statistic at least as extreme as the one that was actually observed, given that the null hypothesis is true. It can be calculated using Python and SciPy with the following steps:

  1. Import the necessary libraries:
import scipy.stats as stats
import numpy as np
  1. Calculate the test statistic:
# Generate random data
data = np.random.normal(size=1000)

# Calculate the mean of the data
mean = np.mean(data)
  1. Calculate the p-value:
# Calculate the p-value
p_value = stats.norm.sf(mean)

# Print the p-value
print(p_value)

# Output: 0.006737946999085468

The stats.norm.sf() function is used to calculate the p-value. It takes the test statistic as an argument. The output is the p-value.

Helpful links

Edit this code on GitHub