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

熱線電話:13121318867

登錄
首頁精彩閱讀R語言基本操作
R語言基本操作
2017-02-20
收藏

R語言基本操作

R語言和Python類似,也是有許許多多的包(Python中是模塊)組成,具體用什么就安裝什么,用的時(shí)候再去加載。

1、安裝包:

語法:install.packages("包名")(兩個(gè)引號(hào)""不能省略)

如:下面就是安裝強(qiáng)大的繪圖包 ggplot2

>install.packages("ggplot2")

2、加載包

語法:library("包名")

>library("ggplot2")

3、查看數(shù)據(jù)類型

語法:class(變量)(數(shù)據(jù)框(data.frame)是R語言特有的數(shù)據(jù)格式)

> class(iris)

[1] "data.frame"

4、取數(shù)據(jù)框中的行和列

語法:<數(shù)據(jù)框>[行,列](如果取從..行(列)到...行(列),中間用:劃分)

還是以iris的數(shù)據(jù)集為例子。

取第十行:

>iris[10,]

取第一行到第100行

>iris[1:100,]

取第三列:

>iris[,3]

取第一列到第三列:

>iris[,1:3]

如果要連續(xù)取多行(列)的話用c來取

iris[c(1:35,50:85,100:135),]

取列:

iris[,c(1:2,4:5)]

5、數(shù)據(jù)框拼接

按行拼接:

rbind

用法:rbind(data.frame1,data.datafram2,........)

> train_data1=iris[1:5,]

> train_data2=iris[7:8,]

> rbind(train_data1,train_data2) 

Sepal.Length Sepal.Width Petal.Length Petal.Width Species

1          5.1         3.5          1.4         0.2  setosa

2          4.9         3.0          1.4         0.2  setosa

3          4.7         3.2          1.3         0.2  setosa

4          4.6         3.1          1.5         0.2  setosa

5          5.0         3.6          1.4         0.2  setosa

7          4.6         3.4          1.4         0.3  setosa

8          5.0         3.4          1.5         0.2  setosa

按列拼接:

cbind

用法:cbind(data.frame1,data.datafram2,........)

> iris[3:4,] 

Sepal.Length Sepal.Width Petal.Length Petal.Width Species

3          4.7         3.2          1.3         0.2  setosa

4          4.6         3.1          1.5         0.2  setosa

> iris[100:101,]   

Sepal.Length Sepal.Width Petal.Length Petal.Width    Species

100          5.7         2.8          4.1         1.3 versicolor

101          6.3         3.3          6.0         2.5  virginica

> train_data1=iris[3:4,1:2]

> train_data2=iris[100:101,4:5]

> cbind(train_data1,train_data2) 

Sepal.Length Sepal.Width Petal.Width    Species

3          4.7         3.2         1.3 versicolor

4          4.6         3.1         2.5  virginica

利用iris的數(shù)據(jù)來比較隨機(jī)森林支持向量機(jī)在分類中的作用

#加載支持向量機(jī)的包

library("e1071")

library(kernlab)

#加載隨機(jī)森林

library("randomForest")

#加載iris的數(shù)據(jù)集

data_iris=iris

#得到訓(xùn)練數(shù)據(jù)集

train_data=data_iris[c(1:35,50:85,100:135),]

#得到測試數(shù)據(jù)集

test_data=data_iris[c(35:50,85:100,135:150),]

#隨機(jī)森林測試

rFM=randomForest(Species~.,data=train_data,importance=TRUE)

rfm_result=predict(rFM,test_data[,c(1:4)])

#支持向量機(jī)測試

svmmodel=svm(Species ~ ., data =train_data)

svm_result=predict(svmmodel,test_data[,c(1:4)])

#隨機(jī)森林比較

a=0

b=1

while(b<=48){

if(rfm_result[b]==test_data[,5][b]){

a=a+1

}else{

a=a

}

b=b+1

}

#隨機(jī)森林正確率

a/48

#支持向量機(jī)比較

j=0

i=1

while(i<=48){

if(svm_result[i]==test_data[,5][i]){

j=j+1

}else{

j=j

}

i=i+1

}

#支持向量機(jī)正確率

j/48

結(jié)果是:隨機(jī)森林正確率百分之百,支持向量機(jī)是0.979.

出現(xiàn)這個(gè)結(jié)果應(yīng)該是數(shù)據(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)檢測極驗(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ù)說明請(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); }