{"nbformat": 4, "metadata": {"language_info": {"mimetype": "text/x-python", "name": "python", "version": "3.6.3", "file_extension": ".py", "nbconvert_exporter": "python", "codemirror_mode": {"name": "ipython", "version": 3}, "pygments_lexer": "ipython3"}, "kernelspec": {"name": "python3", "display_name": "Python 3", "language": "python"}}, "nbformat_minor": 1, "cells": [{"execution_count": null, "cell_type": "code", "metadata": {"_cell_guid": "7d9f3cbc-8c3d-47f3-96aa-1b3ad9b95ff9", "collapsed": true, "_uuid": "564324b724c188fe9668c43464eead918809809a"}, "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.\n"]}, {"execution_count": null, "cell_type": "code", "metadata": {"_cell_guid": "aec65be2-d7ee-4091-b173-c7e4fa40b381", "collapsed": true, "_uuid": "1bbd8146bf159c85483ea6d473043ba3f0d31b27"}, "outputs": [], "source": ["transactions = pd.read_csv('../input/transactions_v2.csv')\n", "members = pd.read_csv('../input/members_v2.csv')\n", "train = pd.read_csv('../input/train_v2.csv')\n", "users = pd.read_csv('../input/user_logs_v2.csv')"]}, {"execution_count": null, "cell_type": "code", "metadata": {"_cell_guid": "5e78943d-0041-49ec-a497-5df4671dc548", "collapsed": true, "_uuid": "59297d90a0aa0d55be0b7d1a53f4dc12dc320611"}, "outputs": [], "source": ["df1 = members.merge(transactions, how='inner', on = 'msno', copy=False)\n", "df2 = df1.merge(users, how= 'inner', on = 'msno', copy=False)\n", "df = df2.merge(train, how = 'inner', on = 'msno', copy = False)\n", "df.head(10)"]}, {"execution_count": null, "cell_type": "code", "metadata": {"_cell_guid": "bbc18092-4527-4334-9ecd-e6a41958b008", "collapsed": true, "_uuid": "1916f25a8c5ca8caaebd8fa92932107d3454eafc"}, "outputs": [], "source": ["df.describe()"]}, {"execution_count": null, "cell_type": "code", "metadata": {"_cell_guid": "2f61657e-6179-48fe-bd06-75616110fe1b", "collapsed": true, "_uuid": "44dff094d05c81449e578897a74103d5845998d2"}, "outputs": [], "source": ["df.isnull().sum()"]}, {"execution_count": null, "cell_type": "code", "metadata": {"_cell_guid": "270e1f62-cff3-4ff9-b446-79393bf2f813", "collapsed": true, "_uuid": "3dcabf64acb07ebb0536a9b15bfb49ca7c19d55a"}, "outputs": [], "source": ["import matplotlib.pyplot as plt\n", "import seaborn as sns"]}, {"execution_count": null, "cell_type": "code", "metadata": {"_cell_guid": "81ce21a0-59d6-4bf1-8f28-11c8cf31308b", "collapsed": true, "_uuid": "7f008724fb316ca36a95c0042eb8fd07dcedb3e5"}, "outputs": [], "source": ["num_cols = ['bd', 'num_25', 'num_50', 'num_75', 'num_985', 'num_100','num_unq',\n", "           'total_secs']"]}, {"execution_count": null, "cell_type": "code", "metadata": {"_cell_guid": "1fa35a02-2ab7-4261-ac2b-a6618d4e4e59", "collapsed": true, "_uuid": "755f7bf0c5e3c14dc8947f90c699e0f12ec45a7e"}, "outputs": [], "source": ["facet = None\n", "for i in range(0,len(num_cols),2):\n", "    if len(num_cols) > i+1:\n", "        plt.figure(figsize=(10,4))\n", "        plt.subplot(121)\n", "        sns.boxplot(facet, num_cols[i],data = df)\n", "        plt.subplot(122)            \n", "        sns.boxplot(facet, num_cols[i+1],data = df)\n", "        plt.tight_layout()\n", "        plt.show()\n", "\n", "    else:\n", "        sns.boxplot(facet, num_cols[i],data = df)"]}, {"execution_count": null, "cell_type": "code", "metadata": {"_cell_guid": "8f5a0f77-7dfe-4343-a687-e959a51ffd7d", "collapsed": true, "_uuid": "52f2278c47b80f2fc92e62626ac0f8dda41ee23d"}, "outputs": [], "source": ["df = df.drop(columns=['msno','transaction_date', 'membership_expire_date', 'gender',\n", "                      'registration_init_time', 'date', 'payment_method_id', 'actual_amount_paid'], axis = 1)\n", "df.head(10)"]}, {"execution_count": null, "cell_type": "code", "metadata": {"_cell_guid": "cb724091-0a88-44af-a4ba-bbdab09edf1a", "collapsed": true, "_uuid": "fc5739360bf4fc9ea522a4bd81c7d39cf449ced8"}, "outputs": [], "source": ["from sklearn.linear_model import LogisticRegression\n", "from sklearn.model_selection import train_test_split"]}, {"execution_count": null, "cell_type": "code", "metadata": {"_cell_guid": "28fc7ca5-d9ab-4c55-954a-5e89fec77dbe", "collapsed": true, "_uuid": "169fa2e23a5f1f083c51ad5ef46e3b7b50a3e6a9"}, "outputs": [], "source": ["X = df.iloc[:,:-1]\n", "y = df.iloc[:,-1]\n", "X.head(10)"]}, {"execution_count": null, "cell_type": "code", "metadata": {"_kg_hide-input": true, "_cell_guid": "2004ee00-08a4-401a-bc36-feac6b6a90ce", "_kg_hide-output": true, "collapsed": true, "_uuid": "033ad08f4626e4bd2e31faaedd28dae5b7ac1754"}, "outputs": [], "source": ["X_train, X_test, y_train, y_test = train_test_split(X,y, test_size = 1/3, random_state = 0)\n", "print(X_train.shape, X_test.shape, y_train.shape)"]}, {"execution_count": null, "cell_type": "code", "metadata": {"_cell_guid": "f93bd190-91cf-4cc0-a70f-6745990c1502", "collapsed": true, "_uuid": "f53ceda2e796e3af4d38f3b0ed57916ce428410f"}, "outputs": [], "source": ["lr = LogisticRegression()"]}, {"execution_count": null, "cell_type": "code", "metadata": {"_cell_guid": "3f5947c4-0322-42e3-b325-6877e80d7866", "collapsed": true, "_uuid": "ff1f1e777cc9a1e1fd1bae83e793b4ad0c9d512e"}, "outputs": [], "source": ["lr.fit(X_train, y_train)\n", "y_pred = lr.predict(X_test)"]}, {"execution_count": null, "cell_type": "code", "metadata": {"_cell_guid": "760ef3ba-6e84-4a85-81c4-ca37a639bbcd", "collapsed": true, "_uuid": "1d9f91bd1ae1bc3bf65dafd2d2197e72f7825fe8"}, "outputs": [], "source": ["from sklearn.metrics import confusion_matrix as cm\n", "from sklearn.metrics import r2_score\n", "from sklearn.metrics import mean_absolute_error"]}, {"execution_count": null, "cell_type": "code", "metadata": {"_cell_guid": "5c59bb33-c2e0-4d02-a012-3a2733aec279", "collapsed": true, "_uuid": "3f07bd51da7f18df1592de1d4b0319bb041f849a"}, "outputs": [], "source": ["acc = cm(y_test, y_pred)\n", "acc"]}, {"execution_count": null, "cell_type": "code", "metadata": {"_cell_guid": "233e438a-0e34-4c62-8841-c93310491252", "collapsed": true, "_uuid": "5df7ebaeeca62e06b4c7ca47f7403a5eb16b8f46"}, "outputs": [], "source": ["r2_score(y_test, y_pred)"]}, {"execution_count": null, "cell_type": "code", "metadata": {"_cell_guid": "d0f0547d-5cee-44b2-94ae-088c249257c9", "collapsed": true, "_uuid": "ace13f8e65d03b00c7c259ba0a38834387196933"}, "outputs": [], "source": ["abserr = mean_absolute_error(y_test, y_pred)"]}, {"execution_count": null, "cell_type": "code", "metadata": {"_cell_guid": "bbe9e9dc-18ef-479b-bd14-c946c212818b", "collapsed": true, "_uuid": "bd791e9aeacfc7353669a2f2ca76a5317ddc603f"}, "outputs": [], "source": ["abserr"]}, {"execution_count": null, "cell_type": "code", "metadata": {"_cell_guid": "ca83f38e-20c5-4d03-b602-9e173bf68597", "collapsed": true, "_uuid": "7482a731d5548c2567f980fe90eafa03217f69b7"}, "outputs": [], "source": []}]}