# Ensemble using Xgboost and randomForest
library(xgboost)
library(randomForest)
library(readr)

# The competition datafiles are in the directory ../input
# List the files we have available to work with
system("ls ../input")

# Read competition data files:
train <- read_csv("../input/training.csv")
test <- read_csv("../input/test.csv")

#Assigning Feature Index and respense
features<-c(2:46)
response<-c(49)


# Converting to XGB Matrix 
dtrain <- xgb.DMatrix(data = as.matrix(train[,features]),label = as.matrix(train[,response]))
dtest <- xgb.DMatrix(data = as.matrix(test[,features]))

# Xgboost Classifier 
# After obtaining best parameters from Grid search(CV)
param <- list(max.depth=5,eta=0.2,silent=1,nthread = 4, 
              objective='binary:logistic',colsample_bytree=0.8,subsample=0.8,min.child=1)
bst<-xgb.train(data=dtrain,params=param,nrounds=200,verbose=1,eval_metric={'auc'})
pred_xg <- predict(bst,dtest)

cat("\n Xgboost Completed ....");

#Applying randomForest
rf = randomForest(x=as.matrix(train[,features]), y=as.factor(train[,response]), replace=F, ntree=200, do.trace=FALSE, mtry=10)
pred_rf = predict(rf,as.matrix(test[,features]), type="prob")[,2]

cat("\n RandomForest Completed....");

#creating a voting ensemble 
submission<-data.frame(id=test$id,prediction=(pred_xg+pred_rf)/2)

cat("\n writing the submission....");
#Generate the output
write.csv(file="Xgboost_rf_ensemble.csv",submission,row.names = FALSE)



