如何在R语言中进行神经网络模型的建立

Python021

如何在R语言中进行神经网络模型的建立,第1张

不能发链接,所以我复制过来了。

#载入程序和数据

library(RSNNS)

data(iris)

#将数据顺序打乱

iris <- iris[sample(1:nrow(iris),length(1:nrow(iris))),1:ncol(iris)]

#定义网络输入

irisValues <- iris[,1:4]

#定义网络输出,并将数据进行格式转换

irisTargets <- decodeClassLabels(iris[,5])

#从中划分出训练样本和检验样本

iris <- splitForTrainingAndTest(irisValues, irisTargets, ratio=0.15)

#数据标准化

iris <- normTrainingAndTestSet(iris)

#利用mlp命令执行前馈反向传播神经网络算法

model <- mlp(iris$inputsTrain, iris$targetsTrain, size=5, learnFunc="Quickprop", learnFuncParams=c(0.1, 2.0, 0.0001, 0.1),maxit=100, inputsTest=iris$inputsTest, targetsTest=iris$targetsTest)

#利用上面建立的模型进行预测

predictions <- predict(model,iris$inputsTest)

#生成混淆矩阵,观察预测精度

confusionMatrix(iris$targetsTest,predictions)

#结果如下:

#predictions

#targets 1 2 3

# 1 8 0 0

# 2 0 4 0

# 3 0 1 10

建立m函数文件存为logistic1

function f=logistic1(b)

t=[0,5,10,24,33,48,57,72,96,120,144,168,192,216]y=[0,0.028,0.103,0.336,0.450,0.597,0.716,0.778,0.835,0.849,0.816,0.839,0.811,0.816]

f = y-b(1)./(1+b(2).*exp(-b(3).*t))

b0=[10,2,2]

>>b=leastsq('logistic1',b0)

b =

0.8221 13.9173 0.0818

或者cftool

General model:

f(x) = b/(1+a*exp(-k*x))

Coefficients (with 95% confidence bounds):

a = 13.92 (6.301,21.53)

b = 0.822 (0.7911,0.853)

k = 0.08184 (0.06479,0.0989)

Goodness of fit:

SSE:0.01404

R-square:0.9898

Adjusted R-square:0.9879

RMSE:0.03572