差分
この文書の現在のバージョンと選択したバージョンの差分を表示します。
両方とも前のリビジョン 前のリビジョン 次のリビジョン | 前のリビジョン | ||
memo:r:rmachinelearning [2013/07/31 05:26] hasegawa |
memo:r:rmachinelearning [2016/07/17 04:22] (現在) |
||
---|---|---|---|
ライン 1: | ライン 1: | ||
====== Rで機械学習 ====== | ====== Rで機械学習 ====== | ||
+ | |||
===== 混合行列 Confusion Matrix ===== | ===== 混合行列 Confusion Matrix ===== | ||
ライン 12: | ライン 13: | ||
``` | ``` | ||
- | library(randomForest);library(caret) | + | library(randomForest);library(caret) |
- | data <- iris | + | data <- iris |
- | + | ||
- | ## データをランダムに並べ替え | + | ## データをランダムに並べ替え |
- | data <- data[sample(1:nrow(data),replace=F),] | + | data <- data[sample(1:nrow(data),replace=F),] |
- | + | ||
- | ## データの半分を学習用データ,残りの半分を検証用データに分割 | + | ## データの半分を学習用データ,残りの半分を検証用データに分割 |
- | data.learn <- data[1:(nrow(data)/2),] | + | data.learn <- data[1:(nrow(data)/2),] |
- | data.test <- data[((nrow(data)/2)+1):nrow(data),] | + | data.test <- data[((nrow(data)/2)+1):nrow(data),] |
- | + | ||
- | ## ランダムフォレストで分類モデルを作成 | + | ## ランダムフォレストで分類モデルを作成 |
- | model.rf <- randomForest(Species~., data=data.learn, type="class") | + | model.rf <- randomForest(Species~., data=data.learn, type="class") |
- | + | ||
- | ## 作成したモデルで予測 | + | ## 作成したモデルで予測 |
- | predicted <- predict(model.rf, data=data.test) | + | predicted <- predict(model.rf, data=data.test) |
- | + | ||
- | ## 観測値 | + | ## 観測値 |
- | observed <- factor(data.test$Species) | + | observed <- factor(data.test$Species) |
- | + | ||
- | ## [Tips] 観測値の因子水準を使って,予測値を因子に変換 | + | ## [Tips] 観測値の因子水準を使って,予測値を因子に変換 |
- | predicted <- factor(predicted,levels=levels(observed)) | + | predicted <- factor(predicted,levels=levels(observed)) |
- | + | ||
- | ## table関数を使って混合行列を作成 | + | ## table関数を使って混合行列を作成 |
- | (cm <- table(predicted,observed)) | + | (cm <- table(predicted,observed)) |
- | + | ||
- | ## confusionMatrix関数を使って混合行列を作成し,有効性指標も算出 | + | ## confusionMatrix関数を使って混合行列を作成し,有効性指標も算出 |
- | confusionMatrix(predicted,observed) | + | cm2 <- confusionMatrix(predicted,observed) |
``` | ``` | ||
ライン 51: | ライン 52: | ||
versicolor 9 8 9 | versicolor 9 8 9 | ||
virginica 5 9 9 | virginica 5 9 9 | ||
+ | ``` | ||
- | > confusionMatrix(predicted,observed) | + | ``` |
+ | > (cm2 <- confusionMatrix(predicted,observed)) | ||
Confusion Matrix and Statistics | Confusion Matrix and Statistics | ||
ライン 82: | ライン 85: | ||
Detection Prevalence 0.3467 0.3467 0.3067 | Detection Prevalence 0.3467 0.3467 0.3067 | ||
``` | ``` | ||
+ | |||
+ | 混合行列をLaTeXで使う | ||
+ | |||
+ | ``` | ||
+ | cm2 <- confusionMatrix(predicted,observed) | ||
+ | |||
+ | latex( | ||
+ | cm2$table | ||
+ | , digits = 3 | ||
+ | , file="cm2conf.tex" | ||
+ | , title = "" # 表の1行1列目の内容 | ||
+ | , label = "tab_ConfusionMatrix" # LaTeXの\label | ||
+ | , caption = "Confusion matrix" # LaTeXの\caption | ||
+ | , here = T # \begin{table}[H]を指定 ctableだと効かないので,texファイルに手作業で追記する | ||
+ | , center = "centering" # center環境の代わりに\centerを使う.booktabだと適切に動作しない | ||
+ | ) | ||
+ | |||
+ | latex( | ||
+ | t(cm2$byClass) # 表が横長過ぎるので,転置する | ||
+ | , digits = 3 # 小数第3位まで表示 | ||
+ | , ctable = T # 論文向きのctable | ||
+ | , file="cm2indicator.tex" | ||
+ | , title = "" # 表の1行1列目の内容 | ||
+ | , label = "tab_EffectivenessIndicator" # LaTeXの\label | ||
+ | , caption = "Effectiveness indicator" # LaTeXの\caption | ||
+ | , here = T # \begin{table}[H]を指定 ctableだと効かないので,texファイルに手作業で追記する | ||
+ | , center = "centering" # center環境の代わりに\centerを使う.booktabだと適切に動作しない | ||
+ | ) | ||
+ | ``` | ||
+ | |||
[Tips] 観測値の因子水準を使って,予測値を因子に変換 | [Tips] 観測値の因子水準を使って,予測値を因子に変換 | ||
ライン 113: | ライン 146: | ||
str(cm2) | str(cm2) | ||
- | 混合行列(tableオブジェクト)から見た目そのままにデータフレームに変換する関数作っておく | + | 混合行列(tableオブジェクト)から見た目そのままにデータフレームに変換する関数Table2Dataframeを作っておく |
- | tbl2df <- function(tbl){ | + | Table2Dataframe <- function(tbl){ |
df <- data.frame(matrix(tbl, nrow = nrow(tbl))) | df <- data.frame(matrix(tbl, nrow = nrow(tbl))) | ||
colnames(df) <- colnames(tbl) | colnames(df) <- colnames(tbl) | ||
ライン 121: | ライン 154: | ||
} | } | ||
# 実行例 | # 実行例 | ||
- | (df <- tbl2df(tbl = cm)) | + | (df <- Table2Dataframe(tbl = cm)) |
is.data.frame(df) | is.data.frame(df) | ||