{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "22d173e2-0d3e-6fab-a853-9725011d0ebc"
      },
      "outputs": [],
      "source": [
        "import pandas as pd\n",
        "import numpy as np\n",
        "import matplotlib.pyplot as plt\n",
        "from sklearn import model_selection, metrics\n",
        "from sklearn import linear_model\n",
        "\n",
        "def scorer(mod,X,target):\n",
        "    return metrics.matthews_corrcoef(target, mod.predict(X))\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"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "318658ea-c8a0-264e-2491-5af4e8cbee08"
      },
      "outputs": [],
      "source": [
        "N_lines = 1000\n",
        "df=pd.read_csv(\"../input/train_categorical.csv\",sep=\",\", nrows = N_lines, na_filter=False)\n",
        "df_num=pd.read_csv(\"../input/train_numeric.csv\",sep=\",\",nrows=N_lines, na_filter=False)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "d5d27776-06cb-925b-6505-baa453a30b57"
      },
      "outputs": [],
      "source": [
        "target=df_num.Response.values\n",
        "folds=8\n",
        "print(np.sum(target), 112/folds)\n",
        "np.random.seed(1994)\n",
        "shuffle_split = cross_validation.StratifiedShuffleSplit(target, n_iter = folds)\n",
        "\n",
        "bin_feat=np.zeros(df.values.shape, dtype='int')\n",
        "bin_feat[df.values!=\"\"]=1"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "d50966c3-a23d-aeb0-7f47-7ca8fba92a01"
      },
      "outputs": [],
      "source": [
        "C_val=np.linspace(-5, 5, 10)\n",
        "res=[]\n",
        "for c in C_val:\n",
        "    logreg = linear_model.LogisticRegression(class_weight=\"balanced\", C=pow(10,c))\n",
        "    res+=[cross_validation.cross_val_score(logreg, bin_feat, target, cv=shuffle_split, scoring=scorer, n_jobs=-1)]# scoring=scorer"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "33dba16b-b60a-7647-6ec6-8224462972aa"
      },
      "outputs": [],
      "source": [
        "#print([(np.min(i), np.max(i)) for i in res])\n",
        "plt.plot(C_val, [np.mean(i) for i in res])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "2be5eede-aeee-7ea7-da4f-460d8e0ed98f"
      },
      "outputs": [],
      "source": [
        "C_val=np.linspace(-5, 0, 1)\n",
        "res=[]\n",
        "for c in C_val:\n",
        "    lass = linear_model.Lasso(alpha=c, normalize=True)\n",
        "    res+=[cross_validation.cross_val_score(lass, bin_feat, target, cv=shuffle_split, scoring=scorer, n_jobs=-1)]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "6d5b6e6f-ddf3-9a03-0f59-7b85e8a1b4c0"
      },
      "outputs": [],
      "source": [
        "plt.plot(C_val, [np.mean(i) for i in res])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "9542bc21-07c8-327c-ef7f-26bb7f57e9e2"
      },
      "outputs": [],
      "source": [
        "np.unique(bin_feat)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "252ed29d-ce71-c755-7263-87bb90e364b1"
      },
      "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.5.2"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}