{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "dd2e92c1-6da6-69e9-12c3-58a6abd366d4"
      },
      "source": [
        "I  just began this competition two days ago. I have been having a rough time creating numeric features I could feed through a machine learning algorithm from the pandas Series of lists contained in train['features']. So I've been doing some studying to improve my meager Python skills and the solution below is what I have arrived at. It quickly and easily makes a new feature out of any item from the train.features/test.features column that you might want to look at."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "58c528fa-ae51-08fc-e947-433c0c2ba401"
      },
      "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",
        "\n",
        "import numpy as np # linear algebra\n",
        "import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n",
        "\n",
        "pd.options.mode.chained_assignment = None\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",
        "\n",
        "from subprocess import check_output\n",
        "print(check_output([\"ls\", \"../input\"]).decode(\"utf8\"))\n",
        "\n",
        "# Any results you write to the current directory are saved as output."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "d7f58ab6-a675-d303-dfde-f50b38573cb3"
      },
      "source": [
        "One quick note: because of the limiting computing power available in Kaggle kernels I have chopped the dataframe down to just ten rows. This allows me to build and then demonstrate the usefulness of my little function here without causing my kernel to die an inglorious death.\n",
        "\n",
        "Viva kernels!"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "a3b92b5c-3247-07dc-770f-ef833ffe2a60"
      },
      "outputs": [],
      "source": [
        "train = pd.read_json('../input/train.json')\n",
        "train = train.iloc[:10, :]\n",
        "\n",
        "def newfeat(name, df, series):\n",
        "    \"\"\"Create a Series for my feature building loop to fill\"\"\"\n",
        "    feature = pd.Series(0, df.index, name=name)\n",
        "    \"\"\"Now populate the new Series with numeric values\"\"\"\n",
        "    for row, word in enumerate(series):\n",
        "        if name in word:\n",
        "            feature.iloc[row] = 1\n",
        "    df[name] = feature\n",
        "    return(df)\n",
        "   \n",
        "train = newfeat('Elevator', train, train.features)\n",
        "train = newfeat('Dogs Allowed', train, train.features)\n",
        "train = newfeat('Cats Allowed', train, train.features)\n",
        "\n",
        "print(train)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "a7a6940f-81d7-743f-99be-f07be340ea3f"
      },
      "source": [
        "So now I have numeric features corresponding to whether a given listing has an elevator or allows dogs and cats. \n",
        "\n",
        "Of course this kernel doesn't address the obvious issue of misspellings, use of hyphens, alternate word choices, or anything else that might throw off the accuracy of my new features. That is a project for another kernel.\n",
        "\n",
        "On a side note, I find it humorous that I felt compelled to figure out how to do this because I wanted to answer a single simple question: Is any given apartment pet friendly or not? And now I can find out!"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "98b30cfd-17e7-f1a0-e238-05a162b079d5"
      },
      "outputs": [],
      "source": [
        "train['pet_friendly'] = train['Cats Allowed'] + train['Dogs Allowed']\n",
        "print(train['pet_friendly'])"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "59f908d4-c660-1bcc-849c-7fe991f8c8f3"
      },
      "source": [
        "Well, that's all I have. Please upvote this kernel if you decide to use my code or you find what I've done here useful. I'm always in the hunt for medals, even if I am a beginner at this stuff. My observation to date has been that a combination of thoughtful feature engineering and hyperparameter tuning are the key to improving machine learning models. Hopefully this provides some fodder for the former."
      ]
    }
  ],
  "metadata": {
    "_change_revision": 0,
    "_is_fork": false,
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.6.0"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}