{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "14071ae5-41a5-e2b8-a52e-84eed888731d"
      },
      "source": [
        "Titanic - Predicting the survivors using xgboost(84% accuracy)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "7bb99f4e-d12d-a7c9-142f-fb2982a0783e"
      },
      "outputs": [],
      "source": [
        "\n",
        "#######################--My First Kaggle Competition---######################\n",
        "\n",
        "#**************************** Sinking of Titanic ****************************\n",
        "\n",
        "# -------------------------  PART 1 -------------------------------------------------\n",
        "\n",
        "########################## TRAINING A MODEL - xgboost ###############################\n",
        "\n",
        "#----------------- Read the train file -----------------------------\n",
        "\n",
        "#  this file will be split into training and testing and for building the classifier\n",
        "database <- read.csv(\"train.csv\", na.strings = c(\"\"))\n",
        "database <- database[c(3,5,6,7,8,10,12,2)] # removed passenger is, name, ticket and cabin number\n",
        "head(database)\n",
        "str(database)\n",
        "\n",
        "#------------------------Data cleaning and formatting\n",
        "\n",
        "# dealing with missing values\n",
        "#------ replace missing age by mean\n",
        "database$Age[is.na(database$Age)] <- round(mean(database$Age, na.rm = TRUE))\n",
        "\n",
        "#-------------- remove two records who are missing Embarked(only two wont impact)\n",
        "database <- na.omit(database)\n",
        "\n",
        "#  Converting categorical values into numeric\n",
        "database$Embarked = as.numeric(factor(database$Embarked,\n",
        "                                       levels = c(\"C\", \"Q\", \"S\"),\n",
        "                                       labels = c(1,2,3))\n",
        ")\n",
        "\n",
        "database$Sex = as.numeric(factor(database$Sex,\n",
        "                                 levels = c(\"female\",\"male\"),\n",
        "                                 labels = c(1,2))\n",
        ")\n",
        "\n",
        "#------------- create training and testing data\n",
        "library(caTools)\n",
        "split <- sample.split(database$Survived, SplitRatio = 0.8)\n",
        "training <- subset(database, split==TRUE)\n",
        "testing <- subset(database, split==FALSE)\n",
        "\n",
        "# -------------------Fitting XGBoost to the Training set\n",
        "#install.packages('xgboost')\n",
        "library(xgboost)\n",
        "classifier = xgboost(data = as.matrix(training[-8]), label = training$Survived, nrounds = 10)\n",
        "\n",
        "# Predicting the Test set results\n",
        "y_pred = predict(classifier, newdata = as.matrix(testing[-8]))\n",
        "y_pred = (y_pred >= 0.5)\n",
        "\n",
        "# Making the Confusion Matrix\n",
        "cm = table(testing[, 8], y_pred)\n",
        "cm\n",
        "\n",
        "# -------------------------  PART 2 -------------------------------------------------\n",
        "\n",
        "########################## PREDICTING THE SURVIVORS - xgboost ###############################\n",
        "\n",
        "# this file will be ran against the created model to predict the outcome\n",
        "database_test <- read.csv(\"test.csv\", na.strings = c(\"\"))\n",
        "database_output <- read.csv(\"test.csv\", na.strings = c(\"\"))\n",
        "database_test <- database_test[c(2,4,5,6,7,9,11)] \n",
        "\n",
        "##dealing with missing values\n",
        "#------ replace missing age by mean\n",
        "database_test$Age[is.na(database_test$Age)] <- round(mean(database_test$Age, na.rm = TRUE))\n",
        "\n",
        "#-------------- remove two records who are missing Embarked(only two wont impact)\n",
        "#database_test <- na.omit(database_test)\n",
        "\n",
        "#  Converting categorical values into numeric\n",
        "database_test$Embarked = as.numeric(factor(database_test$Embarked,\n",
        "                                      levels = c(\"C\", \"Q\", \"S\"),\n",
        "                                      labels = c(1,2,3))\n",
        ")\n",
        "\n",
        "database_test$Sex = as.numeric(factor(database_test$Sex,\n",
        "                                 levels = c(\"female\",\"male\"),\n",
        "                                 labels = c(1,2))\n",
        ")\n",
        "\n",
        "# --- Fun time - Predicting the outcome\n",
        "\n",
        "y_pred = predict(classifier, newdata = as.matrix(database_test))\n",
        "y_pred = ifelse(y_pred >= 0.5,1,0)\n",
        "y_pred\n",
        "\n",
        "#------------Add this predicted outcome to test data after renaming it as Survived\n",
        "\n",
        "database_output$Survived <- y_pred\n",
        "head(database_output)\n",
        "database_output <- database_output[c(1,12)]\n",
        "\n",
        "write.csv(database_output, file = \"D:/Practice R and machine learning/Titanic Comp on kaggle/gender_submission.csv\")\n"
      ]
    }
  ],
  "metadata": {
    "_change_revision": 0,
    "_is_fork": false,
    "kernelspec": {
      "display_name": "R",
      "language": "R",
      "name": "ir"
    },
    "language_info": {
      "codemirror_mode": "r",
      "file_extension": ".r",
      "mimetype": "text/x-r-source",
      "name": "R",
      "pygments_lexer": "r",
      "version": "3.3.3"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}