use the nnet package to analyze the iris dataset. Use 80% of the 150 samples as the training and the rest for validation. Discuss the result.
library(nnet)
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.1.2
library(caret)
## Warning: package 'caret' was built under R version 4.1.2
## Loading required package: lattice
data(iris)
str(iris)
## 'data.frame': 150 obs. of 5 variables:
## $ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
## $ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
## $ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
## $ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
## $ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
# We draw the petal length vs the petal width
ggplot(data = iris, aes(x = Petal.Width, y = Petal.Length)) + geom_point(aes(color = Species))
#normalize the data before using neural networks
iris_norm <- iris
iris_norm$Species <- as.numeric(iris$Species)
iris_norm <- as.data.frame(apply(iris_norm, 2, function(x) (x - min(x))/(max(x)-min(x))))
# We create a trainset and a testset
index <- createDataPartition(iris_norm$Species, p = 0.8, list = FALSE)
trainset <- iris_norm[index,]
testset <- iris_norm[-index,]
# We create a model using the nnet package
set.seed(5)
iris.net <- nnet(Species ~., data = trainset, linout = TRUE, size = 3)
## # weights: 19
## initial value 64.373802
## iter 10 value 2.089332
## iter 20 value 1.316084
## iter 30 value 0.975909
## iter 40 value 0.934558
## iter 50 value 0.915568
## iter 60 value 0.891296
## iter 70 value 0.877370
## iter 80 value 0.859141
## iter 90 value 0.852956
## iter 100 value 0.841861
## final value 0.841861
## stopped after 100 iterations
From this out put, the final value is 0.702139 and it stops at the 100the iteration.
summary(iris.net)
## a 4-3-1 network with 19 weights
## options were - linear output units
## b->h1 i1->h1 i2->h1 i3->h1 i4->h1
## 5.26 1.40 1.11 -3.71 -3.95
## b->h2 i1->h2 i2->h2 i3->h2 i4->h2
## 6.85 1.19 1.19 -3.47 -4.38
## b->h3 i1->h3 i2->h3 i3->h3 i4->h3
## -2.03 -0.32 0.47 -1.60 -1.01
## b->o h1->o h2->o h3->o
## -2.01 -4.36 6.72 -2.92
From these results it can also be seen that the iris.net output is a network with 4 inputs, 3 nodes and 1 output with 19 weights.
As a mini project, install the keras package and learn how to use it. Then carry out various tasks that may be useful to your project and studies.
#install.packages("keras","tensorflow")
#install.packages("devtools")
library(devtools)
## Warning: package 'devtools' was built under R version 4.1.2
## Loading required package: usethis
## Warning: package 'usethis' was built under R version 4.1.2
devtools::install_github("rstudio/keras", dependencies = TRUE)
## Skipping install of 'keras' from a github remote, the SHA1 (f2eb4a23) has not changed since last install.
## Use `force = TRUE` to force installation
devtools::install_github("rstudio/tensorflow", dependencies = TRUE)
## Skipping install of 'tensorflow' from a github remote, the SHA1 (6fc925a7) has not changed since last install.
## Use `force = TRUE` to force installation
#keras::install_keras()
library(keras)
mnist <- dataset_mnist()
## Loaded Tensorflow version 2.7.0
#separating train and test file
train_x<-mnist$train$x
train_y<-mnist$train$y
test_x<-mnist$test$x
test_y<-mnist$test$y
rm(mnist)
# converting a 2D array into a 1D array for feeding into the MLP and normalising the matrix
train_x <- array(train_x, dim = c(dim(train_x)[1], prod(dim(train_x)[-1]))) / 255
test_x <- array(test_x, dim = c(dim(test_x)[1], prod(dim(test_x)[-1]))) / 255
#converting the target variable to once hot encoded vectors using keras inbuilt function
train_y<-to_categorical(train_y,10)
test_y<-to_categorical(test_y,10)
library(tensorflow)
##
## Attaching package: 'tensorflow'
## The following object is masked from 'package:caret':
##
## train
#defining a keras sequential model
model <- keras_model_sequential()
#defining the model with 1 input layer[784 neurons], 1 hidden layer[784 neurons] with dropout rate 0.4 and 1 output layer[10 neurons]
#i.e number of digits from 0 to 9
model %>%
layer_dense(units = 784, input_shape = 784) %>%
layer_dropout(rate=0.4)%>%
layer_activation(activation = 'relu') %>%
layer_dense(units = 10) %>%
layer_activation(activation = 'softmax')
#compiling the defined model with metric = accuracy and optimiser as adam.
model %>% compile(
loss = 'categorical_crossentropy',
optimizer = 'adam',
metrics = c('accuracy')
)
#fitting the model on the training dataset
history <- model %>% fit(train_x, train_y, epochs = 100, batch_size = 128)
#Evaluating model on the cross validation dataset
loss_and_metrics <- model %>% evaluate(test_x, test_y, batch_size = 128)
model %>%
evaluate(test_x, test_y, verbose = 0)
## loss accuracy
## 0.1201192 0.9868000
The history object returned by fit() includes loss and accuracy metrics which we can plot
plot(history)
## `geom_smooth()` using formula 'y ~ x'
Using keras package I learned how to prepare the data with train and test data and create a model to use these data. There are a lot of built in functions to help build the model and even to add in a compiler to be used. Once you build the model you want you can then fit in the train and test data you are using. From here you can evaluate your model and plot the data to see how accurate your model is as well as make predictions. Overall the keras package was a good tool to learn and I see it being useful in future project and studies.