R語言 隨機森林演算法

2021-08-19 15:49:11 字數 2615 閱讀 4919

在隨機森林方法中,建立大量的決策樹。 每個觀察被饋入每個決策樹。 每個觀察的最常見的結果被用作最終輸出。 新的觀察結果被饋入所有的樹並且對每個分類模型取多數投票。

對構建樹時未使用的情況進行錯誤估計。 這稱為oob(袋外)誤差估計,其被提及為百分比。

r語言包「randomforest」用於建立隨機森林。

安裝r包

在r語言控制台中使用以下命令安裝軟體包。 您還必須安裝相關軟體包(如果有)。

install.packages("randomforest)
包「randomforest」具有函式randomforest(),用於建立和分析隨機森林。

語法在r語言中建立隨機森林的基本語法是 -

randomforest(formula, data)
以下是所使用的引數的描述 -

輸入資料

我們將使用名為readingskills的r語言內建資料集來建立決策樹。 它描述了某人的readingskills的分數,如果我們知道變數「age」,「shoesize」,「score」,以及該人是否是母語。

以下是示例資料。

# load the party package. it will automatically load other required packages.

library(party)

# print some records from data set readingskills.

print(head(readingskills))

當我們執行上面的**,它產生以下結果及圖表 -

nativespeaker   age   shoesize      score

1 yes 5

24.83189

32.29385

2 yes 6

25.95238

36.63105

3 no 11

30.42170

49.60593

4 yes 7

28.66450

40.28456

5 yes 11

31.88207

55.46085

6 yes 10

30.07843

52.83124

loading required package: methods

loading required package: grid

......

......

......

......

......

....

......

......

......

......

....

我們將使用randomforest()函式來建立決策樹並檢視它的圖。

# load the party package. it will automatically load other required packages.

library(party)

library(randomforest)

# create the forest.

output.forest <- randomforest(nativespeaker ~ age + shoesize + score,

data = readingskills)

# view the forest results.

print(output.forest)

# importance of each predictor.

print(importance(fit,type = 2))

當我們執行上面的**,它產生以下結果 -

call:

randomforest(formula = nativespeaker ~ age + shoesize + score,

data = readingskills)

type of random forest: classification

number of

trees: 500

no. of variables tried at each split: 1

oob estimate of error rate: 1%

confusion matrix:

noyesclass.error

no99

10.01

yes1

990.01

meandecreasegini

age 13.95406

shoesize 18.91006

score 56.73051

結論

從上面顯示的隨機森林,我們可以得出結論,鞋碼和成績是決定如果某人是母語者或不是母語的重要因素。 此外,該模型只有1%的誤差,這意味著我們可以**精度為99%。

隨機森林(R)

random forest install.packages randomforest library randomforest data iris attach iris table iris species class as.factor iris species 描述 biplot princ...

R語言之裝袋 adaboost 隨機森林演算法

首先,這三個演算法都是分類演算法,分類的準確率很高,這些方法都是組合多個分類器,每個分類器分別進行 通過簡單選舉多數,判定最終所屬分類。為什麼組合分類器能提高分類準確率 可以通過下面的圖進行解釋。左圖單個分類器就是圖上的對角線,當進行多個組合時,出現了圖上的折線圖,每個摺邊都是乙個分類器,當有多個分...

R語言 訓練隨機森林模型

隨機森林演算法涉及對樣本單元和變數進行抽樣,從而生成大量決策樹。對於每個樣本單元,所有決策樹依次對其進行分類,所有決策樹 類別中的眾數類別即為隨機森林所 的這一樣本單元的類別。假設訓練集中共有n個樣本單元,m個變數,則隨機森林演算法如下 1 從訓練集中隨機有放回地抽取n個樣本單元,生成大量決策樹 2...