深度阅读

How to Calculate Confidence Intervals in Python?

作者
作者
2023年08月22日
更新时间
13.89 分钟
阅读时间
0
阅读量

To calculate confidence intervals in Python, you can use the scipy.stats library. The library provides functions to calculate confidence intervals for both the normal and t-distributions.

Here’s an example of how to calculate a 95% confidence interval for a normally distributed set of data:

import numpy as np
from scipy import stats

# Generate some example data
data = np.random.normal(size=100)

# Calculate the mean and standard deviation of the data
mean = np.mean(data)
std = np.std(data, ddof=1)

# Calculate the 95% confidence interval
n = len(data)
se = std / np.sqrt(n)
z = stats.norm.ppf(0.975)
ci = (mean - z * se, mean + z * se)

print("Sample mean:", mean)
print("Standard deviation:", std)
print("95% Confidence interval:", ci)

In this example, we first generate some example data using numpy.random.normal(). We then calculate the sample mean and standard deviation of the data using numpy.mean() and numpy.std(). To calculate the 95% confidence interval, we first calculate the standard error of the mean using std / np.sqrt(n), where n is the sample size. We then calculate the z-score for a 95% confidence interval using stats.norm.ppf(0.975). Finally, we calculate the confidence interval using (mean - z * se, mean + z * se).

You can use the stats.t functions in a similar way to calculate confidence intervals for t-distributions.

相关标签

博客作者

热爱技术,乐于分享,持续学习。专注于Web开发、系统架构设计和人工智能领域。