{"cells":[
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "import numpy as np\nimport pandas as pd \nfrom subprocess import check_output\n#print(check_output([\"ls\", \"../input\"]).decode(\"utf8\"))\nimport datetime\nimport time"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "train1 = pd.read_csv(\"../input/train.csv\", nrows=100000)"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "from sklearn import svm"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "from sklearn.decomposition import PCA"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "test1 = pd.read_csv(\"../input/test.csv\", nrows=100)"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "train1[\"srch_ci\"] = pd.to_datetime(train1[\"srch_ci\"], format='%Y-%m-%d', errors=\"coerce\")"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "test1[\"srch_ci\"] = pd.to_datetime(test1[\"srch_ci\"], format='%Y-%m-%d', errors=\"coerce\")"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "train1[\"srch_co\"] = pd.to_datetime(train1[\"srch_co\"], format='%Y-%m-%d', errors=\"coerce\")"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "test1[\"srch_co\"] = pd.to_datetime(test1[\"srch_co\"], format='%Y-%m-%d', errors=\"coerce\")"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "train1[\"stay_span\"] = (train1[\"srch_co\"] - train1[\"srch_ci\"]).astype('timedelta64[D]')"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "test1[\"stay_span\"] = (test1[\"srch_co\"] - test1[\"srch_ci\"]).astype('timedelta64[D]')"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "train2 = pd.to_datetime(train1[\"date_time\"])"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "test2 = pd.to_datetime(test1[\"date_time\"])"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "train2 = pd.DataFrame(train2)\ntest2 = pd.DataFrame(test2)"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "train1['year'] = train2['date_time'].dt.year\ntrain1['month'] = train2['date_time'].dt.month\ntrain1['day_of_week'] = train2['date_time'].dt.dayofweek\ntrain1['day'] = train2['date_time'].dt.day\ntrain1['hour'] = train2['date_time'].dt.hour"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "test1['year'] = test2['date_time'].dt.year\ntest1['month'] = test2['date_time'].dt.month\ntest1['day_of_week'] = test2['date_time'].dt.dayofweek\ntest1['day'] = test2['date_time'].dt.day\ntest1['hour'] = test2['date_time'].dt.hour"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "train1[\"date_time\"] = pd.to_datetime(train1[\"date_time\"], format='%Y-%m-%d', errors=\"coerce\")\ntrain1[\"srch_ci\"] = pd.to_datetime(train1[\"srch_ci\"], format='%Y-%m-%d', errors=\"coerce\")\ntrain1[\"search_span\"] = (train1[\"srch_ci\"] - train1[\"date_time\"]).astype('timedelta64[D]')"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "test1[\"date_time\"] = pd.to_datetime(test1[\"date_time\"], format='%Y-%m-%d', errors=\"coerce\")\ntest1[\"srch_ci\"] = pd.to_datetime(test1[\"srch_ci\"], format='%Y-%m-%d', errors=\"coerce\")\ntest1[\"search_span\"] = (test1[\"srch_ci\"] - test1[\"date_time\"]).astype('timedelta64[D]')"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "train1.ix[(train1['hour'] >= 10) & (train1['hour'] < 18), 'hour'] = 1\ntrain1.ix[(train1['hour'] >= 18) & (train1['hour'] < 22), 'hour'] = 2\ntrain1.ix[(train1['hour'] >= 22) & (train1['hour'] == 24), 'hour'] = 3\ntrain1.ix[(train1['hour'] >= 1) & (train1['hour'] < 10), 'hour'] = 3"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "test1.ix[(test1['hour'] >= 10) & (test1['hour'] < 18), 'hour'] = 1\ntest1.ix[(test1['hour'] >= 18) & (test1['hour'] < 22), 'hour'] = 2\ntest1.ix[(test1['hour'] >= 22) & (test1['hour'] == 24), 'hour'] = 3\ntest1.ix[(test1['hour'] >= 1) & (test1['hour'] < 10), 'hour'] = 3"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "train1 = train1.drop('srch_ci', axis=1)"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "test1 = test1.drop('srch_ci', axis=1)"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "train1 = train1.drop('srch_co', axis=1)"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "test1 = test1.drop('srch_co', axis=1)"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "train1 = train1.drop('date_time', axis=1)"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "test1 = test1.drop('date_time', axis=1)"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "desti1 = pd.read_csv(\"../input/destinations.csv\")"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "Xt = desti1.ix[:,1:150]"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "yt = desti1.ix[:,0:1]"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "pca1 = PCA(n_components=3)\nX_train = pca1.fit(Xt).transform(Xt)"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "X_train1 = pd.DataFrame(X_train)"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "X_train1[\"srch_destination_id\"] = desti1[\"srch_destination_id\"]"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "X_train1.info()"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "pca2 = PCA(n_components=4)\nX_test = pca2.fit(Xt).transform(Xt)"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "X_test1 = pd.DataFrame(X_test)"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "X_test1[\"srch_destination_id\"] = desti1[\"srch_destination_id\"]"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "X_train1.columns = ['d0','d1','d2','srch_destination_id']"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "\nX_test1.columns = ['d0','d1','d3','d4','srch_destination_id']"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "train1 = train1.join(X_train1, on = 'srch_destination_id', how = 'left', rsuffix='dest')"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "test1 = test1.join(X_test1, on = 'srch_destination_id', how = 'left', rsuffix='dest')"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "train1 = train1.drop(\"srch_destination_iddest\", axis=1)"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "test1 = test1.drop(\"srch_destination_iddest\", axis=1)"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "train1.fillna(-1, inplace=True)"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "test1.fillna(-1, inplace=True)"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "hotelCluster = train1.ix[:,'hotel_cluster']"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "hotelCluster1 = pd.DataFrame(hotelCluster)"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "hotelCluster.shape"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "train1 = train1.drop('hotel_cluster', axis=1)"
 },
 {
  "cell_type": "markdown",
  "metadata": {},
  "source": "hc = np.array(hotelCluster1)"
 },
 {
  "cell_type": "markdown",
  "metadata": {},
  "source": "hc.shape"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "print(hotelCluster.shape)"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "clf = svm.SVC()"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "clf.fit(train1, hotelCluster)"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": ""
 }
],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"}}, "nbformat": 4, "nbformat_minor": 0}