Kasus 1*
library("neuralnet")
## Warning: package 'neuralnet' was built under R version 3.1.3
## Loading required package: grid
## Loading required package: MASS
traininginput <- as.data.frame(runif(50, min=0, max=100))
trainingoutput <- sqrt(traininginput)
trainingdata <- cbind(traininginput,trainingoutput)
colnames(trainingdata) <- c("Input","Output")
net.sqrt <- neuralnet(Output~Input,trainingdata, hidden=10, threshold=0.01)
print(net.sqrt)
## Call: neuralnet(formula = Output ~ Input, data = trainingdata, hidden = 10, threshold = 0.01)
##
## 1 repetition was calculated.
##
## Error Reached Threshold Steps
## 1 0.0005643073494 0.009592216057 16505
#Test the neural network on some training data
testdata <- as.data.frame((1:10)^2) #Generate some squared numbers
net.results <- compute(net.sqrt, testdata)
#Run them through the neural network
#Lets see what properties net.sqrt has
ls(net.results)
## [1] "net.result" "neurons"
#Lets see the results
print(net.results$net.result)
## [,1]
## [1,] 1.082991001
## [2,] 2.000640820
## [3,] 3.020899704
## [4,] 3.993782547
## [5,] 5.002344138
## [6,] 6.004458234
## [7,] 6.996583306
## [8,] 7.998473499
## [9,] 9.007460416
## [10,] 9.986290718
#Lets display a better version of the results
cleanoutput <- cbind(testdata,sqrt(testdata),
as.data.frame(net.results$net.result))
colnames(cleanoutput) <- c("Input","Expected Output","Neural Net Output")
print(cleanoutput)
## Input Expected Output Neural Net Output
## 1 1 1 1.082991001
## 2 4 2 2.000640820
## 3 9 3 3.020899704
## 4 16 4 3.993782547
## 5 25 5 5.002344138
## 6 36 6 6.004458234
## 7 49 7 6.996583306
## 8 64 8 7.998473499
## 9 81 9 9.007460416
## 10 100 10 9.986290718
Kasus 2*
library(neuralnet)
itrain <- iris[sample(1:150, 50),]
itrain$setosa <- c(itrain$Species == "setosa")
itrain$versicolor <- c(itrain$Species == "versicolor")
itrain$virginica <- c(itrain$Species == "virginica")
itrain$Species <- NULL
inet <- neuralnet(setosa + versicolor + virginica ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width, itrain, hidden=3, lifesign="full")
## hidden: 3 thresh: 0.01 rep: 1/1 steps: 1000 min thresh: 0.137180245
## 2000 min thresh: 0.09378143248
## 3000 min thresh: 0.07155774292
## 4000 min thresh: 0.07155774292
## 5000 min thresh: 0.07155774292
## 6000 min thresh: 0.07155774292
## 7000 min thresh: 0.07155774292
## 8000 min thresh: 0.07155774292
## 9000 min thresh: 0.07155774292
## 10000 min thresh: 0.07155774292
## 11000 min thresh: 0.06004895105
## 12000 min thresh: 0.02990162162
## 13000 min thresh: 0.02512168527
## 14000 min thresh: 0.0245153536
## 15000 min thresh: 0.0245153536
## 16000 min thresh: 0.0245153536
## 17000 min thresh: 0.0245153536
## 18000 min thresh: 0.0245153536
## 19000 min thresh: 0.02360921011
## 20000 min thresh: 0.02360921011
## 21000 min thresh: 0.02360921011
## 22000 min thresh: 0.02360921011
## 23000 min thresh: 0.02360921011
## 24000 min thresh: 0.02360921011
## 25000 min thresh: 0.02360921011
## 26000 min thresh: 0.02360921011
## 27000 min thresh: 0.02325615007
## 28000 min thresh: 0.02325615007
## 29000 min thresh: 0.02325615007
## 30000 min thresh: 0.0228829469
## 31000 min thresh: 0.02233009868
## 32000 min thresh: 0.0214910105
## 33000 min thresh: 0.0214910105
## 34000 min thresh: 0.02132753096
## 35000 min thresh: 0.01950619595
## 36000 min thresh: 0.01950619595
## 37000 min thresh: 0.01947938595
## 38000 min thresh: 0.01797372178
## 39000 min thresh: 0.01797372178
## 40000 min thresh: 0.01769753965
## 41000 min thresh: 0.01769753965
## 42000 min thresh: 0.01700626385
## 43000 min thresh: 0.01700626385
## 44000 min thresh: 0.01672159629
## 45000 min thresh: 0.01672159629
## 46000 min thresh: 0.01580463948
## 47000 min thresh: 0.01536023906
## 48000 min thresh: 0.01536023906
## 49000 min thresh: 0.01472203243
## 50000 min thresh: 0.01472203243
## 51000 min thresh: 0.01457902381
## 52000 min thresh: 0.01440348188
## 53000 min thresh: 0.01411069359
## 54000 min thresh: 0.01354772715
## 55000 min thresh: 0.01323812633
## 56000 min thresh: 0.0131189376
## 57000 min thresh: 0.01285350763
## 58000 min thresh: 0.01261026478
## 59000 min thresh: 0.01238672266
## 60000 min thresh: 0.01190481066
## 61000 min thresh: 0.01190481066
## 62000 min thresh: 0.01160194701
## 63000 min thresh: 0.01134952055
## 64000 min thresh: 0.01116540247
## 65000 min thresh: 0.01083546289
## 66000 min thresh: 0.01083546289
## 67000 min thresh: 0.01073533767
## 68000 min thresh: 0.01047215478
## 69000 min thresh: 0.01001640238
## 70000 min thresh: 0.01000323813
## 71000 min thresh: 0.01000323813
## 71073 error: 0.05167 time: 15.51 secs
predict <- compute(inet, iris[1:4])
result<-0
for (i in 1:150) { result[i] <- which.max(predict$net.result[i,]) }
for (i in 1:150) { if (result[i]==1) {result[i] = "setosa"} }
for (i in 1:150) { if (result[i]==2) {result[i] = "versicolor"} }
for (i in 1:150) { if (result[i]==3) {result[i] = "virginica"} }
comparison <- iris
comparison$Predicted <- result