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

熱線電話:13121318867

登錄
首頁精彩閱讀使用R并行方式對數(shù)值型數(shù)據(jù)離散化
使用R并行方式對數(shù)值型數(shù)據(jù)離散化
2018-02-26
收藏

使用R并行方式對數(shù)值型數(shù)據(jù)離散化

數(shù)據(jù)的特征按照其取值可以分為連續(xù)型和離散型。離散數(shù)值屬性在數(shù)據(jù)挖掘的過程中具有重要的作用。比如在信用卡評分模型中,當(dāng)自變量很多時,并非所有字段對于目標(biāo)字段來說都是有效的,因此通常的做法是通過計(jì)算woe值和iv值(類似于信息增益)來初步挑選通過對目標(biāo)變量重要的字段,然后建模邏輯回歸模型。而這當(dāng)中就需要對數(shù)值型數(shù)據(jù)離散化。

數(shù)值型數(shù)據(jù)離散化通常分為有監(jiān)督離散化和無監(jiān)督離散化??紤]到數(shù)據(jù)建模通常是建立目標(biāo)字段和其影響因素之間的關(guān)系的量化,因此會選擇有監(jiān)督離散化。

R語言中用于數(shù)值型數(shù)據(jù)離散化的包discretization。安裝和加載如下:

>install.packages("discretization") >library(discretization)

以R自帶數(shù)據(jù)集iris為例,以”Species” 為目標(biāo)字段,對”Sepal.Length”、”Sepal.Width”、”Petal.Length” 、”Petal.Width” 四個數(shù)值型屬性離散化。

 >lisan_result <- mdlp(iris)
 >class(lisan_result)
   [1] “l(fā)ist”
 >names(lisan_result)
   [1] “cutp” “Disc.data”

使用mdlp()方法對iris離散化,該方法默認(rèn)數(shù)據(jù)框最后一列最后為目標(biāo)字段。返回結(jié)果為列表。”cutp”為各列的分割點(diǎn)向量。”Disc.data”為離散化后的數(shù)據(jù)框。

該方法對于較小的樣本量和維度時,程序運(yùn)行時間還可以接受。但隨著數(shù)據(jù)量的增大,數(shù)據(jù)維度的增加,程序運(yùn)行時間會越來越長。因此考慮采用并行的方式對數(shù)據(jù)進(jìn)行離散化。介紹R用于離散化的包parallel。

 >install.packages(“parallel”)
 >library(parallel)
 >cores <- detectCores() ##查看本機(jī)虛擬核心數(shù)
 > cores
    [1] 4

現(xiàn)在考慮以并行的方式實(shí)現(xiàn)離散化方法。考慮設(shè)計(jì)思路如下:

1.將字段10個為一組分別與目標(biāo)字段組合成數(shù)據(jù)框,(不足10個時以實(shí)際數(shù)量字段與目標(biāo)字段組合)存放在一個列表中。列表的元素即離散字段與目標(biāo)字段構(gòu)成的數(shù)據(jù)框。

2.啟動M個附屬進(jìn)程,并初始化。M<=本機(jī)虛擬核心數(shù)。使用parLapply()作用于步驟1中建立的列表數(shù)據(jù)。此時既有M個附屬進(jìn)程對數(shù)據(jù)進(jìn)行離散化。

3.將步驟2中的離散化結(jié)果合并。

4.將上述步驟封裝成函數(shù)。整理后使得返回結(jié)果與mdlp()函數(shù)一致。這樣方便調(diào)用。

將上述設(shè)計(jì)思路寫成R代碼,如下:輸入離散數(shù)據(jù)、使用核心數(shù),返回結(jié)果與使用mdlp()函數(shù)相同

parallel_lisan <- function(lisan_data,cores_num){

   library(parallel)
   library(discretization

    res <- list()
   lisan_data_v <- list()
   cut_point <- list()
   Disc.data <- data.frame(c(rep(NA,nrow(lisan_data))))

   name_num = ncol(lisan_data)-1    ##將原始數(shù)據(jù)分割成多列,先考慮每組10列。不足的單獨(dú)分為一組。
   group_num = floor(name_num/10)
   last_group_num = name_num%%10

   if( name_num > 10{       ##當(dāng)原始數(shù)據(jù)列數(shù)多余10列
         for(i in 1:group_num){
               lw_flag <- lisan_data[,ncol(lisan_data)]
                lisan_data_v[[i]] <- cbind(lisan_data[,(10*i-9):>   (10*i)],lw_flag)
               }
   lisan_data_v[[group_num+1]] <- lisan_data[,         (10*group_num+1):ncol(lisan_data)]
        }else{
       lisan_data_v[[group_num+1]] <- lisan_data[,(10*group_num+1):ncol(lisan_data)]
}

    cl <- makeCluster(cores_num)     ##初始化核心
    results <- parLapply(cl,lisan_data_v,mdlp)     ##對列表數(shù)據(jù)使用mdlp函數(shù)并行離散化
    for(i in 1:length(results)){
        for(j in 1:length(results[[i]][[1]])){
            cut_point[[(i-1)*10+j]] <- results[[i]][[1]][[j]]
                  }
         temp <- as.data.frame(results[[i]][[2]])
         Disc.data <- cbind(Disc.data,temp[,1:(ncol(temp)-1)])         ##合并離散數(shù)據(jù)結(jié)果
   }

         Disc.data <- Disc.data[,2:ncol(Disc.data)]
         Disc.data$lw_flag <- lisan_data[,ncol(lisan_data)]
         names(Disc.data) <- names(lisan_data)
         stopCluster(cl)
         res[[“cutp”]] <- cut_point
         res[[“Disc.data”]] <- Disc.data

      return(res)
}

數(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)的第一個參數(shù)驗(yàn)證碼對象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺檢測極驗(yàn)服務(wù)器是否宕機(jī) new_captcha: data.new_captcha, // 用于宕機(jī)時表示是新驗(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ì)時完成 $(".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); }