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

熱線電話:13121318867

登錄
首頁大數(shù)據(jù)時(shí)代python numpy的樣本標(biāo)準(zhǔn)差怎么寫?
python numpy的樣本標(biāo)準(zhǔn)差怎么寫?
2023-04-24
收藏

Python中的NumPy(Numerical Python)是一種基于Python語言的科學(xué)計(jì)算庫,其提供了許多高效的數(shù)值計(jì)算工具和數(shù)組操作函數(shù)。其中包括計(jì)算樣本標(biāo)準(zhǔn)差的函數(shù)。

要在Python中使用NumPy計(jì)算樣本標(biāo)準(zhǔn)差,可以使用numpy.std函數(shù)。該函數(shù)的語法如下:

numpy.std(a, axis=None, dtype=None, ddof=0, keepdims=)

其中,參數(shù)a表示輸入的數(shù)據(jù)數(shù)組,可以是一維或多維數(shù)組;axis表示沿著哪個(gè)軸方向進(jìn)行計(jì)算,如果不指定則計(jì)算所有元素的標(biāo)準(zhǔn)差;dtype表示輸出結(jié)果的數(shù)據(jù)類型,如果不指定則默認(rèn)為輸入數(shù)組的數(shù)據(jù)類型ddof表示自由度(degrees of freedom),即用于計(jì)算樣本方差的分母系數(shù),當(dāng)計(jì)算全體數(shù)據(jù)的標(biāo)準(zhǔn)差時(shí),ddof應(yīng)該為0,當(dāng)計(jì)算樣本的標(biāo)準(zhǔn)差時(shí),ddof應(yīng)該為1;keepdims表示是否保持?jǐn)?shù)組的維度不變,在計(jì)算完畢后,默認(rèn)會將標(biāo)準(zhǔn)差的維度縮小。

例如,要計(jì)算以下一維數(shù)組a的樣本標(biāo)準(zhǔn)差

import numpy as np

a = np.array([1, 2, 3, 4, 5])
std_a = np.std(a, ddof=1)
print(std_a) # 輸出:1.5811388300841898

上述代碼中,ddof參數(shù)被設(shè)置為1,表示計(jì)算樣本標(biāo)準(zhǔn)差。計(jì)算結(jié)果為1.58。

同樣的,如果要計(jì)算以下二維數(shù)組b每一列的樣本標(biāo)準(zhǔn)差

import numpy as np

b = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
std_b = np.std(b, axis=0, ddof=1)
print(std_b) # 輸出:[2.44948974 2.44948974 2.44948974]

上述代碼中,axis參數(shù)被設(shè)置為0,表示沿著列方向計(jì)算標(biāo)準(zhǔn)差。計(jì)算結(jié)果為每一列的樣本標(biāo)準(zhǔn)差。

除了numpy.std函數(shù)外,NumPy還提供了其他計(jì)算標(biāo)準(zhǔn)差的函數(shù)。例如,可以使用numpy.var函數(shù)計(jì)算方差,然后再對結(jié)果求平方根即可得到標(biāo)準(zhǔn)差

import numpy as np

a = np.array([1, 2, 3, 4, 5])
var_a = np.var(a, ddof=1)
std_a = np.sqrt(var_a)
print(std_a) # 輸出:1.5811388300841898

另外,還可以使用numpy.mean函數(shù)計(jì)算均值,然后再使用NumPy的廣播功能計(jì)算標(biāo)準(zhǔn)差

import numpy as np

a = np.array([1, 2, 3, 4, 5])
mean_a = np.mean(a)
std_a = np.sqrt(np.mean((a - mean_a) ** 2))
print(std_a) # 輸出:1.5811388300841898

使用廣播的方式計(jì)算標(biāo)準(zhǔn)差更加靈活,可以適用于不同維度和形狀的數(shù)組。

總之,NumPy提供了多種計(jì)算樣本標(biāo)準(zhǔn)差的方法,包括直接使用numpy.std函數(shù)、先計(jì)算方差再求平方根、以及使用均值和廣播方式計(jì)算。選擇哪種方法取決于具體情況,需要根據(jù)數(shù)據(jù)的維度、形狀、大小以及計(jì)算效率等因素來選擇最合適的方法。

數(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(), // 加隨機(jī)數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進(jìn)行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個(gè)參數(shù)驗(yàn)證碼對象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個(gè)配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺檢測極驗(yàn)服務(wù)器是否宕機(jī) new_captcha: data.new_captcha, // 用于宕機(jī)時(shí)表示是新驗(yàn)證碼的宕機(jī) 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){ //倒計(jì)時(shí)完成 $(".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); }