python统计分析-双样本T检验
#!/usr/bin/env python # -*- coding:utf-8 -*- # <editable> def execute(): # <editable> ''' 载入模块 ''' from scipy.stats import ttest_ind, norm import pandas as pd from sqlalchemy import create_engine ''' 连接数据库 ''' engine = create_engine('mysql+pymysql://root:123123qwe@127.0.0.1:3306/analysis') ''' 选择目标数据 ''' # 生成数据 # params = { # "col1": "", # "col2": "", # } # inputs = {"table": '纯随机性检验'} # data_sql = 'select ' + params['col1'] + ',' + params['col2'] + ' from ' + inputs['table'] # data_in = pd.read_sql_query(data_sql, engine) # print(data_in) col1 = norm.rvs(loc=5, scale=10, size=500) col2 = norm.rvs(loc=5, scale=10, size=500) ''' 双样本t检验 ''' # col1 = data_in[params['col1']] # col2 = data_in[params['col2']] # p = ttest_ind(col1, col2)[1] p = ttest_ind(col1, col2)[1] ''' ttest_ind(equal_var=False) equal_var : bool, optional If True (default), perform a standard independent 2 sample test that assumes equal population variances [R263]. If False, perform Welch’s t-test, which does not assume equal population variance [R264]. ''' data_out = '' if (p < 0.05): data_out += '双样本t检验结果' data_out += '检验结果' data_out += "p值为:" + str(p) + ",认为两者总体均值不同" else: data_out += '双样本t检验结果' data_out += '检验结果' data_out += "p值为:" + str(p) + ",无充分证据证明两者总体均值不同" ''' 生成报告 ''' print(data_out) # </editable> if __name__ == '__main__': execute()
作者:沐禹辰
出处:http://www.cnblogs.com/renfanzi/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。
出处:http://www.cnblogs.com/renfanzi/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。