直接使用python scipy
的stats.t.interval()
函数
import pandas as pd
import numpy as np
import tushare as ts
import matplotlib.pyplot as plt
import seaborn as sns
from scipy import stats
计算置信区间首先要有一组数组数据,比如要计算模型准确度置信区间,通过交叉验证得到模型准确度数组,然后对数组使用以下函数:
函数参数:stats.t.interval(置信度,自由度,均值,标准误)
置信度
: 0.95或0.97之类的常用的置信度,自己设置
自由度
:数组的长度-1
均值
:数据的均值
标准误
:通过数据的标准差计算得到,等于-----std/
n
\sqrt n
n, 其中n是数组长度。
**例子:
**stats.t.interval(0.95,accuracy.shape[0]-1, accuracy.mean(),accuracy.std()/np.sqrt(accuracy.shape[0]))