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

熱線電話:13121318867

登錄
首頁精彩閱讀機器學習算法的隨機數(shù)據(jù)生成
機器學習算法的隨機數(shù)據(jù)生成
2016-12-02
收藏

機器學習算法的隨機數(shù)據(jù)生成

在學習機器學習算法的過程中,我們經(jīng)常需要數(shù)據(jù)來驗證算法,調試參數(shù)。但是找到一組十分合適某種特定算法類型的數(shù)據(jù)樣本卻不那么容易。還好numpy, scikit-learn都提供了隨機數(shù)據(jù)生成的功能,我們可以自己生成適合某一種模型的數(shù)據(jù),用隨機數(shù)據(jù)來做清洗,歸一化,轉換,然后選擇模型與算法做擬合和預測。下面對scikit-learn和numpy生成數(shù)據(jù)樣本的方法做一個總結。

1. numpy隨機數(shù)據(jù)生成API

numpy比較適合用來生產(chǎn)一些簡單的抽樣數(shù)據(jù)。API都在random類中,常見的API有:

1) rand(d0, d1, …, dn) 用來生成d0xd1x…dn維的數(shù)組。數(shù)組的值在[0,1]之間

例如:np.random.rand(3,2,2),輸出如下3x2x2的數(shù)組

array([[[ 0.49042678, 0.60643763],
[ 0.18370487, 0.10836908]],

[[ 0.38269728, 0.66130293],
[ 0.5775944 , 0.52354981]],

[[ 0.71705929, 0.89453574],
[ 0.36245334, 0.37545211]]])

2) randn((d0, d1, …, dn), 也是用來生成d0xd1x…dn維的數(shù)組。不過數(shù)組的值服從N(0,1)的標準正態(tài)分布。

例如:np.random.randn(3,2),輸出如下3×2的數(shù)組,這些值是N(0,1)的抽樣數(shù)據(jù)。

array([[-0.5889483 , -0.34054626],
[-2.03094528, -0.21205145],
[-0.20804811, -0.97289898]])

如果需要服???$N(\mu,\sigma^2)$的正態(tài)分布,只需要在randn上每個生成的值x上做變換$\sigma x + \mu $即可,例如:

例如:2*np.random.randn(3,2) + 1,輸出如下3×2的數(shù)組,這些值是N(1,4)的抽樣數(shù)據(jù)。

array([[ 2.32910328, -0.677016 ],
[-0.09049511, 1.04687598],
[ 2.13493001, 3.30025852]])

3)randint(low[, high, size]),生成隨機的大小為size的數(shù)據(jù),size可以為整數(shù),為矩陣維數(shù),或者張量的維數(shù)。值位于半開區(qū)間 [low, high)。

例如:np.random.randint(3, size=[2,3,4])返回維數(shù)維2x3x4的數(shù)據(jù)。取值范圍為最大值為3的整數(shù)。

array([[[2, 1, 2, 1],
[0, 1, 2, 1],
[2, 1, 0, 2]],

[[0, 1, 0, 0],
[1, 1, 2, 1],
[1, 0, 1, 2]]])

再比如: np.random.randint(3, 6, size=[2,3]) 返回維數(shù)為2×3的數(shù)據(jù)。取值范圍為[3,6).

array([[4, 5, 3],
[3, 4, 5]])

4) random_integers(low[, high, size]),和上面的randint類似,區(qū)別在與取值范圍是閉區(qū)間[low, high]。

5) random_sample([size]), 返回隨機的浮點數(shù),在半開區(qū)間 [0.0, 1.0)。如果是其他區(qū)間[a,b),可以加以轉換(b – a) * random_sample([size]) + a

例如: (5-2)*np.random.random_sample(3)+2 返回[2,5)之間的3個隨機數(shù)。

array([ 2.87037573, 4.33790491, 2.1662832 ])

2. scikit-learn隨機數(shù)據(jù)生成API介紹

scikit-learn生成隨機數(shù)據(jù)的API都在datasets類之中,和numpy比起來,可以用來生成適合特定機器學習模型的數(shù)據(jù)。常用的API有:

1) 用make_regression 生成回歸模型的數(shù)據(jù)

2) 用make_hastie_10_2,make_classification或者make_multilabel_classification生成分類模型數(shù)據(jù)

3) 用make_blobs生成聚類模型數(shù)據(jù)

4) 用make_gaussian_quantiles生成分組多維正態(tài)分布的數(shù)據(jù)

3. scikit-learn隨機數(shù)據(jù)生成實例

3.1 回歸模型隨機數(shù)據(jù)

這里我們使用make_regression生成回歸模型數(shù)據(jù)。幾個關鍵參數(shù)有n_samples(生成樣本數(shù)), n_features(樣本特征數(shù)),noise(樣本隨機噪音)和coef(是否返回回歸系數(shù))。例子代碼如下:

\

輸出的圖如下:

\


3.2 分類模型隨機數(shù)據(jù)

這里我們用make_classification生成三元分類模型數(shù)據(jù)。幾個關鍵參數(shù)有n_samples(生成樣本數(shù)), n_features(樣本特征數(shù)), n_redundant(冗余特征數(shù))和n_classes(輸出的類別數(shù)),例子代碼如下:

\

輸出的圖如下:

\

3.3 聚類模型隨機數(shù)據(jù)

這里我們用make_blobs生成聚類模型數(shù)據(jù)。幾個關鍵參數(shù)有n_samples(生成樣本數(shù)), n_features(樣本特征數(shù)),centers(簇中心的個數(shù)或者自定義的簇中心)和cluster_std(簇數(shù)據(jù)方差,代表簇的聚合程度)。例子如下:

\

輸出的圖如下:

\

3.4 分組正態(tài)分布混合數(shù)據(jù)

我們用make_gaussian_quantiles生成分組多維正態(tài)分布的數(shù)據(jù)。幾個關鍵參數(shù)有n_samples(生成樣本數(shù)), n_features(正態(tài)分布的維數(shù)),mean(特征均值), cov(樣本協(xié)方差的系數(shù)), n_classes(數(shù)據(jù)在正態(tài)分布中按分位數(shù)分配的組數(shù))。 例子如下:

\


輸出圖如下


\

以上就是生產(chǎn)隨機數(shù)據(jù)的一個總結,希望可以幫到學習機器學習算法的朋友們。

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

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

數(shù)據(jù)分析師考試動態(tài)
數(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(); // 調用 initGeetest 進行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調,回調的第一個參數(shù)驗證碼對象,之后可以使用它調用相應的接口 initGeetest({ // 以下 4 個配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺檢測極驗服務器是否宕機 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); }