{"cells":[
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "#This code helps to find out feature multipliers for KNN.\n#This is shown using some features derived by me but this method can be extended for other features as well.\n#One needs to derive his own features and then apply similar approach to get the correct weights.\n\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom sklearn.linear_model import LogisticRegression\nlr = LogisticRegression()\n\nrecent_train = pd.read_csv(\"../input/train.csv\")\n\n#select a single x_y_grid at random\nrecent_train = recent_train[(recent_train[\"x\"]>4.5) &(recent_train[\"x\"]<5) &(recent_train[\"y\"]>2) &(recent_train[\"y\"]<2.3)]\n\n\n#derive some features\nrecent_train[\"x\"],recent_train[\"y\"] = recent_train[\"x\"]*1000,recent_train[\"y\"]*1000\nrecent_train[\"hour\"] = recent_train[\"time\"]//60\nrecent_train[\"hour_of_day\"] = recent_train[\"hour\"]%24 + 1\n\nrecent_train[\"day\"] = recent_train[\"hour\"]//24\nrecent_train[\"day_of_week\"] = recent_train[\"day\"]%7 + 1\n\nrecent_train[\"month\"] = recent_train[\"day\"]//30 + 1\nrecent_train[\"month_of_year\"] = (recent_train[\"month\"]-1)%12 + 1 \n\nrecent_train[\"accuracy\"] = np.log(recent_train[\"accuracy\"])\n\nprint(\"recent_train created\")"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "#creating arbitrary test\ntest = recent_train.sample(axis = 0,frac = 0.05)\nprint (\"selected_part and test created\")\nfeatures = [\"x\",\"y\",\"hour_of_day\",\"day_of_week\",\"month_of_year\",\"accuracy\"]\nfw = [1,1,0,0,0,0] #at first iteration\nfw = [ 1.  ,      1.43696871,  1.26585057,  0.78156837,  1.28182383,  7.01014412] #at second iteration\n\nprint (len(test))"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "colname = str(features)\ntest[colname] = list\nindex = iter(test.index)\ntest[\"done\"] = 0\ncount = 0\nfor i in index:\n    new_ld = pd.DataFrame(columns = features)\n    for j in range(15):\n        new_ld1 = abs(recent_train[features] - test.loc[i][features])\n        new_ld1 = new_ld1.drop(i)\n        new_ld1[\"target\"] = (recent_train[\"place_id\"] != test.loc[i][\"place_id\"]) + 0\n        new_ld1[\"x+y\"] = np.sum(new_ld1[features]*fw,axis = 1)#(new_ld1[\"x\"])+(new_ld1[\"y\"])#\n        new_ld1 = new_ld1.sort(\"x+y\")[0:50]\n        count += 1\n        i = next(index)\n        true = new_ld1[new_ld1[\"target\"] == 0]\n        false = new_ld1[new_ld1[\"target\"] != 0]\n        if (len(true)< 10) | (len(false)< 10):\n            continue\n        new_ld = new_ld.append(new_ld1)\n    lr.fit(new_ld[features],new_ld[\"target\"])\n    test.set_value(i,colname,lr.coef_.ravel())\n    test.set_value(i,\"done\",1)\n    print (\"current status: sample number\",count)"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "#average or sum all the multipliers to get overall multiplier\nactual_test2 = test[test[\"done\"]==1]\nfinal_weights = np.array([0,0,0,0,0,0])\nfor lists in actual_test2[colname]:\n    final_weights = final_weights + lists\n\n\nprint (features) \nprint (\"corresponding weights\")\nprint (final_weights/final_weights[0])"
 },
 {
  "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}