# This Python 3 environment comes with many helpful analytics libraries insmmendations/ttalled
# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python
# For example, here's several helpful packages to load in 

import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

# 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

import os
print(os.listdir("../input"))

# Any results you write to the current directory are saved as output.
install.packages("data.table")
library(data.table)
install.packages("class")
library(class)
expedia.train <- fread('../input/expedia-hotel-recommendations/train.csv')
#expedia.sam <- fread('sam')
#expedia.destination <-fread('destinations.csv')
expedia.test <- fread('../input/expedia-hotel-recommendations/test.csv')
head(expedia.train)
dim(expedia.train)
attach(expedia.train)

#??missing value
expedia.test.Xdate <- expedia.test[,-1]
expedia.test.Xdate$is_booking <- rep(1, dim(expedia.test.Xdate)[1])
#train
novalue<-which(is.na(expedia.train$orig_destination_distance), arr.ind = TRUE)
expedia.train02<-expedia.train[-novalue,]
expedia.train.Xdate <- expedia.train02[,-1]
sum(is.na(expedia.train.Xdate))
#test
novaluetest<-which(is.na(expedia.test$orig_destination_distance), arr.ind = TRUE)
expedia.test02<-expedia.test[-novaluetest,]
expedia.test.Xdate <- expedia.test02[,-1]
sum(is.na(expedia.test.Xdate))


#皜祈岫KNN 嚗?????
bb = knn(expedia.train.Xdate,expedia.test.Xdate,expedia.train.Xdate$hotel_cluster, k = 2)
bb = knn(expedia.train.Xdate[,-23],expedia.test.Xdate,expedia.train.Xdate$hotel_cluster, k = 2)

require(DMwR)
data.KNN <- knnImputation(expedia.train.Xdate) #KNN
head(data.KNN) #?閰ａ瞍潭?畾?? 

#linear regression
lmfit <- lm(hotel_cluster~.-hotel_cluster, data = expedia.train02[, -1])
