title: “機械学習” subtitle: “演習課題” date: “2023-10-17” output: html_document: toc: yes toc_float: yes number_sections: no —
rm(list = ls()) # 全オブジェクト削除
# ここにRコードを記入する。
# 例)
plot(1:9)
#決定木 演習1
d <- read.csv('https://stats.dip.jp/01_ds/data/bike_rental.csv')
library(DT)
datatable(d, options = list(pageLength = 5))
library(rpart)
library(rpart.plot)
# カラーパレット
COL <- c(rgb(255, 0, 0, 105, max = 255), # 赤
rgb( 0, 0, 255, 105, max = 255), # 青
rgb( 0, 155, 0, 105, max = 255), # 緑
rgb(100, 100, 100, 55, max = 255)) # 灰
tree <-rpart(レンタル数 ~ 季節 + 月 + 祝日 + 曜日 + 休日 + 天気 + 気温 + 湿度 + 風速, data = d)
rpart.plot(tree, type = 5)
rpart.plot(tree, branch.type = 5)
#決定木 演習2
d<- read.csv('https://stats.dip.jp/01_ds/data/iris.csv')
datatable(d, options=list(pageLength=5))
library(rpart)
library(rpart.plot)
tree <- rpart(Species ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width,
data = d, method = 'class', cp = 0.02)
rpart.plot(tree, type = 5)
rpart.plot(tree, branch.type = 5)
plotcp(tree)
tree2 <- prune(tree, cp = 0.1)
rpart.plot(tree2, branch.type = 5)
tree3 <- prune(tree, cp = 0.02)
rpart.plot(tree3, branch.type = 5)
A.versicolor