{"cells":[
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "# This R environment comes with all of CRAN preinstalled, as well as many other helpful packages\n# The environment is defined by the kaggle/rstats docker image: https://github.com/kaggle/docker-rstats\n# For example, here's several helpful packages to load in \n\nlibrary(ggplot2) # Data visualization\nlibrary(readr) # CSV file I/O, e.g. the read_csv function\nlibrary(mlbench)\nlibrary(caret)\nlibrary(data.table)\nlibrary(bit64)\nlibrary(doMC)\nlibrary(caretEnsemble)\nlibrary(sampling)\nlibrary(class)\nlibrary(gmodels)\nlibrary(SwarmSVM)\nlibrary(apcluster)\nlibrary(ranger)\nlibrary(plyr)\nlibrary(dplyr)\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nsystem(\"ls ../input\")\n\n# Any results you write to the current directory are saved as output.\n\nregisterDoMC(cores = 4)\nData <- fread(\"../input/train.csv\",integer64 = \"character\")\nData <- as.data.frame(Data)\nData <- Data[,2:6]\n################ Creating new features from the timestamp feature ########\nData$hour = (Data$time/60) %% 24\nData$weekday = (Data$time/(60*24)) %% 7\nData$month = (Data$time/(60*24*30)) %% 12 \nData$year = Data$time/(60*24*365)\nData$day = Data$time/(60*24) %% 365\nData <- cbind(Data[,1:3],Data[,6:9],Data[,5])\ncolnames(Data)[8]<-c(\"place_id\")"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "##############3 dividing the data to training and test Data ############\ntrainIndex <- createDataPartition(Data[,8], p = .75,list = FALSE,times = 1)\ntrain<- Data[ trainIndex,]\ntest  <- Data[-trainIndex,]"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "############## Decomposing the cluster into 1600 small clusters (250*250) to increase accuracy and minimize computation #######\nn.centers1 =1600\ncenters1 <- matrix(runif(n.centers1*2, min=c(0, 0), max=c(10, 10)), ncol=2, byrow=TRUE)\ncl<- cbind(train[,1:2])\nClusters <- kmeans(cl,centers1,algorithm = \"Lloyd\") "
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "########################## training process unsing rf alogrithm ###########\nAccuracy.val = 0\ncontrol <- trainControl(method=\"repeatedcv\", number=10, repeats=0, savePredictions=FALSE, classProbs=TRUE)\nmodels <- list()\nptm <- proc.time()\n\nfor (i in 1:1600){\n  print(i)\n  Cluster = train[Clusters$cluster==i,]\n  #remove places which occcurences less than mean\n  ##get mean\n  ids.mean <- Cluster %>% count(place_id)\n  ids.mean <- ids.mean[,2]\n  ids.mean <- round(mean(ids.mean[[1]]))\n  ids <- Cluster %>% count(place_id) %>% filter(n > ids.mean)  \n  Cluster = Cluster[Cluster$place_id %in% ids$place_id,]\n  mod <- train(make.names(place_id)~.,data=Cluster , method =\"rf\",trControl = control)\n  metrics <- mod$results\n  Accuracy.val = Accuracy.val + max(metrics$Accuracy)\n  models[[length(models)+1]] <- mod$finalModel\n}\nproc.time() - ptm\nAccuracy.val = Accuracy.val /1600"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "#### evaluation process will be done by clustering test element to the nearest cluster\n### based on kmeans centers then use the model of this cluster \n\n################ Testing and evaluation processs ##############\nindex = c(1:1600)\nTest =knn(Clusters$centers, test[,1:2],cl=index, k = 1)\ntest = cbind(test,Test)"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "############## prediction process and accuracy calculation ################\nAccuracy = 0\nfor (k in 1:1600){\n  print(k)\n  test.sub1 <- subset(test,test[,9]==k)\n  prediction <- predict(models[k],test.sub1[,1:7])\n  x<-postResample(prediction[[1]], factor(make.names(test.sub1[,8])))\n  #print(x)\n  Accuracy = Accuracy +x[[1]]\n}\nAccuracy_final = Accuracy / 1600"
 }
],"metadata":{"kernelspec":{"display_name":"R","language":"R","name":"ir"}}, "nbformat": 4, "nbformat_minor": 0}