#PubH 5470-2, S'04 #Example 7.1: Use of neural networks. library(nnet) data(iris) # This famous (Fisher's or Anderson's) iris data set gives the # measurements in centimeters of the variables sepal length and # width and petal length and width, respectively, for 50 flowers # from each of 3 species of iris. The species are _Iris setosa_, # _versicolor_, and _virginica_. iris[1:3,] # Sepal.Length Sepal.Width Petal.Length Petal.Width Species #1 5.1 3.5 1.4 0.2 setosa #2 4.9 3.0 1.4 0.2 setosa #3 4.7 3.2 1.3 0.2 setosa ###fit a single-hidden-layer neural network: #training data: n=100; test data: n=50 set.seed(022104) tr<-sample(1:150, 100) ir.nnet <- nnet(Species ~., data=iris[tr,], size=5, decay=0.01, maxit=500) #########Input: among others, # weights: (case) weights for each example - if missing defaults to 1. # size: number of units in the hidden layer. Can be zero if there are # skip-layer units. # decay: parameter for weight decay. Default 0. # maxit: maximum number of iterations. Default 100. #########Returned Value: # object of class '"nnet"' or '"nnet.formula"'. Mostly internal # structure, but has components # wts: the best set of weights found # value: value of fitting criterion plus weight decay term. #fitted.values: the fitted values for the training data. #residuals: the residuals for the training data. #########doing prediction: table(iris$Species[-tr], predict(ir.nnet, iris[-tr,], type="class")) # setosa versicolor virginica # setosa 11 0 0 # versicolor 0 20 0 # virginica 0 0 19 ####predicted: matrix values returned by the network: # predict(ir.nnet, iris[-tr,], type="raw") # setosa versicolor virginica #6 0.9991274553 0.0004588820 4.136627e-04 #13 0.9977274973 0.0022070330 6.546974e-05 #15 0.9994675454 0.0003600989 1.723556e-04 #16 0.9993765284 0.0002482534 3.752182e-04 #...