導航:首頁 > 源碼編譯 > 人工智慧決策樹演算法里有r嗎

人工智慧決策樹演算法里有r嗎

發布時間:2022-08-10 09:46:28

㈠ 如何用R語言實現決策樹C5.0模型

R有專門的C5.0包:C50

㈡ 使用R完成決策樹分類

使用R完成決策樹分類
傳統的ID3和C4.5一般用於分類問題,其中ID3使用信息增益進行特徵選擇,即遞歸的選擇分類能力最強的特徵對數據進行分割,C4.5唯一不同的是使用信息增益比進行特徵選擇。
特徵A對訓練數據D的信息增益g(D, A) = 集合D的經驗熵H(D) - 特徵A給定情況下D的經驗條件熵H(D|A)
特徵A對訓練數據D的信息增益比r(D, A) = g(D, A) / H(D)
而CART(分類與回歸)模型既可以用於分類、也可以用於回歸,對於回歸樹(最小二乘回歸樹生成演算法),需要尋找最優切分變數和最優切分點,對於分類樹(CART生成演算法),使用基尼指數選擇最優特徵。
一個使用rpart完成決策樹分類的例子如下:
[plain] view plain
library(rpart);

## rpart.control對樹進行一些設置
## xval是10折交叉驗證
## minsplit是最小分支節點數,這里指大於等於20,那麼該節點會繼續分劃下去,否則停止
## minbucket:葉子節點最小樣本數
## maxdepth:樹的深度
## cp全稱為complexity parameter,指某個點的復雜度,對每一步拆分,模型的擬合優度必須提高的程度
ct <- rpart.control(xval=10, minsplit=20, cp=0.1)

## kyphosis是rpart這個包自帶的數據集
## na.action:缺失數據的處理辦法,默認為刪除因變數缺失的觀測而保留自變數缺失的觀測。
## method:樹的末端數據類型選擇相應的變數分割方法:
## 連續性method=「anova」,離散型method=「class」,計數型method=「poisson」,生存分析型method=「exp」
## parms用來設置三個參數:先驗概率、損失矩陣、分類純度的度量方法(gini和information)
## cost我覺得是損失矩陣,在剪枝的時候,葉子節點的加權誤差與父節點的誤差進行比較,考慮損失矩陣的時候,從將「減少-誤差」調整為「減少-損失」
fit <- rpart(Kyphosis~Age + Number + Start,
data=kyphosis, method="class",control=ct,
parms = list(prior = c(0.65,0.35), split = "information"));

## 第一種
par(mfrow=c(1,3));
plot(fit);
text(fit,use.n=T,all=T,cex=0.9);

## 第二種,這種會更漂亮一些
library(rpart.plot);
rpart.plot(fit, branch=1, branch.type=2, type=1, extra=102,
shadow.col="gray", box.col="green",
border.col="blue", split.col="red",
split.cex=1.2, main="Kyphosis決策樹");

## rpart包提供了復雜度損失修剪的修剪方法,printcp會告訴分裂到每一層,cp是多少,平均相對誤差是多少
## 交叉驗證的估計誤差(「xerror」列),以及標准誤差(「xstd」列),平均相對誤差=xerror±xstd
printcp(fit);

## 通過上面的分析來確定cp的值
## 我們可以用下面的辦法選擇具有最小xerror的cp的辦法:
## prune(fit, cp= fit$cptable[which.min(fit$cptable[,"xerror"]),"CP"])

fit2 <- prune(fit, cp=0.01);
rpart.plot(fit2, branch=1, branch.type=2, type=1, extra=102,
shadow.col="gray", box.col="green",
border.col="blue", split.col="red",
split.cex=1.2, main="Kyphosis決策樹");
效果圖如下:

㈢ 如何用R 做決策樹

導入rpart包,rpart.plot包,
然後用rpart函數建立模型,
可以用prp函數觀察模型,
再用predict函數預測測試集。

㈣ 決策樹的演算法

C4.5演算法繼承了ID3演算法的優點,並在以下幾方面對ID3演算法進行了改進:
1) 用信息增益率來選擇屬性,克服了用信息增益選擇屬性時偏向選擇取值多的屬性的不足;
2) 在樹構造過程中進行剪枝;
3) 能夠完成對連續屬性的離散化處理;
4) 能夠對不完整數據進行處理。
C4.5演算法有如下優點:產生的分類規則易於理解,准確率較高。其缺點是:在構造樹的過程中,需要對數據集進行多次的順序掃描和排序,因而導致演算法的低效。此外,C4.5隻適合於能夠駐留於內存的數據集,當訓練集大得無法在內存容納時程序無法運行。
具體演算法步驟如下;
1創建節點N
2如果訓練集為空,在返回節點N標記為Failure
3如果訓練集中的所有記錄都屬於同一個類別,則以該類別標記節點N
4如果候選屬性為空,則返回N作為葉節點,標記為訓練集中最普通的類;
5for each 候選屬性 attribute_list
6if 候選屬性是連續的then
7對該屬性進行離散化
8選擇候選屬性attribute_list中具有最高信息增益率的屬性D
9標記節點N為屬性D
10for each 屬性D的一致值d
11由節點N長出一個條件為D=d的分支
12設s是訓練集中D=d的訓練樣本的集合
13if s為空
14加上一個樹葉,標記為訓練集中最普通的類
15else加上一個有C4.5(R - {D},C,s)返回的點 背景:
分類與回歸樹(CART——Classification And Regression Tree)) 是一種非常有趣並且十分有效的非參數分類和回歸方法。它通過構建二叉樹達到預測目的。
分類與回歸樹CART 模型最早由Breiman 等人提出,已經在統計領域和數據挖掘技術中普遍使用。它採用與傳統統計學完全不同的方式構建預測准則,它是以二叉樹的形式給出,易於理解、使用和解釋。由CART 模型構建的預測樹在很多情況下比常用的統計方法構建的代數學預測准則更加准確,且數據越復雜、變數越多,演算法的優越性就越顯著。模型的關鍵是預測准則的構建,准確的。
定義:
分類和回歸首先利用已知的多變數數據構建預測准則, 進而根據其它變數值對一個變數進行預測。在分類中, 人們往往先對某一客體進行各種測量, 然後利用一定的分類准則確定該客體歸屬那一類。例如, 給定某一化石的鑒定特徵, 預測該化石屬那一科、那一屬, 甚至那一種。另外一個例子是, 已知某一地區的地質和物化探信息, 預測該區是否有礦。回歸則與分類不同, 它被用來預測客體的某一數值, 而不是客體的歸類。例如, 給定某一地區的礦產資源特徵, 預測該區的資源量。

㈤ 數據分析之美 決策樹R語言實現

數據分析之美:決策樹R語言實現
R語言實現決策樹
1.准備數據
[plain] view plain
> install.packages("tree")
> library(tree)
> library(ISLR)
> attach(Carseats)
> High=ifelse(Sales<=8,"No","Yes") //set high values by sales data to calssify
> Carseats=data.frame(Carseats,High) //include the high data into the data source
> fix(Carseats)
2.生成決策樹
[plain] view plain

> tree.carseats=tree(High~.-Sales,Carseats)
> summary(tree.carseats)

[plain] view plain
//output training error is 9%
Classification tree:
tree(formula = High ~ . - Sales, data = Carseats)
Variables actually used in tree construction:
[1] "ShelveLoc" "Price" "Income" "CompPrice" "Population"
[6] "Advertising" "Age" "US"
Number of terminal nodes: 27
Resial mean deviance: 0.4575 = 170.7 / 373
Misclassification error rate: 0.09 = 36 / 400
3. 顯示決策樹
[plain] view plain

> plot(tree . carseats )
> text(tree .carseats ,pretty =0)
4.Test Error

[plain] view plain

//prepare train data and test data
//We begin by using the sample() function to split the set of observations sample() into two halves, by selecting a random subset of 200 observations out of the original 400 observations.
> set . seed (1)
> train=sample(1:nrow(Carseats),200)
> Carseats.test=Carseats[-train,]
> High.test=High[-train]
//get the tree model with train data
> tree. carseats =tree (High~.-Sales , Carseats , subset =train )
//get the test error with tree model, train data and predict method
//predict is a generic function for predictions from the results of various model fitting functions.
> tree.pred = predict ( tree.carseats , Carseats .test ,type =" class ")
> table ( tree.pred ,High. test)
High. test
tree. pred No Yes
No 86 27
Yes 30 57
> (86+57) /200
[1] 0.715

5.決策樹剪枝
[plain] view plain

/**
Next, we consider whether pruning the tree might lead to improved results. The function cv.tree() performs cross-validation in order to cv.tree() determine the optimal level of tree complexity; cost complexity pruning is used in order to select a sequence of trees for consideration.

For regression trees, only the default, deviance, is accepted. For classification trees, the default is deviance and the alternative is misclass (number of misclassifications or total loss).
We use the argument FUN=prune.misclass in order to indicate that we want the classification error rate to guide the cross-validation and pruning process, rather than the default for the cv.tree() function, which is deviance.

If the tree is regression tree,
> plot(cv. boston$size ,cv. boston$dev ,type=』b 』)
*/
> set . seed (3)
> cv. carseats =cv. tree(tree .carseats ,FUN = prune . misclass ,K=10)
//The cv.tree() function reports the number of terminal nodes of each tree considered (size) as well as the corresponding error rate(dev) and the value of the cost-complexity parameter used (k, which corresponds to α.
> names (cv. carseats )
[1] " size" "dev " "k" " method "
> cv. carseats
$size //the number of terminal nodes of each tree considered
[1] 19 17 14 13 9 7 3 2 1
$dev //the corresponding error rate
[1] 55 55 53 52 50 56 69 65 80
$k // the value of the cost-complexity parameter used
[1] -Inf 0.0000000 0.6666667 1.0000000 1.7500000
2.0000000 4.2500000
[8] 5.0000000 23.0000000
$method //miscalss for classification tree
[1] " misclass "
attr (," class ")
[1] " prune " "tree. sequence "

[plain] view plain

//plot the error rate with tree node size to see whcih node size is best
> plot(cv. carseats$size ,cv. carseats$dev ,type=』b 』)

/**
Note that, despite the name, dev corresponds to the cross-validation error rate in this instance. The tree with 9 terminal nodes results in the lowest cross-validation error rate, with 50 cross-validation errors. We plot the error rate as a function of both size and k.
*/
> prune . carseats = prune . misclass ( tree. carseats , best =9)
> plot( prune . carseats )
> text( prune .carseats , pretty =0)

//get test error again to see whether the this pruned tree perform on the test data set
> tree.pred = predict ( prune . carseats , Carseats .test , type =" class ")
> table ( tree.pred ,High. test)
High. test
tree. pred No Yes
No 94 24
Yes 22 60
> (94+60) /200
[1] 0.77

㈥ 人工智慧演算法有哪些

人工智慧演算法有:決策樹、隨機森林演算法、邏輯回歸、SVM、樸素貝葉斯、K最近鄰演算法、K均值演算法、Adaboost演算法、神經網路、馬爾可夫。

㈦ 如何用R進行決策樹C4.5演算法的運用

你是說對樣本是嘛?看你用決策樹來幹嘛?如果是分類的話,一般進行離散化,也就是每個feature的值屬於某幾種,如果是回歸預測的話,則直接使用原來的值~

㈧ 關於數據挖掘中決策樹的知識

在數據挖掘中,有很多的演算法是需要我們去學習的,比如決策樹演算法。在數據挖掘中,決策樹能夠幫助我們解決更多的問題。當然,關於決策樹的概念是有很多的,所以說我們需要多多學習多多總結,這樣才能夠學會並且學會數據挖掘的知識,在這篇文章中我們就重點為大家介紹一下關於決策樹的相關知識。
1.決策樹的演算法
決策樹的演算法是以樹狀結構表示數據分類的結果。一般情況,一棵決策樹包含一個根節點、若干個內部結點和若干個葉結點。而葉結點對應於決策結果,其他每個結點則對應於一個屬性測試;每個結點包含的樣本集合根據屬性測試的結果被劃分到子結點中;根結點包含樣本全集,從根結點到每個葉結點的路徑對應了一個判定測試序列。決策樹學習的目的就是為了產生一棵泛化能力強,即能處理未見示例能力強的決策樹。這些就是決策樹演算法的結構。
2.決策樹的原理
一般來說,決策樹歸納的基本演算法是貪心演算法,自頂向下以遞歸方式構造決策樹。而貪心演算法在每一步選擇中都採取在當前狀態下最優的選擇。在決策樹生成過程中,劃分選擇即屬性選擇度量是關鍵。通過屬性選擇度量,選擇出最好的將樣本分類的屬性。這樣就能夠方便數據屬性的劃分,然後,下一步是樹的剪枝。在決策樹學習中,為了盡可能正確分類訓練樣本,結點劃分過程將不斷重復,這樣才能夠使用決策樹解決很多的問題。而分類是數據挖掘中的一種應用方法,而決策樹則是一種典型的普遍使用的分類方法,並且決策樹技術早已被證明是利用計算機模擬人決策的有效方法。
3.決策樹的現狀
近年來隨著信息技術、計算機科學的迅速發展,決策樹作為重要方法之一,越來越受到人們的關注。而其在人工智慧方面的潛力以及與越來越多新技術的結合,由此可見,決策樹在數據挖掘乃至數據分析中還是有很長的使用時間,這就是決策樹至今經典的原因。
在這篇文章中我們給大家介紹了關於數據挖掘中決策樹的知識,當大家學習了決策樹的概念,決策樹的結構以決策樹的原理,就能夠掌握決策樹的基礎知識。不過要想學習數據挖掘,還是要學習更多的知識,希望這篇文章能夠幫助到大家。

㈨ r 語言的哪個程序包中包含決策樹分類演算法的實現函數

這種從數據產生決策樹的機器學習技術叫做決策樹學習, 通俗點說就是決策樹,說白了,這是一種依託於分類、訓練上的預測樹,根據已知預測、歸類未來。

閱讀全文

與人工智慧決策樹演算法里有r嗎相關的資料

熱點內容
粉筆教育app從哪裡看做過的題 瀏覽:391
app數據包在哪裡找到 瀏覽:923
百煉成仙綠帽改編1-11 瀏覽:107
女主和一對雙胞胎兄弟 瀏覽:437
刀劍神域小說TXT 瀏覽:1000
php獲取文件地址 瀏覽:578
linuxsed替換字元 瀏覽:413
如何填寫國家反詐中心app注冊使用 瀏覽:790
日本影視網站 瀏覽:933
伺服器點亮埠以後有什麼特徵 瀏覽:980
51單片機定時器pwm 瀏覽:685
民國修真 瀏覽:386
php數組作為參數傳遞 瀏覽:991
運行命令查ip 瀏覽:202
漲奶吃奶小說 瀏覽:340
股票十大戰法主圖指標源碼 瀏覽:702
查看網路的命令是什麼意思 瀏覽:589
《鬼吹燈》1-8全本txt 瀏覽:336
python繪圖兩個圓代碼 瀏覽:607
python中end的用法 瀏覽:682