# This R environment comes with all of CRAN preinstalled, as well as many other helpful packages
# The environment is defined by the kaggle/rstats docker image: https://github.com/kaggle/docker-rstats
# For example, here's several helpful packages to load in 

library(ggplot2) # Data visualization
library(readr) # CSV file I/O, e.g. the read_csv function

# Input data files are available in the "../input/" directory.
# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory

system("ls ../input")

# Any results you write to the current directory are saved as output.
# This R environment comes with all of CRAN preinstalled, as well as many other helpful packages
# The environment is defined by the kaggle/rstats docker image: https://github.com/kaggle/docker-rstats
# For example, here's several helpful packages to load in 

library(ggplot2) # Data visualization
library(readr) # CSV file I/O, e.g. the read_csv function

# Input data files are available in the "../input/" directory.
# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory

system("ls ../input")

# Any results you write to the current directory are saved as output.

library(ggplot2) # Data visualization
library(readr) # CSV file I/O, e.g. the read_csv function
library(dplyr)
library(randomForest)
#Import train dataset


library(dplyr)
library(readr)
library(reshape)
library(reshape2)

library(data.table)  
library(h2o)


train_Full <- read_csv("../input/train.csv")
test_Full <- read_csv("../input/test.csv")
result_final<-data.frame()
numTrees=30

x_out=62.5
y_out=100

for(y_out in seq(97.5,100 , 2.5))
  {
    
    
        train<-train_Full[train_Full$x <= (x_out+.5)*.1 & train_Full$x > (x_out-3)*.1 & train_Full$y <= (y_out+.5)*.1 & train_Full$y > (y_out-3)*.1,]
        test<-test_Full[test_Full$x <= (x_out)*.1 & test_Full$x > (x_out-2.5)*.1 & test_Full$y <= (y_out)*.1 & test_Full$y > (y_out-2.5)*.1,]
        
        
        train$Hr <- train$time %% 60
        train$Day <- (trunc(train$time /  60 ) %% 24) 
        train$Days <- (trunc(train$time / 1440 ) %% 7)
        test$Hr <- test$time %% 60
        test$Day <- (trunc(test$time /  60 ) %% 24) 
        test$Days <- (trunc(test$time / 1440 ) %% 7)
        
        train<-train[train$place_id %in% head(arrange(data.frame(table(train$place_id))[data.frame(table(train$place_id))$Freq>=10,] , by = desc(Freq)) , 1000)[,1],]
        train$place_id <- factor(train$place_id)
        
        
        features<-colnames(train)[c(-1,-6)]
  
        train<-train[,-1]
        
        rf <- randomForest(place_id~. , data=train, xtest=test[ , c(-1)] , ntree=numTrees)
        
        result<-data.frame(rf$test$votes )
        result$row_id<- test$row_id
        result <- melt(result[,-1] , id="row_id")
        result<- arrange(result ,row_id, desc(value))
        
        result<- summarize(group_by(result ,row_id) ,place_id=paste(head(variable, 3),collapse=" "))
        
        result$place_id<- gsub('p' ,'', gsub('X', '', result$place_id))
        result_final<-rbind(result_final,result)
        names(result_final) = c('row_id' , 	'place_id')
        write_csv(result_final , paste0("Test_result.csv"))
        print(paste0("Test_result_",x_out ,"_", y_out ,".csv"))
        #write_csv(result_final , paste0("Test_result_",x_out ,"_", y_out ,".csv"))
        #  write_csv(data.frame(predictions[,1]) , paste0("D:/personal/Kaggle/Facebook/RF_prediction/TrainRF_result_",x_out , y_out , x,y,".csv"))
           #  write_csv(data.frame(result) , paste0("D:/personal/Kaggle/Facebook/RF_result/Test_result_",x_out , y_out , x,y,".csv"))
    #  write_csv(data.frame(predictions[,1]) , paste0("D:/personal/Kaggle/Facebook/RF_prediction/TrainRF_result_",x_out , y_out , x,y,".csv"))
    
    
  }


       