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()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM