{"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)\n\n# Input data files are available in the \"../input/\" directory.\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": "%pylab inline\nimport matplotlib.pyplot as plt\nplt.style.use('ggplot')\nimport pandas as pd\n\ndf_train = pd.read_csv(\"../input/train.csv\")"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "def barplots(df):\n    columns = ['x', 'y', 'accuracy', 'time']\n    f, axarr = plt.subplots(1, len(columns), figsize=(18, 4))\n    for i, c in enumerate(columns):\n        axarr[i].hist(df[c], 100)\n        axarr[i].set_title(c)\n    f.show()\n    \nbarplots(df_train)"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "def scatterplot(df, column, lim):\n    f = pylab.figure(figsize=(6, 6))\n    ax = f.add_axes([0, 0, 1, 1])\n    df = df[df['x'] > lim[0][0]][df['x'] < lim[0][1]][df['y'] > lim[1][0]][df['y'] < lim[1][1]]\n    ax.scatter(df['x'], df['y'], c=df[column])\n    ax.set_xlim(lim[0])\n    ax.set_ylim(lim[1])\n    ax.set_title(column)\n    f.show()\n    \nscatterplot(df_train, 'place_id', ((2, 3), (4, 5)))"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "scatterplot(df_train, 'accuracy', ((2, 3), (4, 5)))"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "def plot(timeline):\n    f = pylab.figure(figsize=(20, 4))\n    ax = f.add_axes([0, 0, 1, 1])\n    ax.plot(timeline)\n    f.show()\n\ndef running_mean(x, N):\n    a = numpy.insert(np.array(x, dtype=pd.Series), 0, 0)\n    cumsum = np.cumsum(a) \n    return (cumsum[N:] - cumsum[:-N]) / N"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "for place_id in df_train['place_id'].value_counts().head().axes[0]:\n    barplots(df_train[df_train['place_id'] == place_id])"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "from sklearn.cross_validation import KFold\nfrom sklearn.ensemble import RandomForestRegressor\nfrom sklearn.metrics import r2_score\n\ndf_train_small = df_train[:100000]\ndata_small = df_train_small['x', 'y', 'accuracy', 'time']\ntarget_small = df_train_small['place_id']\n\ndef r2_scores_of_n_trees(data, target):\n    kf = KFold(len(data.index), n_folds=5, shuffle=True, random_state=1)\n    data = data.values\n    n_trees = 10\n    for train_index, test_index in kf:\n        clf = RandomForestRegressor(n_estimators=n, random_state=1)\n        clf.fit(data[train_index], target[train_index])\n        predictions = clf.predict(data[test_index])"
 }
],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"}}, "nbformat": 4, "nbformat_minor": 0}