python统计分析-正态性检验

 

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

# <editable>

def execute():
    # <editable>
    '''
    载入模块
    '''
    from scipy.stats import normaltest
    import numpy as np
    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",
        "nan_policy": 'propagate',
    }
    inputs = {"table": '纯随机性检验'}
    data_sql = 'select ' + params['columns'] + ' from ' + inputs['table']
    data_in = pd.read_sql_query(data_sql, engine)
    # print(data_in)

    '''
    正态性检验
    '''
    data_in = data_in.select_dtypes(include=['number'])  # 筛选数值型数据
    '''
    nan_policy : {'propagate', 'raise', 'omit'}, optional
    Defines how to handle when input contains nan.
    The following options are available (default is 'propagate'):

      * 'propagate': returns nan
      * 'raise': throws an error
      * 'omit': performs the calculations ignoring nan values
    '''
    p = normaltest(data_in, nan_policy=params['nan_policy'])[1]
    print(p)

    print('正态性检验结果')
    print('检验结果,当p<0.05时,可以证明数据不服从正态分布')
    p = pd.DataFrame(p)
    p.columns = ['p值']

    data_out = np.around(p, decimals=4)

    '''
    生成报告
    '''
    print(data_out)


# </editable>


if __name__ == '__main__':
    execute()

 

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