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

熱線電話:13121318867

登錄
首頁(yè)精彩閱讀使用R語(yǔ)言實(shí)現(xiàn)數(shù)據(jù)分段
使用R語(yǔ)言實(shí)現(xiàn)數(shù)據(jù)分段
2016-04-11
收藏

使用R語(yǔ)言實(shí)現(xiàn)數(shù)據(jù)分段

今天跟大家講講我工作中用到的數(shù)據(jù)分段,數(shù)據(jù)分段一般在什么地方會(huì)使用到呢?評(píng)分。之前寫過(guò)一篇《實(shí)戰(zhàn): RFM》模型使用,那篇文章就詳細(xì)介紹了CRM(客戶關(guān)系管理)分析中關(guān)于RFM的應(yīng)用。應(yīng)用中就提到如何對(duì)R(最近一次消費(fèi)距當(dāng)前的時(shí)間間隔)、F(時(shí)間段內(nèi)的消費(fèi)頻次)和M(時(shí)間段內(nèi)的消費(fèi)總額)指標(biāo)進(jìn)行分段,形成三種得分指標(biāo),最后根據(jù)得分指標(biāo)計(jì)算出每個(gè)用戶的總得分,從而可以計(jì)算用戶的價(jià)值高低。

本文與之前提到的文章不同之處在于腳本的更改,使腳本更具靈活性?!秾?shí)戰(zhàn): RFM模型使用》文中對(duì)R、F和M分段使用for循環(huán),而且需要對(duì)每一個(gè)指標(biāo)做循環(huán),如果某個(gè)數(shù)據(jù)框的字段非常多,這樣用for循環(huán)就顯得非常麻煩。所以就有必要寫一段更靈活的連續(xù)變量分段操作的R腳本。這里用案例說(shuō)明一下數(shù)據(jù)分段操作:

#隨機(jī)參數(shù)一列會(huì)員的消費(fèi)總額

set.seed(1234)

Money <- c(round(runif(n = 5000, min = 56, max = 9143)), round(rnorm(n = 5000, mean = 892, sd = 23)))

#使用《實(shí)戰(zhàn): RFM模型使用》的分段方法,這里分成10段,盡量保證每段中的數(shù)據(jù)量大致相當(dāng)

library(Hmisc)

#使用cut2()函數(shù)對(duì)數(shù)據(jù)進(jìn)行分段

M_X <- cut2(x = Money, g = 10, onlycuts = TRUE)

#使用for循環(huán)將每一段范圍值設(shè)定一個(gè)評(píng)分,即1:10分

M_score <- 0

for(i in 1:10) {

M_score[Money >= M_X[i] & Money < M_X[i+1]] = i

#由于范圍Money < M_X[i+1]不包含最后一個(gè)值,故另外計(jì)算

M_score[Money == M_X[11]] = 10

}

table(M_score)

QQ截圖20160322104116.png


通過(guò)上面的方法,可以將連續(xù)型數(shù)據(jù)分成n段,從案例返回的結(jié)果可知,10段中的樣本量基本相當(dāng),可以視作分段成功。下面再看看自定義函數(shù)實(shí)現(xiàn)的分段:

#自定義得分函數(shù),x為目標(biāo)向量,g為所需分段數(shù)量

Score_function <- function(x,g = 10){

require(Hmisc)

#計(jì)算分段的切割點(diǎn)

cuts <- cut2(x,g = g, onlycuts = TRUE)

#將所需結(jié)果存放在res數(shù)據(jù)框中

res <- data.frame(x=x, cut = cut2(x, cuts = cuts),score = as.numeric(cut2(x, cuts = cuts)))

#這里返回res數(shù)據(jù)框中的評(píng)分字段

return(res[,'score'])

}

M_score2 <- Score_function(x = Money, g = 10)

table(M_score2)

QQ截圖20160322104125.png

同樣,分段的結(jié)果與《實(shí)戰(zhàn): RFM模型使用》腳本的結(jié)果一致,這里說(shuō)一下自定義函數(shù)的優(yōu)勢(shì):

1)可以靈活的更改分組數(shù)量,即g參數(shù)

2)不需要循環(huán),速度得到提升

3)可以結(jié)合sapply()函數(shù),應(yīng)用于大型數(shù)據(jù)框(高維數(shù)據(jù)),從而避免對(duì)每個(gè)字段都計(jì)算一次for循環(huán)

下面創(chuàng)建一個(gè)數(shù)據(jù)框,來(lái)驗(yàn)收一下自定義函數(shù)的效果:

set.seed(1234)

x1 <- round(rnorm(n = 5000, mean = 125, sd = 30))

x2 <- round(runif(n = 5000, min = 10, max = 100))

x3 <- round(runif(n = 5000, min = 100, max = 1000))

x4 <- round(rnorm(n = 5000, mean = 100, sd = 10))

df <- data.frame(x1 = x1, x2 = x2, x3 = x3, x4 = x4)

#結(jié)合sapply()函數(shù)

df2 <- sapply(df, Score_function)

head(df2)

df2 <- as.data.frame(df2)

table(df2$x1);table(df2$x2);table(df2$x3);table(df2$x4)

QQ截圖20160322104200.png

如果使用《實(shí)戰(zhàn): RFM模型使用》的方法,4個(gè)變量需要單獨(dú)拿出來(lái)做4次for循環(huán)。如果你覺得還可以再套一個(gè)循環(huán),這樣就可以不用單獨(dú)4次for循環(huán)了,問(wèn)題是這樣做會(huì)大大降低計(jì)算效率,影響速度。

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

若不方便掃碼,搜微信號(hào):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)證碼對(duì)象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個(gè)配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺(tái)檢測(cè)極驗(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ù)說(shuō)明請(qǐng)參見: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 = '請(qǐng)輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請(qǐng)輸入正確的'+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); }