99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁精彩閱讀批量數(shù)據(jù)采集過程中方差的計算
批量數(shù)據(jù)采集過程中方差的計算
2018-03-09
收藏

批量數(shù)據(jù)采集過程中方差的計算

最近項目用需要判斷開始數(shù)據(jù)是否穩(wěn)定,即采集到的數(shù)據(jù)是否符合期望,我用方差來判斷采集到的數(shù)據(jù)是否穩(wěn)定。有兩種判斷方法:第一種是數(shù)據(jù)不斷的進來,我累積的進行方差計算;第二鐘是利用滑動窗口的思想,數(shù)據(jù)個數(shù)達到窗口大小時計算方差值,采用循環(huán)數(shù)組的模式來實現(xiàn)此功能。

第一種實現(xiàn)方法就是采用迭代式的思想進行方差計算。我實在網(wǎng)上看到一位大神的博客中有對此方法的描述,他用matlab代碼進行了說明,,我用C語言實現(xiàn)了;下面附上代碼:
[cpp] view plain copy

    double GetVariance(uint64_t value)  
    {  
        static uint8_t cnt = 0;  
        static double Var = 0;  
        static double Esp = 0;  
        double TempValue = 0;  
          
        cnt = cnt + 1;  
        if(cnt == 1)  
        {  
            Var = 0;  
            Esp = value;  
            return Var;  
        }  
        TempValue = value - Esp;  
        Esp = (value + Esp*(cnt - 1))/cnt;   
        Var = Var + TempValue*(value - Esp);  
        return (Var/cnt);  
    } 
這樣在程序中不斷調(diào)用該函數(shù)即可迭代式的計算出方差,而不需要知道數(shù)據(jù)的個數(shù)。
第二種方法是采用滑動窗口的思想,這里需要說明一下,我做的時候有兩種情況,一種是窗口不動,數(shù)據(jù)不斷前移,F(xiàn)IFO,這種實現(xiàn)起來最簡單;還有一種情況是窗口向前移動,這種實現(xiàn)起來就比較復雜了,我用單步調(diào)試好多次,才搞清楚之間的區(qū)別。
(1)窗口不動,數(shù)據(jù)前移:

[cpp] view plain copy

    double Function(uint16_t value)  
    {  
        static uint8_t cnt=0;  
        static uint8_t len=7;  
        static uint16_t sample[7]={0};  
        uint8_t i=0;  
        double var;  
        if(cnt < len)  
        {  
            sample[cnt++] = value;  
            return 0;  
        }  
        else  
        {  
            for(;i+1<cnt;i++)  
            {  
                sample[i]=sample[i+1];  
            }  
            sample[i]=value;  
            var=Variance(sample,7);  
    }  
    }  
其中 Variance()是我寫的計算方差函數(shù),這樣就實現(xiàn)了滑動計算數(shù)據(jù)方差值。
(2)窗口前移,這種實現(xiàn)數(shù)據(jù)的滑動,設(shè)定好窗口大小后,按照FIFO原則,數(shù)據(jù)不斷進入出去,但是這種實現(xiàn)數(shù)據(jù)滑動后對計算方差增加了難度,這里只說出如何實現(xiàn)窗口向前滑動的代碼:
[cpp] view plain copy

    void Function(uint16_t value)  
    {  
        static uint8_t cnt=0;  
        static uint8_t len=7;  
        static uint8_t index=0;  
        static int order[7]={0};  
        static int sample[7]={0};  
        uint8_t i=0;  
          
        sample[index] = value;  
        if(cnt < len)  
        {  
            cnt++;  
        }  
        else  
        {  
            for(i=0;i<cnt;i++)  
            {  
                if(order[i] == index)  
                break;  
            }  
            for(;i+1<cnt;i++)  
            {  
                order[i]=order[i+1];  
            }  
        }  
          
        order[cnt-1] = index;  
        index=(1+index)%len;  
    }  


最后把計算方差的函數(shù)Varanice()代碼列出來:
[cpp] view plain copy

    double Variance(uint16_t data[], uint8_t n)  
    {  
        double mean = 0, divisor;  
        uint16_t sum = 0,Varian = 0;  
        uint8_t i;  
      
        for(i=0;i<n;i++)  
        {  
            sum = sum + data[i];  
        }  
        mean = sum/n;  
        for(i=0;i<n;i++)  
        {  
            Varian = Varian + pow(data[i]-mean,2);  
        }  
          
        /*程序中divisor是自由度,20是小樣本判斷的一個標準。如果是小樣本的話,約束較大,
            自由度就要減一;如果是大樣本的話,自由度為樣本個數(shù)。*/  
        if(n<20)  
        {  
            divisor = n-1;  
        }     
        else  
        {  
            divisor = n;  
        }  
        return (Varian/divisor);  
          
    } 
以上代碼如有錯誤還望指正,共同進步

數(shù)據(jù)分析咨詢請掃描二維碼

若不方便掃碼,搜微信號:CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個參數(shù)驗證碼對象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺檢測極驗服務(wù)器是否宕機 new_captcha: data.new_captcha, // 用于宕機時表示是新驗證碼的宕機 product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計時完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }