R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

#Checking the structure of Data str(train) newtrain <- as.matrix(train[,-32]) #Without Search = 32th Column dimnames(newtrain) <- NULL

str(test) newtest <- as.matrix(test[,-32]) #Without Search = 32th Column dimnames(newtest) <- NULL

str(valid) newvalid <- as.matrix(valid[,-32]) #Without Search = 32th Column dimnames(newvalid) <- NULL

#Normalize the matrix options(scipen = 999) #Check the attributes used #Total columns available is 32 newtrain[,1:31] <- normalize(newtrain[,1:31]) #Independent = Predictors newtrain[,32] <- as.numeric(newtrain[,32]) -1 #Label = 0,1,2 summary(newtrain) training <- newtrain[,1:31] traintarget <- newtrain[,32]

newtest[,1:31] <- normalize(newtest[,1:31]) #Independent = Predictors newtest[,32] <- as.numeric(newtest[,32]) -1 #Label = 0,1,2 summary(newtest) testing <- newtest[,1:31] testtarget <- newtest[,32]

newvalid[,1:31] <- normalize(newvalid[,1:31]) #Independent = Predictors newvalid[,32] <- as.numeric(newvalid[,32]) -1 #Label = 0,1,2 summary(newvalid) validating <- newvalid[,1:31] validtarget <- newvalid[,32]

#One hot encoding trainlabels <- to_categorical(traintarget) testlabels <- to_categorical(testtarget) validlabels <- to_categorical(validtarget)

#Create Sequential Model model <- keras_model_sequential() #Pipe symbol %>% model %>% layer_dense(units = 9, activation = ‘relu’, input_shape = c(31))%>% layer_dense(units = 9, activation = ‘relu’) %>% layer_dense(units = 3, activation = ‘softmax’) summary(model)
#31 independent * 9 layers= 279 + 9 constant values for each node = 288 #layer_dense(units = 3, activation = ‘softmax’) is Visible/Output layer with 3 classes

#Compile the model #For 3 class problem we will use “categorical_crossentropy” model %>% compile(loss =‘categorical_crossentropy’, optimizer = ‘adam’, metrics = ‘accuracy’)

#Fit the model (Multi-layer Perceptron Neural Network) #Using Train data on Validation set history <- model %>% fit(training, trainlabels, epoch=200, validation_data = list(validating,validlabels) ) plot(history)

#Evaluate the model using Test Data model1 <- model %>% evaluate(testing,testlabels)

#Prediction and Confusion Matrix - test data prob <- model %>% predict_proba(testing) pred <- model %>% predict_classes(testing) table1 <- table(Predicted=pred, Actual=testtarget)

cbind(prob,pred,testtarget)

model1 table1

#Evaluate the model using Valid Data model1 <- model %>% evaluate(validating,validlabels)

#Prediction and Confusion Matrix - Valid data prob <- model %>% predict_proba(validating) pred <- model %>% predict_classes(validating) table1 <- table(Predicted=pred, Actual=validtarget)

cbind(prob,pred,validtarget)

model1{r cars} summary(cars)

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.