{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "5fc5ef90-86ac-7b0f-e5b1-d7c0f4c3a4d0"
      },
      "source": [
        "----------\n",
        "# Futures sequence by Line, Station and Feature type\n",
        "Narasimharao Pureti\n",
        ">9 September 2016"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "e35ffc1e-a486-7df8-63be-665c148742a2"
      },
      "outputs": [],
      "source": [
        "library(dplyr)\n",
        "library(ggplot2)\n",
        "suppressMessages(library(data.table))\n",
        "library(plotly)\n",
        "library(reshape)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "331d81ec-ca50-0d98-8bef-98e37f9bd4f9"
      },
      "outputs": [],
      "source": [
        "train_dat <- fread(\"../input/train_date.csv\", nrows = 1)\n",
        "train_num <- fread(\"../input/train_numeric.csv\", nrows = 1)\n",
        "train_cat <- fread(\"../input/test_categorical.csv\", nrows = 1)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "65beeed8-7f84-a7a6-574c-51fe1fcf229d"
      },
      "outputs": [],
      "source": [
        "#Numerical training set features\n",
        "features_num<-data.frame(features=names(train_num),Ftype=\"Numeric\")  \n",
        "\n",
        "#Date training set features\n",
        "features_dat<-data.frame(features=names(train_dat),Ftype=\"Date\")  \n",
        "\n",
        "#Categorical training set features\n",
        "features_cat<-data.frame(features=names(train_cat),Ftype=\"Categorical\")  \n",
        "\n",
        "# combine all \n",
        "features_stat<-rbind(features_num[2:969,],features_dat[2:1157,],features_cat[2:2141,])\n",
        "\n",
        "# extract line, station, feature number from feature names\n",
        "features_stat<-cbind(features_stat, colsplit(features_stat$features, split = \"_\", names = c(\"Line\",\"Station\",\"Fno\"))) # reshape\n",
        "features_stat[,3:5]<-apply(features_stat[,3:5], 2, function(x) as.numeric(gsub(\"[LSFD]\", \"\", x)))\n",
        "\n",
        "glimpse(features_stat)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "66bdaa9d-ae12-bab6-ca80-4db8bfbe5bc4"
      },
      "outputs": [],
      "source": [
        "# arrange features with accending order of feature no\n",
        "y<-features_stat%>%\n",
        "  arrange(desc(-Fno))\n",
        "\n",
        "glimpse(y) \n",
        "\n",
        "# we can see feature are ordered num-date-num and so on..."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "ecf6790f-de61-06e9-3eba-b823d5a8a9bd"
      },
      "outputs": [],
      "source": [
        "p <- plot_ly(y, x=Station,y=Fno,mode = \"markers\",symbol = Ftype,\n",
        "           colors = \"Set1\",marker=list( size=7 , opacity=1) )\n",
        "p%>%\n",
        "  layout(xaxis = list(title = \"Station No\"),yaxis = list(title = \"Feature No\"))\n",
        "p\n",
        "# Station 24, 25 have highest no of features 1177, 1184\n",
        "\n",
        "\n",
        "# i amnot sure why i cant see plot here instead i see table "
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "31df1699-a4ff-05c8-ca22-5277744fc3ea"
      },
      "outputs": [],
      "source": [
        "Lines <- unique(features_stat$Line)\n",
        "Lines\n",
        "# no of line present in the data"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "f828d1a8-8f92-7e84-391e-11e248a1d38d"
      },
      "outputs": [],
      "source": [
        "Stations <- unique(features_stat$Station)\n",
        "head(Stations)\n",
        "tail(Stations)\n",
        "# no of line present in the data"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "89fb10e5-2ff7-0cdd-5d28-ab2b4f528e4e"
      },
      "outputs": [],
      "source": [
        "#features per each station\n",
        "features_stat%>%\n",
        "  group_by(Line,Station)%>%\n",
        "  summarise(Total_features=n())   #tally()"
      ]
    }
  ],
  "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.1"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}