options(warn=-1) #surpress warnings
library(ggplot2) # Data visualization
library(readr) # CSV file I/O, e.g. the read_csv function
library(data.table)
library(h2o)
Nr=20000 #max number of lines, 20000 to keep runtime feasible
NMC=2 #number of Monte-Carlo Run
#get the data Response and on all the train data, to localize the rare positive responses
train_numeric <- fread("../input/train_numeric.csv", select = c("Id","Response"))
nrows=nrow(train_numeric)
no=as.matrix(train_numeric);
#finding where are the null and rare postive response
IG0=which(no[,2]==0)
IG1=which(no[,2]==1)
select<-0*no[,2]
nmax=length(IG1);
#get the data - nrows set to Nr
train_numericB <- fread("../input/train_numeric.csv",nrows=Nr)
train_dateB <- fread("../input/train_date.csv",nrows=Nr)
TOTAL_num<-numeric(length(setdiff(names(train_numericB),c("Id","Response"))))
TOTAL_dat<-numeric(length(names(train_dateB)))
h2o.init(nthreads = 1)
#Monte Carlo importance sampling of feature ranking
set.seed(1)
for (MC in 1:NMC){
  #All the positive (rare) responses are taken, samples are taken over null response
  ENS=setdiff(1:Nr,IG1)
  #sampling
  ENS2=sample(ENS,nmax,replace =FALSE)
  train_numeric<-train_numericB[c(ENS2,IG1),]
  train_numeric2<-as.data.frame(no[c(ENS2,IG1),])
  train_date<-train_dateB[c(ENS2,IG1),]
  train_numeric[,"Response"]<- NULL 
  train_numeric[,"Id"]<- NULL 
  train_numeric<-cbind(train_numeric2,train_numeric)
  train_numeric[,"Response"] <- as.factor(train_numeric[,"Response"])
  train_numeric=as.h2o(train_numeric)
  train_date=as.h2o(train_date)
  train <- h2o.cbind(train_numeric,train_date)
  train_numeric_name <- colnames(train_numeric)[-c(1,dim(train_numeric)[2])]
  train_date_name <- colnames(train_date)[-1]
  train_name <- c(train_numeric_name,train_date_name)
  #Gradient Boosting for feature ranking over on sampled data rows
  gbm1 <- h2o.gbm(train_name,"Response",train,
                  ignore_const_cols = TRUE,
                  ntrees = 25,
                  max_depth = 4,
                  min_rows = 20,
                  learn_rate = 0.1,
                  sample_rate = 0.8,
                  col_sample_rate = 0.8,
                  balance_classes = FALSE,
                  seed = 2016,
                  nfolds = 2,
                  fold_assignment = "Stratified",
                  keep_cross_validation_predictions = TRUE,
                  keep_cross_validation_fold_assignment = TRUE,
                  score_tree_interval = 20,
                  stopping_rounds = 10,
                  stopping_metric = "AUC",
                  stopping_tolerance = 0.01
  )
  
  #importance computation
  importance <- h2o.varimp(gbm1)
  importance <- as.data.table(importance)
  importance <- importance[scaled_importance>0]
  
  train_num=intersect(names(train),setdiff(names(train_numeric),c("Id","Response")))
  train_dat=intersect(names(train),names(train_date))
  
  category_num<-numeric(length((train_num)))
  #scaled ranking is redistributed over total ids to set 
  #a global ranking by summing along MC runs
  ct<-1
  for (i in (train_num)){
    print(i)
    id<-which(importance$variable==i)
    if (length(importance$scaled_importance[id])>0){
      category_num[ct]<-importance$scaled_importance[id]
    }
    ct<-ct+1
  }
  #summing relative numerical importance along MC runs
  TOTAL_num<-TOTAL_num+category_num
  
  category_dat<-numeric(length((train_dat)))
  ct<-1
  for (i in (train_dat)){
    print(i)
    id<-which(importance$variable==i)
    if (length(importance$scaled_importance[id])>0){
      category_dat[ct]<-importance$scaled_importance[id]
    }
    ct<-ct+1
  }
  #summing relative date importance along MC runs
  TOTAL_dat<-TOTAL_dat+category_dat
  
  IG_num<-(train_num)
  sorted_n<-sort(-TOTAL_num,index.return = TRUE)
  print(head(IG_num[sorted_n$ix]))
  IG_dat<-(train_dat)
  sorted_d<-sort(-TOTAL_dat,index.return = TRUE)
  print(head(IG_dat[sorted_d$ix]))
}
topFeaturesNumeric<-IG_num[sorted_n$ix]
topFeaturesDate<-IG_dat[sorted_d$ix]
#writing best features on csv
write.csv(topFeaturesNumeric, file = 'BOSHimportanceNUM.csv')
write.csv(topFeaturesDate, file = 'BOSHimportanceDAT.csv')
# Any results you write to the current directory are saved as output.