當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


R predict.nnet 通過訓練有素的神經網絡預測新示例

R語言 predict.nnet 位於 nnet 包(package)。

說明

通過訓練有素的神經網絡預測新的例子。

用法

## S3 method for class 'nnet'
predict(object, newdata, type = c("raw","class"), ...)

參數

object

nnet 返回的類 nnet 的對象。

newdata

測試示例的矩陣或 DataFrame 。向量被認為是包含單個情況的行向量。

type

輸出類型

...

傳遞給其他方法或從其他方法傳遞的參數。

細節

此函數是類 "nnet" 的通用函數 predict() 的方法。可以通過為適當類的對象 x 調用 predict(x) 來調用它,也可以直接通過調用 predict.nnet(x) 來調用它,而不管對象的類如何。

如果是 type = "raw" ,則為經過訓練的網絡返回的值矩陣;如果 type = "class" ,則對應的類(可能僅在網絡由 nnet.formula 生成時才有用)。

例子

# use half the iris data
ir <- rbind(iris3[,,1], iris3[,,2], iris3[,,3])
targets <- class.ind( c(rep("s", 50), rep("c", 50), rep("v", 50)) )
samp <- c(sample(1:50,25), sample(51:100,25), sample(101:150,25))
ir1 <- nnet(ir[samp,], targets[samp,],size = 2, rang = 0.1,
            decay = 5e-4, maxit = 200)
test.cl <- function(true, pred){
        true <- max.col(true)
        cres <- max.col(pred)
        table(true, cres)
}
test.cl(targets[-samp,], predict(ir1, ir[-samp,]))

# or
ird <- data.frame(rbind(iris3[,,1], iris3[,,2], iris3[,,3]),
        species = factor(c(rep("s",50), rep("c", 50), rep("v", 50))))
ir.nn2 <- nnet(species ~ ., data = ird, subset = samp, size = 2, rang = 0.1,
               decay = 5e-4, maxit = 200)
table(ird$species[-samp], predict(ir.nn2, ird[-samp,], type = "class"))

參考

Ripley, B. D. (1996) Pattern Recognition and Neural Networks. Cambridge.

Venables, W. N. and Ripley, B. D. (2002) Modern Applied Statistics with S. Fourth edition. Springer.

也可以看看

nnet , which.is.max

相關用法


注:本文由純淨天空篩選整理自R-devel大神的英文原創作品 Predict New Examples by a Trained Neural Net。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。