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

熱線電話:13121318867

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

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

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

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

#隨機參數(shù)一列會員的消費總額

set.seed(1234)

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

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

library(Hmisc)

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

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

#使用for循環(huán)將每一段范圍值設定一個評分,即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]不包含最后一個值,故另外計算

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

}

table(M_score)

QQ截圖20160322104116.png


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

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

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

require(Hmisc)

#計算分段的切割點

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ù)框中的評分字段

return(res[,'score'])

}

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

table(M_score2)

QQ截圖20160322104125.png

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

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

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

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

下面創(chuàng)建一個數(shù)據(jù)框,來驗收一下自定義函數(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

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

數(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)用相應的接口 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); }