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

熱線電話:13121318867

登錄
首頁精彩閱讀R語言炫技必備基本功
R語言炫技必備基本功
2017-02-20
收藏

R語言炫技必備基本功

R語言主要用于統(tǒng)計(jì)分析和繪圖,可以理解為是一種數(shù)學(xué)計(jì)算軟件,可編程,有很多有用的函數(shù)庫和數(shù)據(jù)集,它強(qiáng)大的作圖工具是做數(shù)據(jù)分析的好幫手,在高手如云的大數(shù)據(jù)江湖中,不炫個(gè)技都不敢說自己是江湖中人,那么我們就看看想要炫技需要掌握哪些基本功吧

注意:本文中實(shí)際使用的樣本數(shù)據(jù)是根據(jù)具體命令任意挑選某組樣本數(shù)據(jù),不具有針對性,因此自己試驗(yàn)可以隨意找樣本嘗試

一個(gè)table引發(fā)的血案

table函數(shù)就是用來輸出指定字段的統(tǒng)計(jì)表格,可以用來分析數(shù)據(jù)比例情況,像下面的樣子:

> table(full$Title, full$Survived)

               0   1
  Master      17  23
  Miss        55 130
  Mr         436  81
  Mrs         26 100
  Rare Title  15   8

那么為了讓table夠直觀,各路大俠紛紛使出了洪荒之力,注意,下面開始炫技部分:

第一種作圖方式(用于觀察標(biāo)準(zhǔn)殘差的場景):

> mosaicplot(table(full$Title, full$Survived), shade=TRUE)

第二種作圖方式(用于觀察總數(shù)目的場景):

> barplot(table(full$Survived, full$Title), sub="Survival by Title", ylab="number of passengers", col=c("steelblue4","steelblue2")) > le> barplot(table(full$Survived, full$Title), sub="Survival by Title", ylab="number of passengers", col=c("steelblue4","steelblue2"))
> legend("topleft",legend = c("Died","Survived"),fill=c("steelblue4","steelblue2"),inset = .05)

第三種作圖方式(用于觀察比例情況的場景):

> barplot(prop.table(table(full$Survived, full$Title),2), sub="Survival by Title", ylab="number of passengers", col=c("steelblue4","steelblue2"))
> legend("topleft",legend = c("Died","Survived"),fill=c("steelblue4","steelblue2"),inset = .05)

當(dāng)然還可以有第四種作圖方式(同樣是用于觀察比例情況的場景):

> library('ggthemes')
> ggplot(full, aes(x = Title, fill = factor(Survived))) + geom_bar(stat='count', position='fill') + theme_few()

不同風(fēng)格的決策樹

在上節(jié)數(shù)據(jù)缺失填補(bǔ)中我們見過這樣一棵決策樹

> library("rpart")
> library("rpart.plot")
> my_tree <- rpart(Fare ~ Pclass + Fsize + Embarked, data = train, method = "class", control=rpart.control(cp=0.0001))
> prp(my_tree, type = 4, extra = 100)

如果我們想看到每個(gè)分支的比例關(guān)系還可以在枝干上下文章:

> prp(my_tree, type = 2, extra = 100,branch.type=1)

圖中根據(jù)不同的枝干粗細(xì)能看出樣本集中在那個(gè)分支上

數(shù)據(jù)總覽方式

第一種:按列總覽

優(yōu)點(diǎn):可以看到有哪些列,什么類型,每一列取值舉幾個(gè)例子,也能看到有多少行

> str(train)
'data.frame':      2197291 obs. of  15 variables:
 $ people_id        : chr  "ppl_100" "ppl_100" "ppl_100" "ppl_100" ...
 $ activity_id      : chr  "act2_1734928" "act2_2434093" "act2_3404049" "act2_3651215" ...
 $ date             : chr  "2023-08-26" "2022-09-27" "2022-09-27" "2023-08-04" ...
 $ activity_category: chr  "type 4" "type 2" "type 2" "type 2" ...
 $ char_1           : chr  "" "" "" "" ...
 $ char_2           : chr  "" "" "" "" ...
 $ char_3           : chr  "" "" "" "" ...
 $ char_4           : chr  "" "" "" "" ...
 $ char_5           : chr  "" "" "" "" ...
 $ char_6           : chr  "" "" "" "" ...
 $ char_7           : chr  "" "" "" "" ...
 $ char_8           : chr  "" "" "" "" ...
 $ char_9           : chr  "" "" "" "" ...
 $ char_10          : chr  "type 76" "type 1" "type 1" "type 1" ...
 $ outcome          : int  0 0 0 0 0 0 1 1 1 1 ...

第二種:分布總覽

優(yōu)點(diǎn):能看出每一列的最大值、最小值、均值、中位數(shù)等分布數(shù)據(jù)

> summary(train)
comment_count      sex         has_free_course     score
 Min.   :   0.0        Min.   :0.0000   Min.   :0.0000        Min.   :0.00
 1st Qu.:   0.0        1st Qu.:0.0000   1st Qu.:0.0000        1st Qu.:0.00
 Median :   9.0        Median :1.0000   Median :0.0000        Median :4.90
 Mean   : 397.6        Mean   :0.6259   Mean   :0.3786        Mean   :2.92
 3rd Qu.: 169.0        3rd Qu.:1.0000   3rd Qu.:1.0000        3rd Qu.:5.00
 Max.   :5409.0        Max.   :2.0000   Max.   :1.0000        Max.   :5.00

第三種:采樣瀏覽

優(yōu)點(diǎn):可以抽出其中少數(shù)樣本看全部信息

> library(dplyr)
> sample_n(train, 4)
> sample_n(train, 4)
         people_id  activity_id       date activity_category char_1 char_2
513235  ppl_184793 act2_3805654 2023-02-25            type 2
1127284  ppl_29203 act2_1960547 2022-09-16            type 5
1174958 ppl_294918 act2_3624924 2022-10-19            type 3
1794311 ppl_390987  act2_633897 2023-02-10            type 2
        char_3 char_4 char_5 char_6 char_7 char_8 char_9   char_10 outcome
513235                                                      type 1       0
1127284                                                  type 1349       1
1174958                                                    type 23       0
1794311                                                     type 1       0

第四種:用戶友好的表格采樣瀏覽

優(yōu)點(diǎn):不自動(dòng)換行,按表格形式組織,直觀

> library(knitr)
> kable(sample_n(train, 4))
> kable(sample_n(train, 4))


|        |people_id  |activity_id  |date       |activity_category |char_1 |char_2 |char_3 |char_4 |char_5 |char_6 |char_7 |char_8 |char_9 |char_10   | outcome|
|:-------|:----------|:------------|:----------|:-----------------|:------|:------|:------|:------|:------|:------|:------|:------|:------|:---------|-------:|
|1784154 |ppl_389138 |act2_2793972 |2022-11-03 |type 5            |       |       |       |       |       |       |       |       |       |type 649  |       1|
|1138360 |ppl_294144 |act2_149226  |2022-09-18 |type 5            |       |       |       |       |       |       |       |       |       |type 1058 |       0|
|1698603 |ppl_373844 |act2_3579388 |2022-08-27 |type 4            |       |       |       |       |       |       |       |       |       |type 230  |       0|
|1505324 |ppl_351017 |act2_2570186 |2022-09-30 |type 5            |       |       |       |       |       |       |       |       |       |type 248  |       0|

請尊重原創(chuàng),轉(zhuǎn)載請注明來源網(wǎng)站www.shareditor.com以及原始鏈接地址

R語言中的管道

shell中管道非常方便,比如把一個(gè)文件中第二列按數(shù)字排序后去重可以寫成cat file | awk  '{print $2}' | sort -n -k 1 | uniq,那么R語言中的管道怎么用呢?我們先來看一個(gè)例子:

> library(dplyr)
> ggplot(filter(train, char_5 != ""), aes(x = outcome, fill = char_5)) + geom_bar(width = 0.6, position = "fill")

這個(gè)例子中有以下處理步驟:

1. 拿出train數(shù)據(jù)

2. 對train數(shù)據(jù)做過濾,過濾掉char_5這一列為空的樣本

3. 用過濾好的數(shù)據(jù)執(zhí)行g(shù)gplot畫圖

這三部如果用一層層管道操作就方便多了,實(shí)際上R語言為我們提供了這樣的管道,即把函數(shù)的第一個(gè)參數(shù)單獨(dú)提出來作為管道輸入,管道操作符是%>%,也就是可以這樣執(zhí)行:

> train %>%
+ filter(char_5 != "") %>%
+ ggplot(aes(x=outcome, fill=char_10))+geom_bar(width=0.6, position="fill")

那么管道到底有什么好處呢?我們來追蹤一下實(shí)際的過程來體會

假設(shè)我們樣本長這個(gè)樣子:

> library(knitr)
> kable(sample_n(train, 4))


|        |people_id  |activity_id  |date       |activity_category |char_1 |char_2 |char_3 |char_4 |char_5 |char_6 |char_7 |char_8 |char_9 |char_10   | outcome|
|:-------|:----------|:------------|:----------|:-----------------|:------|:------|:------|:------|:------|:------|:------|:------|:------|:---------|-------:|
|567545  |ppl_194099 |act2_1420548 |2023-02-08 |type 2            |       |       |       |       |       |       |       |       |       |type 1    |       0|
|115164  |ppl_112033 |act2_2209862 |2022-10-23 |type 5            |       |       |       |       |       |       |       |       |       |type 481  |       1|
|1616290 |ppl_369463 |act2_2515098 |2023-07-11 |type 4            |       |       |       |       |       |       |       |       |       |type 295  |       0|
|1714893 |ppl_376799 |act2_1464019 |2022-10-01 |type 5            |       |       |       |       |       |       |       |       |       |type 1907 |       0|

這時(shí)我們發(fā)現(xiàn)有一些列是空值,如果我希望了解一下其中的char_5都有哪些取值以及比例情況,我們可以這樣來做:

> train %>%
+ count(char_5)
# A tibble: 8 × 2
  char_5       n
   <chr>   <int>
1        2039676
2 type 1   49214
3 type 2   26982
4 type 3    6013
5 type 4    1995
6 type 5    5421
7 type 6   67989
8 type 7       1

現(xiàn)在我們看到了輸出了char_5和n兩列分別表示可能取值和頻次,但是還是不夠直觀,希望畫圖來看,那么我們繼續(xù):

> train %>%
+ count(char_5) %>%
+ ggplot(aes (x = reorder(char_5,n), y = n)) +
+ geom_bar(stat = "identity", fill = "light blue")

發(fā)現(xiàn)我們有很多空值,這時(shí)我們繼續(xù)調(diào)整:

> train %>%
+ filter(char_5!="") %>%
+ count(char_5) %>%
+ ggplot(aes (x = reorder(char_5,n), y = n)) +
+ geom_bar(stat = "identity", fill = "light blue")

這就是我們的管道的作用:一步一步調(diào)試,不需要總想著把參數(shù)插到函數(shù)的哪個(gè)位置

回到本源,最基本的作圖

有人會說,R語言怎么總是畫這么復(fù)雜的圖像,但是卻連最基本的散點(diǎn)圖折線圖都不能畫嗎?下面回到本源,來展示一下R語言的最基本的作圖功能。

散點(diǎn)圖

> a <- c(49, 26, 69, 19, 54, 67, 19, 33)
> plot(a)

如果希望看到變化趨勢,我們可以畫折線圖,加上type即可

> plot(a, type='b')

如果這是一個(gè)每日消費(fèi)金額,我們想看累積消費(fèi)怎么辦?我們可以利用累積函數(shù)cumsum,它的功能像這個(gè)樣子:

> a
[1] 49 26 69 19 54 67 19 33
> cumsum(a)
[1]  49  75 144 163 217 284 303 336
>

那么可以這樣作圖:

> plot(cumsum(a), type='b')

最后讓我們用一個(gè)完美的正弦曲線收筆:

> x1 <- 0:100
> x2 <- x1 * 2 * pi / 100
> Y = sin(x2)
> par(family='STXihei') # 這句是為了解決圖像中中文亂碼問題
> plot(x2, Y, type='l', main='正弦曲線', xlab='x軸', ylab='y軸')

    


數(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)的第一個(gè)參數(shù)驗(yàn)證碼對象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個(gè)配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺檢測極驗(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ù)說明請參見: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 = '請輸入'+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); }