使用nnet进行预测,我做对了吗? [英] Using nnet for prediction, am i doing it right?
问题描述
我对R和AI还是很陌生/ ML 技术.我想使用神经网络进行预测,由于我是新手,所以我只想看看这是应该怎么做的.
I'm still pretty new to R and AI / ML techniques. I would like to use a neural net for prediction, and since I'm new I would just like to see if this is how it should be done.
作为一个测试用例,我基于两个先前的值来预测sin()
的值.为了进行培训,我使用y = sin(x)
,x1 = sin(x-1)
,x2 = sin(x-2)
创建一个数据框,然后使用公式y ~ x1 + x2
.
As a test case, I'm predicting values of sin()
, based on 2 previous values. For training I create a data frame withy = sin(x)
, x1 = sin(x-1)
, x2 = sin(x-2)
, then use the formula y ~ x1 + x2
.
这似乎可行,但我只是想知道这是否是正确的方法,或者是否有更惯用的方法.
It seems to work, but I am just wondering if this is the right way to do it, or if there is a more idiomatic way.
这是代码:
require(quantmod) #for Lag()
requre(nnet)
x <- seq(0, 20, 0.1)
y <- sin(x)
te <- data.frame(y, Lag(y), Lag(y,2))
names(te) <- c("y", "x1", "x2")
p <- nnet(y ~ x1 + x2, data=te, linout=TRUE, size=10)
ps <- predict(p, x1=y)
plot(y, type="l")
lines(ps, col=2)
谢谢
这对于预测通话更好吗?
Is this better for the predict call?
t2 <- data.frame(sin(x), Lag(sin(x)))
names(t2) <- c("x1", "x2")
vv <- predict(p, t2)
plot(vv)
我想我想通过查看nnet的预测(它应该近似于正弦波)来看到nnet实际上正在工作.
I guess I'd like to see that the nnet is actually working by looking at its predictions (which should approximate a sin wave.)
推荐答案
我真的很喜欢caret
软件包,因为它为各种模型(例如nnet
)提供了一个很好的统一接口.此外,它使用交叉验证或自举重新采样自动调整超参数(例如size
和decay
).缺点是所有这些重新采样都需要一些时间.
I really like the caret
package, as it provides a nice, unified interface to a variety of models, such as nnet
. Furthermore, it automatically tunes hyperparameters (such as size
and decay
) using cross-validation or bootstrap re-sampling. The downside is that all this re-sampling takes some time.
#Load Packages
require(quantmod) #for Lag()
require(nnet)
require(caret)
#Make toy dataset
y <- sin(seq(0, 20, 0.1))
te <- data.frame(y, x1=Lag(y), x2=Lag(y,2))
names(te) <- c("y", "x1", "x2")
#Fit model
model <- train(y ~ x1 + x2, te, method='nnet', linout=TRUE, trace = FALSE,
#Grid of tuning parameters to try:
tuneGrid=expand.grid(.size=c(1,5,10),.decay=c(0,0.001,0.1)))
ps <- predict(model, te)
#Examine results
model
plot(y)
lines(ps, col=2)
它也可以在适当的范围内进行预测,因此您可以直接比较结果.如果您对神经网络感兴趣,还应该看看neuralnet
和RSNNS
软件包. caret
当前可以调整nnet
和neuralnet
模型,但是还没有用于RSNNS
的界面.
It also predicts on the proper scale, so you can directly compare results. If you are interested in neural networks, you should also take a look at the neuralnet
and RSNNS
packages. caret
can currently tune nnet
and neuralnet
models, but does not yet have an interface for RSNNS
.
/edit:caret
现在具有用于RSNNS
的界面.事实证明,如果您向软件包维护者发送电子邮件并要求将模型添加到caret
中,他通常会这样做!
/edit: caret
now has an interface for RSNNS
. It turns out if you email the package maintainer and ask that a model be added to caret
he'll usually do it!
/edit:caret
现在还支持 brnn 软件包.此外,插入符号现在还可以更轻松地指定您自己的定制模型 ,以与您喜欢的任何神经网络软件包进行交互!
/edit: caret
also now supports Bayesian regularization for feed-forward neural networks from the brnn package. Furthermore, caret now also makes it much easier to specify your own custom models, to interface with any neural network package you like!
这篇关于使用nnet进行预测,我做对了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!