用R中的神经网络预测值

时间:2015-01-22 07:57:32

标签: r neural-network

我是使用R的新手,但是我越喜欢使用R,我就越喜欢它。 但是,我尝试将R用于人工神经网络。

我的输入数据是带有测量土壤属性的csv-Table。 磷,pH,粘土含量,粉砂含量等。

目标是训练具有neuralnet的神经网络,并通过使用测量的pH,粘土含量,淤泥含量等来预测磷(P)。

实际上我的下面的脚本用于训练网络,但我不知道,如果脚本通过计算带有解释变量的测试数据集确实预测了P.

代码如下。

library(neuralnet)

getwd("F:/Dropbox/Agrar/Fernerkundung_Agrar/ANN_Test/") 
setwd("F:/Dropbox/Agrar/Fernerkundung_Agrar/ANN_Test/")
library(gdata)


# This is a script to train a neural net and test it afterward.
# Objectives are to extimate Phosphorus-Contents in Soil from other variables
# Again. Targetparameter is P (Phosphorus) which has to be estimated from pH, ECa (Soil apparent electrical conductivity) (and later other input variables)

# Traininginput is a CSV-File with measured varaiables P, pH, ECa [here called EC2509 because it was measured in September 25th]
traininginput = read.csv("in.csv", header = TRUE, sep = ";", dec = ".")


#Train the neural network
#Going to have 3 hidden layers -> thumb rule input + output variables
#Threshold is a numeric value specifying the threshold for the partial derivatives of the error function as stopping criteria.
# All later considerd parameters are P~ + PH + clay_A + silt_A + sand_A + LS + A_ABAG + TWI + EC2509
# First let's try P and two variables 
net.Phos <- neuralnet(P~PH,traininginput, hidden=3, threshold=0.01, rep = 100) # stepmax = 1000000 will be included later


print(net.Phos)
write.table(net.Phos,"ann_setup.txt", header = TRUE, sep = " ", dec = ".")

#Plot the neural network
png(filename="ANN.png")
pdf(filename="ANN.pdf")
plot(net.Phos)
dev.off()

#Test the neural network: Testdata contains measured variables P, pH, ECa and others. Exactly the same setup than input
testdata = read.csv("testann.csv", header = TRUE, sep = ";", dec = ".")
head(testdata)
net.test <- compute(net.Phos, testdata$PH) #Run them through the neural network

#Lets see what properties net.sqrt has
ls(net.Phos)
write.table(net.Phos,"ann_properties.txt", header = TRUE, sep = ",", dec = ".")

#Lets see the test results
print(net.test$neurons[[1]])
print(net.test$net.test)


#Lets display a better version of the results
cleanoutput <- cbind(testdata$PH,testdata$P,
    as.data.frame(net.results$net.result[,1]))
colnames(cleanoutput) <- c("PH","Expected P","Neural Net P")
print(cleanoutput)
write.csv(Cleanout,"ann_setup.csv", header = TRUE, sep = " ", dec = ".")

0 个答案:

没有答案