
library(h2o)

##set the maximum memory size to 7g. If this is not set, the default values will be 1g for 32 bit java and 1/4 of awailable memory for 64 but java
h2o.init(nthreads=-1,max_mem_size='7G')  

##Upload the training data 
train<-h2o.uploadFile("../input/train.csv",destination_frame = "train.hex")
train$target<-log(train$Demanda_uni_equil+1)

##Create a partition to generate the product averages
avg<-train[train$Semana <= 5,]                          ##partition includes first 5 semana

##Create a partition to generate fit into a model                   
modelData<-train[train$Semana > 5 & train$Semana < 8,]  ##partition includes first 6,7 semana

##Create a partition for evaluation
eval<-train[train$Semana == 8,]                         ##partition includes first semana 8               

#Declare the set of independent attributes to learn from
independent<-c("Agencia_ID","Canal_ID","Ruta_SAK","Cliente_ID","Producto_ID")

groups<-h2o.group_by(data=avg,by="Producto_ID",mean("target"))

modelDataTwo<-h2o.merge(x=modelData,y=groups,all.x = T)
modelDataTwo$mean_target[is.na(modelDataTwo$mean_target)]<-0.7

evalTwo<-h2o.merge(x=eval,y=groups,all.x = T)
evalTwo$mean_target[is.na(evalTwo$mean_target)]<-0.7


g<-h2o.gbm(
  training_frame = modelDataTwo,      
  validation_frame = evalTwo,  
  x=independent,                 
  y="target",                   
  model_id="gbm1",              
  ##ntrees = 15,                  
  learn_rate = 0.3,             
  score_tree_interval = 3,      
  sample_rate = 0.5,            
  col_sample_rate = 0.8,        
  offset_column = "mean_target"
)

##Generate an R like summary for each column
summary(g)

h2o.rm(train)
h2o.rm(avg)
h2o.rm(modelData)
h2o.rm(modelDataTwo)

test<-h2o.uploadFile("../input/test.csv",destination_frame = "test.hex")
testTwo<-h2o.merge(x=test,y=groups,all.x = T)
testTwo$mean_target[is.na(testTwo$mean_target)]<-0.7
testTwo[1:5,]
p<-h2o.predict(g,testTwo)
p<-exp(p)-1

##Generate an R like summary for each column
summary(p)

submissionFrame<-h2o.cbind(test$id,p)
colnames(submissionFrame)<-c("id","Demanda_uni_equil")
h2o.exportFile(submissionFrame,path="output.csv")  