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

熱線電話:13121318867

登錄
首頁精彩閱讀R異常值檢測
R異常值檢測
2016-01-10
收藏

R異常值檢測

正常值 vs.異常值

異常值的定義是:和大多數(shù)樣本表現(xiàn)不一致的個體。

數(shù)據(jù)噪聲:就好比當你收聽一個信號不好的無線廣播時,你會聽到許多背景噪聲。

異常值和正常值圖中橙色區(qū)域的數(shù)據(jù)圍繞某個值上下波動并且沒有表現(xiàn)出確定的趨勢,我們稱之為白噪聲數(shù)據(jù)。

圖中紅圈中的數(shù)據(jù)是噪聲數(shù)據(jù)嗎?抑或是某種未發(fā)現(xiàn)趨勢的峰值呢?

一個好的算法可以檢測出異常值并將其剔除掉,AnomalyDetection包種的AnomalyDetectionTs函數(shù)可以很好地實現(xiàn)這個功能。

異常值檢測實例

本文案例采用的是維基百科的數(shù)據(jù),我們可以利用R通過API接口下載某個特定詞項每日瀏覽量的數(shù)據(jù)。

本文的研究數(shù)據(jù)是:英文詞語fifa從2013-02-22至今每日瀏覽量的數(shù)據(jù)。

異常值檢測

應用上文提到的算法,我們可以繪制出原始的時間序列圖并標注出異常值的位置。異常值位置算法解析

算法中的參數(shù)max_anoms=0.01表示在最終結果中標注出0.01%的異常值;而參數(shù)direction=”pos”則表示檢測高于平均水平的異常值。

如下表所示,該算法不僅可以檢測出異常值,還會返回對應的平均水平值。

檢測異常值如果你想了解更多的關于該算法的數(shù)學原理,你可以搜索Generalized ESD和時間序列分解。

發(fā)現(xiàn)異常情況異常情況

上圖中黑色直線表示該時期內的瀏覽量呈遞減趨勢。有趣的是,位于直線上方用黑圈標注出來的兩個點并沒有被判定為異常值,這是因為該異常值檢測算法主要關注的是趨勢變化時的情況。黑圈中的點還是處于下降趨勢中,所以沒有被判定為異常值。相反地,2014-07-12的瀏覽量突然上升,突破前期的下降趨勢,因此該點被判定為異常值。

新聞核實

詞語fifa瀏覽量的異常值與國際足聯(lián)的新聞消息息息相關,第一組異常值出現(xiàn)在2014年世界杯期間(2014年6 – 7月),而第二組異常值出現(xiàn)在國際足聯(lián)丑聞事件期間(2015年5月)。

洛杉磯時報中展示了國際足聯(lián)丑聞事件進展的時間表,正如我們算法所檢測出的異常值,該丑聞事件有兩個重要的時間點:5月27日和28日。

附錄(R Code)

install.packages(“devtools”)

devtools::install_github(“petermeissner/wikipediatrend”)

devtools::install_github(“twitter/AnomalyDetection”)

install.packages(“Rcpp”)

library(wikipediatrend) ## Library containing API wikipedia access

library(AnomalyDetection)

library(ggplot2)

Download wiki webpage “fifa”

fifa_data = wp_trend(“fifa”, from=”2013-03-18”, lang = “en”)

Plotting data

ggplot(fifa_data, aes(x=date, y=count, color=count)) + geom_line()

Convert date variable

fifa_data<img alt=”date=as.POSIXct(fift_data”data-cke-saved-src=”https://chart.googleapis.com/chart?cht=tx&chl=date%20%3D%20as.POSIXct(fifa_data”>date)

Keep only desiered variables (date & page views)

fifa_data=fifa_data[,c(1,2)]

Apply anomaly detection

data_anomaly = AnomalyDetectionTs(fifa_data, max_anoms=0.01, direction=”pos”, plot=TRUE, e_value = T)

jpeg(“03_fifa_wikipedia_term_page_views_anomaly_detection.jpg”, width= 8.25, height= 5.25, units=”in”, res=500, pointsize = 4)

Plot original data + anomalies points

data_anomaly$plot

dev.off()

Calculate deviation percentage from the expected value

data_anomalyperc_diff=round(100*(data_anomalyexpected_value-data_anomalyanoms)/data_anomalyexpected_value)

Plot anomalies table

anomaly_table=data_anomaly$anoms

數(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); }