python统计分析-单样本T检验

 

#!/usr/bin/env python
# -*- coding:utf-8 -*-

# <editable>

def execute():
    # <editable>
    '''
    载入模块
    '''
    from scipy.stats import ttest_1samp
    import pandas as pd
    from sqlalchemy import create_engine
    '''
    连接数据库
    '''
    engine = create_engine('mysql+pymysql://root:123123qwe@127.0.0.1:3306/analysis')
    '''
    选择目标数据
    '''
    params = {
        "columns": "SUNACTIVITY",
        "sequence": "SUNACTIVITY",
        "method": "pearson",
        "popmean": 1,
    }
    inputs = {"table": '纯随机性检验'}
    data_sql = 'select ' + params['columns'] + ' from ' + inputs['table']
    data_in = pd.read_sql_query(data_sql, engine)
    print(data_in)

    '''
    单样本t检验
    '''
    sequence = data_in[params['sequence']]
    p = ttest_1samp(sequence, float(params['popmean']))[1]
    data_out = ''
    if (p < 0.05):
        data_out += '单样本t检验结果\n'
        data_out += '检验结果\n'
        data_out += "p值为:" + str(p) + ",可以证明有统计学意义(小于0.01有显著差异性)"
    else:
        data_out += '单样本t检验结果'
        data_out += '检验结果'
        data_out += "p值为:" + str(p) + ",无充分证据证明有统计学意义"

    '''
    生成报告
    '''
    print(data_out)
    '''
    数据示例
        SUNACTIVITY
    0           5.0
    1          11.0
    2          16.0
    3          23.0
    4          36.0
    5          40.4
    6          29.8
    7          15.2
    8           7.5
    9           2.9
    10         83.4
    11         47.7
    12         47.8
    13         30.7
    14         12.2
    15         40.4
    16         29.8
    17         15.2
    18          7.5
    19          2.9
    20         12.6
    单样本t检验结果
    检验结果
    p值为:2.48103846791979e-05,可以证明有统计学意义(小于0.01有显著差异性)

    '''


# </editable>

if __name__ == '__main__':
    execute()

 

posted @ 2021-04-22 10:51  我当道士那儿些年  阅读(321)  评论(0编辑  收藏  举报