scipy之t分布获取均值置信区间

import json
import math
import numpy as np
from scipy import stats
import matplotlib.pyplot as plt

data_list = []
with open('test_130380_73.log','r') as f:
    data = f.readlines()
    #readlines按行读取返回数组,遍历数组元素
    for i in data:
        item = i.split('<<<<<')
        each = item[1].split('>')
        data_list.append(json.loads(each[0]) )

cache = []
mysql = []
emqt = []
push = []
all = []
for dic in data_list:
    if dic['type'] == 'mysql':
        mysql.append(dic['use'])
    elif dic['type'] == 'cache':
        cache.append(dic['use'])
    elif dic['type'] == 'emqt':
        emqt.append(dic['use'])
    elif dic['type'] == 'push':
        push.append(dic['use'])
    elif dic['type'] == 'all':
        all.append(dic['use'])

# print('cache用时:',cache)
# print('mysql时间:',mysql)
# print('emqt时间:',emqt)
# print('push时间:',push)
# print('all时间:',all)

#stats.t.interval(置信度,自由度,均值,标准差)   得到置信区间
mysql_ci = stats.t.interval(0.95, df=len(mysql)-1, loc=np.mean(mysql), scale=stats.sem(mysql))
cache_ci = stats.t.interval(0.95, df=len(cache)-1, loc=np.mean(cache), scale=stats.sem(cache))
emqt_ci = stats.t.interval(0.95, df=len(emqt)-1, loc=np.mean(emqt), scale=stats.sem(emqt))
push_ci = stats.t.interval(0.95, df=len(push)-1, loc=np.mean(push), scale=stats.sem(push))
all_ci = stats.t.interval(0.95, df=len(all)-1, loc=np.mean(all), scale=stats.sem(all))

print('mysql_ci',mysql_ci)
print('cache_ci',cache_ci)
print('emqt_ci',emqt_ci)
print('push_ci',push_ci)
print('all_ci',all_ci)
print('cache样本数:',len(cache),'最大用时:',max(cache),'最小用时:',min(cache),'cache均值:','%.2f' % np.mean(cache))
print('emqt样本数',len(emqt),'最大用时:',max(emqt),'最小用时:',min(emqt),'emqt均值:','%.2f' % np.mean(emqt))
print('push样本数',len(push),'最大用时:',max(push),'最小用时:',min(push),'push均值:','%.2f' % np.mean(push))
print('all样本数',len(all),'最大用时:',max(all),'最小用时:',min(all),'all均值:','%.2f' % np.mean(all))
print('mysql样本数',len(mysql),'最大用时:',max(mysql),'最小用时:',min(mysql),'mysql均值:','%.2f' % np.mean(mysql))


©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容