{"cells":[
 {
  "cell_type": "markdown",
  "metadata": {},
  "source": "# Accuracy Explained\n\n\nauthor: \"Jean Soula\"\ndate: \"July 6, 2016\"\n\n**The complete version is available here** : https://github.com/jeansoula/Kaggle-Facebook/blob/master/FB-accuracy.ipynb\n\n# I. Import data\n\n### Load Libraries"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "%reset\n%matplotlib inline\n\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns # pip install seaborn\n\nsns.set(style=\"whitegrid\", color_codes=True)\npd.set_option('display.max_columns', None)"
 },
 {
  "cell_type": "markdown",
  "metadata": {},
  "source": "## Load CSV"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "df = pd.read_csv('../input/train.csv', header=0,sep=',')"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "#different place_id\nlen(df['place_id'].unique())"
 },
 {
  "cell_type": "markdown",
  "metadata": {},
  "source": "# II. Data Exploration \n\n#### Data cleaning"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "df_cl = df.copy()"
 },
 {
  "cell_type": "markdown",
  "metadata": {},
  "source": "#### Data viz"
 },
 {
  "cell_type": "markdown",
  "metadata": {},
  "source": "#### Number of event in function of time"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "ax = sns.distplot(df_cl[\"time\"], kde=False)"
 },
 {
  "cell_type": "markdown",
  "metadata": {},
  "source": "#### Number of event in function of accuracy"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "ax = sns.distplot(df_cl[\"accuracy\"], kde=False)"
 },
 {
  "cell_type": "markdown",
  "metadata": {},
  "source": "### Is accuracy linked to distance with place_id's center ?"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "aggregation = {\n    'x' : {\n        'median_x' : 'median',\n        'mean_x' : 'mean',\n        'variance_x' : 'var' \n    },\n    'y' : {\n        'median_y' : 'median',\n        'mean_y' : 'mean',\n        'var_y' : 'var'\n    }\n}\ndf_cl_agg = df_cl.groupby(\"place_id\").agg(aggregation)"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "df_cl_agg.columns = df_cl_agg.columns.droplevel()\ndf_cl_agg.head()"
 },
 {
  "cell_type": "markdown",
  "metadata": {},
  "source": "#### Distance of each point with the center of its place_id (euclidian)"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "result = pd.merge(df_cl, df_cl_agg, left_on='place_id', right_index=True, how='inner', suffixes=('_left', '_right'))"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "result.head()"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "result[\"distance_median\"] = np.sqrt( np.power((result[\"median_y\"]-result[\"y\"]), 2) + np.power((result[\"median_x\"]-result[\"x\"]), 2) )"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "result.head()"
 },
 {
  "cell_type": "markdown",
  "metadata": {},
  "source": "I struggled a lot to compute the last part of my jupyter notebook here, that's why I give you this [link] \n(https://github.com/jeansoula/Kaggle-Facebook/blob/master/FB-accuracy.ipynb) to follow the last part of my reasoning."
 },
 {
  "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}