{"cells":[
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom scipy.optimize import fmin_bfgs\nimport matplotlib\nimport matplotlib.pyplot as plot\n#from sklearn import preprocessing\n#from matplotlib import style\nimport pylab\nimport datetime\nimport re\n\n# Input data files are available in the \"../input/\" directory.\ndf = pd.read_csv('../input/train.csv',usecols = ['is_booking','srch_adults_cnt','srch_destination_id',\\\n'srch_ci','srch_co','hotel_cluster'],chunksize=1000)\ndf = pd.concat(df, ignore_index=True)\n\ndf = df.groupby(['is_booking']).get_group(1)\n\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nfrom subprocess import check_output\nprint(check_output([\"ls\", \"../input\"]).decode(\"utf8\"))\n\n# Any results you write to the current directory are saved as output."
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "dfx = df.ix[:,'hotel_cluster']\nylabel = dfx.value_counts()\nylabel = ylabel.index\ny = dfx.as_matrix()\n"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "dfx = df.ix[:,'srch_adults_cnt']\nx1 = dfx.as_matrix()\nmu = np.mean(x1)\ns = np.amax(x1)-np.amin(x1)\nx1 = (x1 - mu)/s\n"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "dfx = df.ix[:,'srch_destination_id']\nx2 = dfx.as_matrix()\nmu = np.mean(x2)\ns = np.amax(x2)-np.amin(x2)\nx2 = (x2 - mu)/s\n"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "dfx = df.ix[:,'srch_ci']\ndfx = pd.to_datetime(dfx)\nci = dfx.dt.year*365 + dfx.dt.month*30 + dfx.dt.day\n"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "dfx = df.ix[:,'srch_co']\ndfx = pd.to_datetime(dfx)\nco = (dfx.dt.year)*365 + (dfx.dt.month)*30 + dfx.dt.day\n"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "x3 = co - ci\nx3 = x3.as_matrix()\nmu = np.mean(x3)\ns = np.amax(x3)-np.amin(x3)\nx3 = (x3 - mu)/s\n"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "x0 = np.ones(len(y))\n\nX = np.vstack((x0,x1,x2,x3))\n"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "print(X.shape)"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "X = np.transpose(X)\n\nm, num_features = X.shape\nprint(X.shape, m, num_features)\nlmd = 1.0\nall_theta = []\n"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "initial_theta = np.random.rand(num_features,1)"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "theta = fmin_bfgs(costfunction, initial_theta , fprime = gradient, args = (X,y==0,lmd))\nprint(theta)"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "def sigmoid(z):\n\n  h = np.reciprocal(1.0 + np.exp(-z))\n  return h\n\n\ndef costfunction(theta,X,y,lmd):\n\n  #z = np.dot((np.transpose(X)),theta)\n  #print(X.shape, theta.shape)\n  z = np.dot( X , theta )\n  h = sigmoid(z)\n  m = len(y)\n  y = np.reshape( y , (-1,1))\n  n = len(theta)\n  J = (1.0/m)*( - np.dot(np.transpose(y),np.log(h)) \\\n                - np.dot(np.transpose(1 - y),np.log(1 - h)) ) \\\n      + (0.5*lmd/m)*( np.dot( np.transpose( theta[1:m] ) , theta[1:m]  )  )\n  return J\n\ndef gradient(theta,X,y,lmd):\n\n  m = len(y)\n  n = len(theta)\n  #print(X.shape,theta.shape)\n  #theta = np.reshape( theta, (-1,1) )\n  #print(X.shape,theta.shape)\n  z = np.dot(X , theta)\n  #z = np.dot( X , theta)\n  h = sigmoid(z)\n  #print(h.shape, y.shape, X.shape)\n  #rint(h.shape,y.shape)\n  grad = np.dot( np.transpose(X) , (h-y) )/m\n  temp = theta\n  temp[0] = 0\n  grad = grad + (lmd/m)*temp\n  return  grad\n"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "theta = fmin_bfgs(costfunction, initial_theta , fprime = gradient, args = (X,y==0,lmd))\nprint(theta)"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "theta = fmin_bfgs(costfunction, initial_theta , fprime = gradient, args = (X,y==0,lmd))\nall_theta.append(theta)"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "theta = fmin_bfgs(costfunction, initial_theta , fprime = gradient, args = (X,y==1,lmd))\nall_theta.append(theta)"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "theta = fmin_bfgs(costfunction, initial_theta , fprime = gradient, args = (X,y==2,lmd))\nall_theta.append(theta)"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "theta = fmin_bfgs(costfunction, initial_theta , fprime = gradient, args = (X,y==3,lmd))\nall_theta.append(theta)"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "theta = fmin_bfgs(costfunction, initial_theta , fprime = gradient, args = (X,y==4,lmd))\nall_theta.append(theta)"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "theta = fmin_bfgs(costfunction, initial_theta , fprime = gradient, args = (X,y==5,lmd))\nall_theta.append(theta)"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "theta = fmin_bfgs(costfunction, initial_theta , fprime = gradient, args = (X,y==5,lmd))\nall_theta.append(theta)"
 },
 {
  "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}