library(mlbench)
library(keras)

data2<-data(BreastCancer)
data2<-BreastCancer
data2<-na.omit(data2)
data2<-data2[-1]

for(i in 1:ncol(data2)){data2[,i]<-as.numeric(as.factor(data2[,i]))}

data2<-as.matrix(data2)

data2[,10]<-as.numeric(as.factor(data2[,10]))

split<-sample(1:2,nrow(data2),prob = c(0.8,0.2),replace = T)

train<-data2[split==1,]

test<-data2[split==2,]

trainx<-train[,1:9]
train_y<-as.numeric(as.factor(train[,10]))

train_y<-train_y-1


y_train<- to_categorical(train_y)


testx<-test[,1:9]
test_y<-as.numeric(as.factor(test[,10]))
test_y<-test_y-1
y_test<-to_categorical(test_y)


model<-keras_model_sequential()

model %>%
  layer_dense(units =512,activation = 'relu',input_shape = 9) %>%
  layer_dense(units = 10, activation = 'relu')%>%
  layer_dense(units = 10, activation = 'relu')%>%
  layer_dense(units = 2, activation = 'sigmoid')
  


model %>% compile(
  loss='binary_crossentropy',
  optimizer='adam',
  metrics=c('accuracy')
)

summary(model)
## Model: "sequential"
## ___________________________________________________________________________
## Layer (type)                     Output Shape                  Param #     
## ===========================================================================
## dense (Dense)                    (None, 512)                   5120        
## ___________________________________________________________________________
## dense_1 (Dense)                  (None, 10)                    5130        
## ___________________________________________________________________________
## dense_2 (Dense)                  (None, 10)                    110         
## ___________________________________________________________________________
## dense_3 (Dense)                  (None, 2)                     22          
## ===========================================================================
## Total params: 10,382
## Trainable params: 10,382
## Non-trainable params: 0
## ___________________________________________________________________________
trainx<-as.matrix(trainx)
class(trainx)
## [1] "matrix"
history <- model %>% fit(trainx,y_train,epochs=50,batch_size=5,validation_split=0.2)

plot(history)

score <- model %>% evaluate(testx, y_test)

cat('Test loss:', score$loss, "\n")
## Test loss: 0.05903848
cat('Test accuracy:', score$acc, "\n")
## Test accuracy: 0.9794521
sam<-predict_classes(model,testx)


library(caret)
## Loading required package: lattice
## Loading required package: ggplot2
table(sam,test_y)
##    test_y
## sam  0  1
##   0 97  2
##   1  1 46
confusionMatrix(table(sam,test_y))
## Confusion Matrix and Statistics
## 
##    test_y
## sam  0  1
##   0 97  2
##   1  1 46
##                                           
##                Accuracy : 0.9795          
##                  95% CI : (0.9411, 0.9957)
##     No Information Rate : 0.6712          
##     P-Value [Acc > NIR] : <2e-16          
##                                           
##                   Kappa : 0.9532          
##                                           
##  Mcnemar's Test P-Value : 1               
##                                           
##             Sensitivity : 0.9898          
##             Specificity : 0.9583          
##          Pos Pred Value : 0.9798          
##          Neg Pred Value : 0.9787          
##              Prevalence : 0.6712          
##          Detection Rate : 0.6644          
##    Detection Prevalence : 0.6781          
##       Balanced Accuracy : 0.9741          
##                                           
##        'Positive' Class : 0               
##