{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "a97fb2bf-de24-c9c2-49a7-3ccf1e558ed0"
      },
      "source": [
        "This is a first test noutbook"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "0e5ad33c-758b-fed2-6dac-ac1a46014fc8"
      },
      "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",
        "# 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": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "ece0f1d5-4f7b-28c1-ef0a-b89c61230501"
      },
      "outputs": [],
      "source": [
        "# Import the `RandomForestClassifier`\n",
        "from sklearn.ensemble import RandomForestClassifier\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",
        "# Load the train and test datasets to create two DataFrames\n",
        "train_url = \"../input/train.csv\"\n",
        "train = pd.read_csv(train_url)\n",
        "\n",
        "test_url = \"../input/test.csv\"\n",
        "test = pd.read_csv(test_url)\n",
        "\n",
        "# Create the target numpy array: target\n",
        "target = train[\"Survived\"].values\n",
        "\n",
        "#print(train[\"Sex\"][train[\"Sex\"] == \"male\"])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "6ea65fae-81d5-c6d8-eca5-5319cd3ef8f7"
      },
      "outputs": [],
      "source": [
        "# Convert the male and female groups to integer form\n",
        "train[\"Sex\"][train[\"Sex\"] == \"male\"] = 0\n",
        "train[\"Sex\"][train[\"Sex\"] == \"female\"] = 1\n",
        "\n",
        "test[\"Sex\"][test[\"Sex\"] == \"male\"] = 0\n",
        "test[\"Sex\"][test[\"Sex\"] == \"female\"] = 1\n",
        "\n",
        "# Impute the Embarked variable\n",
        "train[\"Embarked\"] = train[\"Embarked\"].fillna(\"S\") \n",
        "train[\"Age\"] = train[\"Age\"].fillna(29)\n",
        "\n",
        "test[\"Embarked\"] = test[\"Embarked\"].fillna(\"S\") \n",
        "test[\"Age\"] = test[\"Age\"].fillna(29) \n",
        "test.Fare[152] = test[\"Fare\"].median()\n",
        "\n",
        "# Convert the Embarked classes to integer form\n",
        "train[\"Embarked\"][train[\"Embarked\"] == \"S\"] = 0\n",
        "train[\"Embarked\"][train[\"Embarked\"] == \"C\"] = 1\n",
        "train[\"Embarked\"][train[\"Embarked\"] == \"Q\"] = 2\n",
        "\n",
        "test[\"Embarked\"][test[\"Embarked\"] == \"S\"] = 0\n",
        "test[\"Embarked\"][test[\"Embarked\"] == \"C\"] = 1\n",
        "test[\"Embarked\"][test[\"Embarked\"] == \"Q\"] = 2"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "c5a425c1-4ab5-411a-f15f-8ea100b8896a"
      },
      "outputs": [],
      "source": [
        "train[[\"Pclass\", \"Age\", \"Sex\", \"Fare\", \"SibSp\", \"Parch\", \"Embarked\"]].values"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "5e22c7e9-2914-e588-5e20-50d752a3858d"
      },
      "outputs": [],
      "source": [
        "sum(test[\"Fare\"].value_counts())"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "279aa18a-96cd-bbbd-638e-14a2f9dd97ce"
      },
      "outputs": [],
      "source": [
        "\n",
        "# We want the Pclass, Age, Sex, Fare,SibSp, Parch, and Embarked variables\n",
        "features_forest = train[[\"Pclass\", \"Age\", \"Sex\", \"Fare\", \"SibSp\", \"Parch\", \"Embarked\"]].values\n",
        "\n",
        "# Building and fitting my_forest\n",
        "forest = RandomForestClassifier(max_depth = 10, min_samples_split=2, n_estimators = 100, random_state = 1)\n",
        "my_forest = forest.fit(features_forest, target)\n",
        "\n",
        "# Print the score of the fitted random forest\n",
        "print(my_forest.score(features_forest, target))\n",
        "\n",
        "# Compute predictions on our test set features then print the length of the prediction vector\n",
        "test_features = test[[\"Pclass\", \"Age\", \"Sex\", \"Fare\", \"SibSp\", \"Parch\", \"Embarked\"]].values\n",
        "pred_forest = my_forest.predict(test_features)\n",
        "print(len(pred_forest))\n",
        "\n",
        "\n",
        "\n",
        "# Create a data frame with two columns: PassengerId & Survived. Survived contains your predictions\n",
        "PassengerId =np.array(test[\"PassengerId\"]).astype(int)\n",
        "my_solution = pd.DataFrame(pred_forest, PassengerId, columns = [\"Survived\"])\n",
        "print(my_solution)\n",
        "\n",
        "# Check that your data frame has 418 entries\n",
        "print(my_solution.shape)\n",
        "\n",
        "# Write your solution to a csv file with the name my_solution.csv\n",
        "my_solution.to_csv(\"my_solution_one.csv\", index_label = [\"PassengerId\"])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "3f3f1fce-73f1-94fc-38a0-b7e229ddaca5"
      },
      "outputs": [],
      "source": [
        "submission = my_solution"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "b7f8a0f7-d1cd-4caf-3d40-7504654905a6"
      },
      "outputs": [],
      "source": [
        "submission.to_csv('submission.csv', index_label = [\"PassengerId\"])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "b739f69f-7bfb-a230-2994-5e9905e674e7"
      },
      "outputs": [],
      "source": [
        "print(check_output([\"ls\", \"../working\"]).decode(\"utf8\"))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "fce6892c-3331-89cc-620d-f140aa1b23a8"
      },
      "outputs": [],
      "source": [
        ""
      ]
    }
  ],
  "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
}