# 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(data.table)
#expedia_train <- fread('../input/train.csv', header=TRUE)
#expedia_test <- fread('../input/test.csv', header=TRUE)

#id <- expedia_train$user_id
#h_cluster <- expedia_train$hotel_cluster
#cov(id,h_cluster)

#head(expedia_train)
#summary(expedia_train)

#eu_train <- round(dist(expedia_train, method="euclidean"), digits=2)
#eu

#Script - R version of most popular local hotel (아래)


#dest_id_hotel_cluster_count <- expedia_train[,length(is_booking),by=list(srch_destination_id, hotel_cluster)]

#top_five <- function(hc,v1){
#  hc_sorted <- hc[order(v1,decreasing=TRUE)]
#  n <- min(5,length(hc_sorted))
#  paste(hc_sorted[1:n],collapse=" ")
#}

#dest_top_five <- dest_id_hotel_cluster_count[,top_five(hotel_cluster,V1),by=srch_destination_id]

#dd <- merge(expedia_test,dest_top_five, by="srch_destination_id",all.x=TRUE)[order(id),list(id,V1)]

#setnames(dd,c("id","hotel_cluster"))

#write.csv(dd, file='submission_1.csv', row.names=FALSE)




#강의노트 1번



#dest_id_hotel_cluster_count <- 
#expedia_train[,length(is_booking),
#by=list(srch_destination_id, hotel_cluster)]

#dest_id_hotel_cluster_count
#v1이 뜻하는 것이 뭐지? 

#top_five <- function(hc,v1) {
#hc_sorted <- hc[order(v1,decreasing=TRUE)]
#n <- min(5,length(hc_sorted))
#paste(hc_sorted[1:n],collapse=" ")
#}
#hc는 어디서 나온 값?
#top_five

#dest_top_five <-
#dest_id_hotel_cluster_count[,top_five(hotel_cluster,V1),by=srch_destination_id]

#dd <- merge(expedia_test,dest_top_five,
#by="srch_destination_id",all.x=TRUE)[order(id),list(id,V1)]
#setnames(dd,c("id","hotel_cluster"))
#write.csv(dd, file='submission.csv', row.names=FALSE)






library(data.table)
expedia_train <- fread('../input/train.csv', header=TRUE, select= c("is_booking","orig_destination_distance","hotel_cluster","srch_destination_id"))
expedia_test <- fread('../input/test.csv', header=TRUE)

sum_and_count <- function(x){
  sum(x)*0.8456 + length(x) *(1-0.8456)
}

dest_id_hotel_cluster_count <- expedia_train[,sum_and_count(is_booking),by=list(orig_destination_distance, hotel_cluster)]
dest_id_hotel_cluster_count1 <- expedia_train[,sum_and_count(is_booking),by=list(srch_destination_id, hotel_cluster)]


top_five <- function(hc,v1){
  hc_sorted <- hc[order(v1,decreasing=TRUE)]
  n <- min(5,length(hc_sorted))
  paste(hc_sorted[1:n],collapse=" ")
}

dest_top_five <- dest_id_hotel_cluster_count[,top_five(hotel_cluster,V1),by=orig_destination_distance]
dest_top_five1 <- dest_id_hotel_cluster_count1[,top_five(hotel_cluster,V1),by=srch_destination_id]

dd <- merge(expedia_test,dest_top_five, by="orig_destination_distance",all.x=TRUE)[order(id),list(id,V1)]

dd1 <- merge(expedia_test,dest_top_five1, by="srch_destination_id",all.x=TRUE)[order(id),list(id,V1)]

dd$V1[is.na(dd$V1)] <- dd1$V1[is.na(dd$V1)] 

setnames(dd,c("id","hotel_cluster"))

#dd

#write.csv(dd, file='submission_combo_merge.csv', row.names=FALSE)

dest_id_hotel_cluster_count <- expedia_train[,length(is_booking),by=list(srch_destination_id, hotel_cluster)]

top_five <- function(hc,v1){
  hc_sorted <- hc[order(v1,decreasing=TRUE)]
  n <- min(5,length(hc_sorted))
  paste(hc_sorted[1:n],collapse=" ")
}

dest_top_five <- dest_id_hotel_cluster_count[,top_five(hotel_cluster,V1),by=srch_destination_id]

dd3 <- merge(expedia_test,dest_top_five, by="srch_destination_id",all.x=TRUE)[order(id),list(id,V1)]

setnames(dd3,c("id","hotel_cluster"))

#write.csv(dd, file='submission_1.csv', row.names=FALSE)
dd3 <- c(dd, dd3$hotel_cluster)

head(dd3)
















