mirror of
https://github.com/ArthurDanjou/ArtStudies.git
synced 2026-02-14 10:07:38 +01:00
Add Random Forest analysis notebook for Machine Learning 2
This commit is contained in:
79
M2/Machine Learning 2/Random_forest.Rmd
Normal file
79
M2/Machine Learning 2/Random_forest.Rmd
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
```{r}
|
||||||
|
library(randomForest)
|
||||||
|
library(MASS)
|
||||||
|
library(tree)
|
||||||
|
data(Boston)
|
||||||
|
```
|
||||||
|
|
||||||
|
## Sampling
|
||||||
|
|
||||||
|
```{r}
|
||||||
|
set.seed(123)
|
||||||
|
appr = sample(1:nrow(Boston), nrow(Boston) / 2)
|
||||||
|
```
|
||||||
|
|
||||||
|
## Regression Tree
|
||||||
|
|
||||||
|
```{r}
|
||||||
|
arbre.boston = tree(medv ~ ., Boston, subset = appr)
|
||||||
|
summary(arbre.boston)
|
||||||
|
plot(arbre.boston)
|
||||||
|
text(arbre.boston, pretty = 0)
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
```{r}
|
||||||
|
cv.boston = cv.tree(arbre.boston)
|
||||||
|
plot(cv.boston$size, cv.boston$dev, type = 'b')
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
```{r}
|
||||||
|
yprev = predict(arbre.boston, newdata = Boston[-appr, ])
|
||||||
|
boston.test = Boston[-appr, "medv"]
|
||||||
|
plot(yprev, boston.test)
|
||||||
|
abline(0, 1)
|
||||||
|
mean((yprev - boston.test)^2)
|
||||||
|
```
|
||||||
|
|
||||||
|
## Bagging
|
||||||
|
|
||||||
|
```{r}
|
||||||
|
set.seed(123)
|
||||||
|
|
||||||
|
bag.boston = randomForest(
|
||||||
|
medv ~ .,
|
||||||
|
data = Boston,
|
||||||
|
subset = appr,
|
||||||
|
mtry = 13,
|
||||||
|
importance = TRUE
|
||||||
|
)
|
||||||
|
bag.boston
|
||||||
|
yprev.bag = predict(bag.boston, newdata = Boston[-appr, ])
|
||||||
|
plot(yprev.bag, boston.test)
|
||||||
|
abline(0, 1)
|
||||||
|
mean((yprev.bag - boston.test)^2)
|
||||||
|
```
|
||||||
|
|
||||||
|
## Random Forest
|
||||||
|
|
||||||
|
```{r}
|
||||||
|
set.seed(123)
|
||||||
|
rf.boston = randomForest(
|
||||||
|
medv ~ .,
|
||||||
|
data = Boston,
|
||||||
|
subset = appr,
|
||||||
|
mtry = 6,
|
||||||
|
importance = TRUE
|
||||||
|
)
|
||||||
|
yprev.rf = predict(rf.boston, newdata = Boston[-appr, ])
|
||||||
|
mean((yprev.rf - boston.test)^2)
|
||||||
|
```
|
||||||
|
|
||||||
|
## Variable importance
|
||||||
|
|
||||||
|
|
||||||
|
```{r}
|
||||||
|
importance(rf.boston)
|
||||||
|
varImpPlot(rf.boston)
|
||||||
|
```
|
||||||
Reference in New Issue
Block a user