{"metadata": {"language_info": {"mimetype": "text/x-python", "version": "3.6.1", "file_extension": ".py", "name": "python", "nbconvert_exporter": "python", "codemirror_mode": {"name": "ipython", "version": 3}, "pygments_lexer": "ipython3"}, "kernelspec": {"name": "python3", "language": "python", "display_name": "Python 3"}}, "cells": [{"source": ["### Forked from: Regressing During Insomnia [0.21496]\n", "\n", "* Modified to save out output merged files/data + FE + dates parsing"], "cell_type": "markdown", "metadata": {"_cell_guid": "fe54fd0c-a173-4888-9f4d-8aef82ef46ad", "_uuid": "3d769c8a43ebc81bb5dcb645477202096d42cb80"}}, {"outputs": [], "source": ["from multiprocessing import Pool, cpu_count\n", "import gc; gc.enable()\n", "import xgboost as xgb\n", "import pandas as pd\n", "import numpy as np\n", "from sklearn import *\n", "import sklearn\n", "from sklearn.model_selection import train_test_split \n", "from sklearn.ensemble import RandomForestClassifier, IsolationForest\n", "from sklearn.metrics import classification_report"], "cell_type": "code", "execution_count": null, "metadata": {"_cell_guid": "f229ddc8-6924-4abf-8719-e0ec3c652863", "collapsed": true, "_uuid": "0797f9105fc62785850af750fd949002f8b9a322"}}, {"outputs": [], "source": ["train = pd.read_csv('../input/train.csv')\n", "# test = pd.read_csv('../input/sample_submission_zero.csv')\n", "test = pd.read_csv('../input/sample_submission_zero.csv',dtype = {'msno' : str}) # new: alt\n", "\n", "transactions = pd.read_csv('../input/transactions.csv', usecols=['msno'])\n", "transactions = pd.DataFrame(transactions['msno'].value_counts().reset_index())\n", "transactions.columns = ['msno','trans_count']\n", "train = pd.merge(train, transactions, how='left', on='msno')\n", "test = pd.merge(test, transactions, how='left', on='msno')\n", "transactions = []; print('transaction merge...')\n", "\n", "user_logs = pd.read_csv('../input/user_logs.csv', usecols=['msno'])\n", "user_logs = pd.DataFrame(user_logs['msno'].value_counts().reset_index())\n", "user_logs.columns = ['msno','logs_count']\n", "train = pd.merge(train, user_logs, how='left', on='msno')\n", "test = pd.merge(test, user_logs, how='left', on='msno')\n", "user_logs = []; print('user logs merge...')\n", "\n", "members = pd.read_csv('../input/members.csv')\n", "train = pd.merge(train, members, how='left', on='msno')\n", "test = pd.merge(test, members, how='left', on='msno')\n", "members = []; print('members merge...') "], "cell_type": "code", "execution_count": null, "metadata": {"_cell_guid": "34b22abb-18d4-49c1-a855-5e1ae5e69c73", "collapsed": true, "_uuid": "2676faf0df55da6bce99d1d1ea87c9247b55a58b"}}, {"outputs": [], "source": ["gender = {'male':1, 'female':2}\n", "train['gender'] = train['gender'].map(gender)\n", "test['gender'] = test['gender'].map(gender)\n", "\n", "# train = train.fillna(-1)\n", "# test = test.fillna(-1)"], "cell_type": "code", "execution_count": null, "metadata": {"_cell_guid": "48c17f47-87b0-4bc5-99f2-f4ba90faa921", "collapsed": true, "_uuid": "b24e7f1fb0c01887208a020356d783e46c7b4172"}}, {"outputs": [], "source": ["transactions = pd.read_csv('../input/transactions.csv')\n", "transactions = transactions.sort_values(by=['transaction_date'], ascending=[False]).reset_index(drop=True)\n", "transactions = transactions.drop_duplicates(subset=['msno'], keep='first')\n", "\n", "train = pd.merge(train, transactions, how='left', on='msno')\n", "test = pd.merge(test, transactions, how='left', on='msno')\n", "transactions=[]"], "cell_type": "code", "execution_count": null, "metadata": {"_cell_guid": "4a52098a-2eee-401f-8b5d-eddaa5e28fbd", "collapsed": true, "_uuid": "2d1849e5b6c07933039b3d025b3390a7db320c01"}}, {"source": ["### ALT + Some Aggregated feature engineering in advance.\n", "* Source: https://www.kaggle.com/talysacc/lgbm-starter-lb-0-23434\n", "* Different pipeline"], "cell_type": "markdown", "metadata": {"_cell_guid": "69cf9d20-4618-4586-a640-5abc4b4e281f", "_uuid": "95a581e1a55f7602516b88a7128a5e80ae859491"}}, {"outputs": [], "source": ["## Add features to test & train\n", "\n", "# df_members = pd.read_csv('../input/members.csv',dtype={'registered_via' : np.uint8,\n", "#                                                       'gender' : 'category'})\n", "# df_test = pd.merge(left=df_test,right=df_members,how='left',on=['msno'])\n", "# del df_members\n", "\n", "df_transactions = pd.read_csv('../input/transactions.csv',dtype = {'payment_method' : np.uint8,\n", "                                                                  'payment_plan_days' : np.uint8,\n", "                                                                  'plan_list_price' : np.uint8,\n", "                                                                  'actual_amount_paid': np.uint8,\n", "                                                                  'is_auto_renew' : np.bool,\n", "                                                                  'is_cancel' : np.bool})\n", "# orig: left = test ...\n", "df_transactions = pd.merge(left = test[['msno']],right = df_transactions,how='left',on='msno')\n", "grouped  = df_transactions.copy().groupby('msno')\n", "\n", "df_stats = grouped.agg({'msno' : {'total_order' : 'count'},\n", "                         'plan_list_price' : {'plan_net_worth' : 'sum'},\n", "                         'actual_amount_paid' : {'mean_payment_each_transaction' : 'mean',\n", "                                                  'total_actual_payment' : 'sum'},\n", "                         'is_cancel' : {'cancel_times' : lambda x : sum(x==1)}})\n", "             \n", "df_stats.columns = df_stats.columns.droplevel(0)\n", "df_stats.reset_index(inplace=True)\n", "\n", "test = pd.merge(left = test,right = df_stats,how='left',on='msno')\n", "del df_transactions"], "cell_type": "code", "execution_count": null, "metadata": {"_cell_guid": "f8d2839e-c5bd-423d-9172-e86c64aaef1c", "collapsed": true, "scrolled": true, "_uuid": "7ed051e1af672cea839c0975f9129b5835076840"}}, {"outputs": [], "source": ["## Add features to train\n", "df_transactions = pd.read_csv('../input/transactions.csv',dtype = {'payment_method' : np.uint8,\n", "                                                                  'payment_plan_days' : np.uint8,\n", "                                                                  'plan_list_price' : np.uint8,\n", "                                                                  'actual_amount_paid': np.uint8,\n", "                                                                  'is_auto_renew' : np.bool,\n", "                                                                  'is_cancel' : np.bool})\n", "# orig: left = test ...\n", "df_transactions = pd.merge(left = train[['msno']],right = df_transactions,how='left',on='msno')\n", "grouped  = df_transactions.copy().groupby('msno')\n", "\n", "df_stats = grouped.agg({'msno' : {'total_order' : 'count'},\n", "                         'plan_list_price' : {'plan_net_worth' : 'sum'},\n", "                         'actual_amount_paid' : {'mean_payment_each_transaction' : 'mean',\n", "                                                  'total_actual_payment' : 'sum'},\n", "                         'is_cancel' : {'cancel_times' : lambda x : sum(x==1)}})\n", "             \n", "df_stats.columns = df_stats.columns.droplevel(0)\n", "df_stats.reset_index(inplace=True)\n", "\n", "train = pd.merge(left = train,right = df_stats,how='left',on='msno')\n", "del df_transactions"], "cell_type": "code", "execution_count": null, "metadata": {"_cell_guid": "ac85cc93-e96e-47ba-a23a-fbbc6c09326a", "collapsed": true, "_uuid": "3402eddf1d936f331f61f432438c8c4aa0792864"}}, {"source": ["### Back to original (insomnia) pipeline"], "cell_type": "markdown", "metadata": {"_cell_guid": "2878cc4d-e0ba-4e6a-b368-c4181dbfbb03", "_uuid": "ffaefab1770d12b4441b101e8cb3387ce554fad0"}}, {"outputs": [], "source": ["def transform_df(df):\n", "    df = pd.DataFrame(df)\n", "    df = df.sort_values(by=['date'], ascending=[False])\n", "    df = df.reset_index(drop=True)\n", "    df = df.drop_duplicates(subset=['msno'], keep='first')\n", "    return df\n", "\n", "def transform_df2(df):\n", "    df = df.sort_values(by=['date'], ascending=[False])\n", "    df = df.reset_index(drop=True)\n", "    df = df.drop_duplicates(subset=['msno'], keep='first')\n", "    return df\n", "\n", "df_iter = pd.read_csv('../input/user_logs.csv', low_memory=False, iterator=True, chunksize=10000000)\n", "last_user_logs = []\n", "i = 0 #~400 Million Records - starting at the end but remove locally if needed\n", "for df in df_iter:\n", "    if i>35:\n", "        if len(df)>0:\n", "            print(df.shape)\n", "            p = Pool(cpu_count())\n", "            df = p.map(transform_df, np.array_split(df, cpu_count()))   \n", "            df = pd.concat(df, axis=0, ignore_index=True).reset_index(drop=True)\n", "            df = transform_df2(df)\n", "            p.close(); p.join()\n", "            last_user_logs.append(df)\n", "            print('...', df.shape)\n", "            df = []\n", "    i+=1\n", "\n", "last_user_logs = pd.concat(last_user_logs, axis=0, ignore_index=True).reset_index(drop=True)\n", "last_user_logs = transform_df2(last_user_logs)\n", "\n", "train = pd.merge(train, last_user_logs, how='left', on='msno')\n", "test = pd.merge(test, last_user_logs, how='left', on='msno')\n", "last_user_logs=[]"], "cell_type": "code", "execution_count": null, "metadata": {"_cell_guid": "ef0ff7bd-cc4a-4603-b8a4-72591b6ba6c7", "collapsed": true, "_uuid": "0de7839769997cade8bb31b96ec40df398edee90"}}, {"outputs": [], "source": ["train.dtypes"], "cell_type": "code", "execution_count": null, "metadata": {"_cell_guid": "63b426e1-12d5-4f64-a5e2-42b0df923a5f", "collapsed": true, "_uuid": "8fd66ac5c332a807950f2373e2ded33f6f236d33"}}, {"outputs": [], "source": ["test.dtypes"], "cell_type": "code", "execution_count": null, "metadata": {"_cell_guid": "0c622d67-75bb-464e-aa1c-73e26ae616a7", "collapsed": true, "_uuid": "db69e18255b612b1f37a8eefd15acf9bd731d3d0"}}, {"outputs": [], "source": ["test.info()"], "cell_type": "code", "execution_count": null, "metadata": {"_cell_guid": "d0dafd9a-4bf4-42ba-b322-bf4318e7798e", "collapsed": true, "_uuid": "3fcc1ba27c63be2e62b469cd2d93c0147d8451cd"}}, {"outputs": [], "source": ["test.tail()"], "cell_type": "code", "execution_count": null, "metadata": {"_cell_guid": "4f9ad0a4-45c1-44cb-96a6-e1dd391e5a57", "collapsed": true, "_uuid": "1301fc31fbf741c7c3a0e445569aca43946b3cf0"}}, {"outputs": [], "source": ["test.loc[test.msno==\"oECkzJik4wKsbOEVY6UACLbmgM8qymFdb5cJaHrodY8=\"]"], "cell_type": "code", "execution_count": null, "metadata": {"_cell_guid": "d667fd20-c00f-456e-b517-b157724578d0", "collapsed": true, "scrolled": true, "_uuid": "e8a7291ec95ecaff2d886a697cabba612cd7f67d"}}, {"source": ["#### We have at least 1 mystery user for whom we have no data.\n", "* this causes our test data to be stored using mostly floats instead of ints (due to nan handling). "], "cell_type": "markdown", "metadata": {"_cell_guid": "c942cb35-682a-464f-996f-5c4b30885195", "_uuid": "9bbce05307ccacd7b4859e6e1b9250ebc44866db"}}, {"outputs": [], "source": ["test.shape"], "cell_type": "code", "execution_count": null, "metadata": {"_cell_guid": "a51ef25a-41b5-4a21-bfac-fd160ccce706", "collapsed": true, "_uuid": "144e3fbfed96da80954b6a848064a69ce9f0cb47"}}, {"outputs": [], "source": ["# test.to_numeric().shape"], "cell_type": "code", "execution_count": null, "metadata": {"_cell_guid": "cddd3c54-6d11-42ca-810c-0afea1b6d640", "collapsed": true, "_uuid": "ad669efa121f459d33e10e15d5d5ed9697342224"}}, {"source": ["## Parse as DateTime\n", "\n", "* __Note__ that many types are doubles in test, but not in train (the last row in test in missing many vals)"], "cell_type": "markdown", "metadata": {"_cell_guid": "d12ecb46-6d34-4f33-9ab6-f4d6d44b0182", "_uuid": "4f90a24dc4a0d6c877ddb04a91ac5eb7e61aca2d"}}, {"outputs": [], "source": ["dateCols = [\"registration_init_time\",\"transaction_date\", \"membership_expire_date\",\"expiration_date\",\"date\"]"], "cell_type": "code", "execution_count": null, "metadata": {"_cell_guid": "38172659-584a-40a0-9aea-eed152d69d7b", "collapsed": true, "_uuid": "bef94861468c0d88cc6a8f17e3f808aa5f3089aa"}}, {"outputs": [], "source": ["# train.registration_init_time = pd.to_datetime(train.registration_init_time.astype(int),format=\"%Y%m%d\")\n", "# test.registration_init_time = pd.to_datetime(test.registration_init_time.astype(int),format=\"%Y%m%d\")\n", "\n", "# train.transaction_date = pd.to_datetime(train.transaction_date.astype(int),format=\"%Y%m%d\")\n", "# test.transaction_date = pd.to_datetime(test.transaction_date.astype(int),format=\"%Y%m%d\")\n", "\n", "# train.membership_expire_date = pd.to_datetime(train.membership_expire_date.astype(int),format=\"%Y%m%d\")\n", "# test.membership_expire_date = pd.to_datetime(test.membership_expire_date.astype(int),format=\"%Y%m%d\")"], "cell_type": "code", "execution_count": null, "metadata": {"_cell_guid": "14310bf0-c56f-4abb-baae-bac3e85ae1e2", "collapsed": true, "_uuid": "989cf676fda0cd2938fd2cbd7473657da4ce1211"}}, {"outputs": [], "source": ["train.registration_init_time = pd.to_datetime(train.registration_init_time,format=\"%Y%m%d\")\n", "test.registration_init_time = pd.to_datetime(test.registration_init_time,format=\"%Y%m%d\")\n", "\n", "train.transaction_date = pd.to_datetime(train.transaction_date,format=\"%Y%m%d\")\n", "test.transaction_date = pd.to_datetime(test.transaction_date,format=\"%Y%m%d\")\n", "\n", "train.membership_expire_date = pd.to_datetime(train.membership_expire_date,format=\"%Y%m%d\")\n", "test.membership_expire_date = pd.to_datetime(test.membership_expire_date,format=\"%Y%m%d\")\n", "\n", "train.expiration_date = pd.to_datetime(train.expiration_date,format=\"%Y%m%d\")\n", "test.expiration_date = pd.to_datetime(test.expiration_date,format=\"%Y%m%d\")\n", "\n", "train.date = pd.to_datetime(train.date,format=\"%Y%m%d\")\n", "test.date = pd.to_datetime(test.date,format=\"%Y%m%d\")"], "cell_type": "code", "execution_count": null, "metadata": {"_cell_guid": "26d51472-b3c0-4eec-8857-a6ef66843cff", "collapsed": true, "_uuid": "71e8d559fba8204f37303cb5b4f02ab48565341b"}}, {"outputs": [], "source": ["train[dateCols+[\"is_churn\"]].head()"], "cell_type": "code", "execution_count": null, "metadata": {"_cell_guid": "f81013f3-a4a1-4e20-915c-101e53e4a3f4", "collapsed": true, "_uuid": "1f7a9e0fe1c1cd9453b77ee7bbdd9b499fed6077"}}, {"outputs": [], "source": ["train[\"sum_nan\"] = train.isnull().sum(axis=1)\n", "# train[\"sum_nan\"].describe()\n", "test[\"sum_nan\"] = test.isnull().sum(axis=1)\n", "test[\"sum_nan\"].describe()"], "cell_type": "code", "execution_count": null, "metadata": {"_cell_guid": "c149658a-8398-4114-adcf-49cb24c0f0dd", "collapsed": true, "_uuid": "8506a7b5d511a2e8fabbfd3c6d8a87dd48155699"}}, {"source": ["### Add DateTime Features"], "cell_type": "markdown", "metadata": {"_cell_guid": "c482aba5-b7eb-47ba-877f-6a02184e4b4c", "_uuid": "e6ea17260a58ca7a151b90e8893956ff343f5cd6"}}, {"outputs": [], "source": ["def get_date_diffs(df):\n", "    \"\"\"\n", "    Get time between the expiry date and other columns in days + add day of week, month features.\n", "     - Could add more, e.g. time between other dates, is weekend, etc' .\n", "     membership_expire_date is the deciding date for churn determination.\n", "    \"\"\"\n", "    df[\"exp-registration-diff\"] = (df.membership_expire_date - df.registration_init_time ).dt.days\n", "    df[\"exp-transaction-diff\"] = (df.membership_expire_date - df.transaction_date ).dt.days\n", "    df[\"exp-expiration-diff\"] = (df.membership_expire_date - df.expiration_date ).dt.days\n", "    df[\"exp-logdate-diff\"] = (df.membership_expire_date - df[\"date\"] ).dt.days\n", "    \n", "    for col in dateCols:\n", "        df[\"dayOfWeek_%s\" %(col)] = df[col].dt.dayofweek\n", "        df[\"dayOfMonth_%s\" %(col)] = df[col].dt.day\n", "    \n", "    df[\"payment_plan_days_div-exp-expiration-diff\"] = df.payment_plan_days / df[\"exp-expiration-diff\"]\n", "    df[\"payment_plan_days_div-exp-transaction-diff\"] = df.payment_plan_days / df[\"exp-transaction-diff\"]\n", "    df[\"payment_plan_days_div-eexp-logdate-diff\"] = df.payment_plan_days / df[\"exp-logdate-diff\"]"], "cell_type": "code", "execution_count": null, "metadata": {"_cell_guid": "969a68e8-23d1-4e7d-bfed-6ce467480457", "collapsed": true, "_uuid": "51ebc31aaee3565fd2816dac23ea0ac1e25a3328"}}, {"outputs": [], "source": ["print(train.shape)\n", "get_date_diffs(train)\n", "print(train.shape)"], "cell_type": "code", "execution_count": null, "metadata": {"_cell_guid": "e69512e7-a29e-48ff-87aa-b949c43d17d9", "collapsed": true, "_uuid": "f606c4aafce2a5cafe5b35ace7c5985ef3571be6"}}, {"outputs": [], "source": ["print(test.shape)\n", "get_date_diffs(test)\n", "print(test.shape)"], "cell_type": "code", "execution_count": null, "metadata": {"_cell_guid": "8bf4c462-f074-43a3-9639-ddf8fd6ee55b", "collapsed": true, "_uuid": "05fa1633a6dc40259ac3131ff0058b853c446cc2"}}, {"outputs": [], "source": ["set(train.columns)"], "cell_type": "code", "execution_count": null, "metadata": {"_cell_guid": "12dbe143-77f1-4171-bf89-03c96e37661a", "collapsed": true, "scrolled": true, "_uuid": "b5e4f4bc28fb3d0615998a4b1e13ebbd2b5b13d2"}}, {"source": ["#### Feature: total amount of songs played, vs number of unique songs\n", " * Could add more : e.g. number of 98.5+100 % / 25% played"], "cell_type": "markdown", "metadata": {"_cell_guid": "aef55772-09fa-4d0f-819f-2b0977e1de2e", "_uuid": "f11d672ab5761e314df0a07011ec611aea2e30a6"}}, {"outputs": [], "source": ["train[\"played_songs_nonUnique_ratio\"] = (train['num_100'] + train['num_25'] + train['num_50'] + train['num_75'] + train['num_985'])/train[\"num_unq\"]\n", "test[\"played_songs_nonUnique_ratio\"] = (test['num_100'] + test['num_25'] + test['num_50'] + test['num_75'] + test['num_985'])/test[\"num_unq\"]\n", "\n", "train[\"played_songs_nonUnique_ratio\"].describe()"], "cell_type": "code", "execution_count": null, "metadata": {"_cell_guid": "7d513b35-76c7-4f19-a27d-3bc0d7fad5e9", "collapsed": true, "_uuid": "7230a61bf79f739f8b894cd53e2871d30d5a53d6"}}, {"source": ["### NaN imputing \n", "* try to save as int after!"], "cell_type": "markdown", "metadata": {"_cell_guid": "73c61976-8307-401c-8fb8-e69e9a671145", "_uuid": "71d34665b874e1868e48ebdca7e939e3e451585a"}}, {"outputs": [], "source": ["train.isnull().sum()"], "cell_type": "code", "execution_count": null, "metadata": {"_cell_guid": "67ed7c70-4a76-4d5e-bd5a-58f509ea5a54", "collapsed": true, "_uuid": "d740771569f9518626d7391300a345630c0daf49"}}, {"outputs": [], "source": ["train.columns[~train.isnull().any()].tolist()"], "cell_type": "code", "execution_count": null, "metadata": {"_cell_guid": "7428b40d-8c3e-4de2-80a9-75e1d40d26fe", "collapsed": true, "_uuid": "0e36c6dbcaccb0c02799b314ad2d56cbfd7d3459"}}, {"outputs": [], "source": ["test.isnull().sum()"], "cell_type": "code", "execution_count": null, "metadata": {"_cell_guid": "04c3c691-d423-47aa-9539-81e8e8baa95f", "collapsed": true, "scrolled": true, "_uuid": "0aad0dbdd87b58ced99dae0660e3c2bd64dbad37"}}, {"source": ["### fill na\n", "* Remove nans , downcast to int for better datatype consistency. \n", "* may mess up features!!"], "cell_type": "markdown", "metadata": {"_cell_guid": "294a570e-9ab8-4131-8476-b644d4e83d08", "_uuid": "5cc7eaaa8859016dbc7cb8c4dee9caf59d3ec1ac"}}, {"outputs": [], "source": ["### Ma ymess up date columns or other features.\n", "## Could help with test vals having different types.. \n", "train = train.fillna(0,downcast=\"infer\")\n", "test = test.fillna(0,downcast=\"infer\")"], "cell_type": "code", "execution_count": null, "metadata": {"_cell_guid": "9eb24825-f980-495f-ae2e-1d37426eccbe", "collapsed": true, "_uuid": "f9262d629fb5e1aea034d4f664a23063c6475294"}}, {"outputs": [], "source": ["train[\"price_paid_diff\"]  = train.plan_list_price -  train.actual_amount_paid\n", "test[\"price_paid_diff\"]  = test.plan_list_price -  test.actual_amount_paid"], "cell_type": "code", "execution_count": null, "metadata": {"_cell_guid": "d0ece9eb-a464-4a1d-9b79-5aa0f6c60195", "collapsed": true, "_uuid": "4c9c27c7f8e05045ff10a1fb6a68dfc5a02dfe21"}}, {"outputs": [], "source": ["test.info()"], "cell_type": "code", "execution_count": null, "metadata": {"_cell_guid": "97807265-80cf-4afd-a7e5-afdfa5fa02b5", "collapsed": true, "scrolled": true, "_uuid": "7f721b7745caae7fda2f2536168364a6d7a5c931"}}, {"outputs": [], "source": ["len(set(train.columns) - set(test.columns) )"], "cell_type": "code", "execution_count": null, "metadata": {"_cell_guid": "8cee24f9-4b96-4319-9573-c6ad6cd21bde", "collapsed": true, "_uuid": "3f23d4ce07cfc99af4fe00fc7ce5a9f923381187"}}, {"outputs": [], "source": ["test[\"is_churn\"]= 0"], "cell_type": "code", "execution_count": null, "metadata": {"_cell_guid": "e8ca4cbc-5bc9-4567-9883-605bae7923b9", "collapsed": true, "_uuid": "6287d7c3f3d750dff9a2c5b20948b7ea0144500d"}}, {"source": ["# Adversarial validation: predict if train/test:\n", "\n", "* https://www.kaggle.com/nlothian/adversarial-validation\n", "* http://fastml.com/adversarial-validation-part-two/\n", "    * https://github.com/zygmuntz/adversarial-validation/blob/master/numerai/sort_train.py\n", "    \n", "    \n", "* Currently doesn't work with sklearn - errors with inf/nans (downcasting didn't help, and data doesn't display nans). Likely downcasting related. \n", "\n", "* LAter: Add also isolation forest features! "], "cell_type": "markdown", "metadata": {"_cell_guid": "3a5b644f-7d02-4f77-96da-14070a820f99", "_uuid": "1e26f7612008796093d8e202fc81f17fdcd2cd6c"}}, {"outputs": [], "source": ["train[\"is_test\"] = 0\n", "test[\"is_test\"] = 1"], "cell_type": "code", "execution_count": null, "metadata": {"_cell_guid": "1b0a4325-8dd6-491c-97d5-1aaea76a99a2", "collapsed": true, "scrolled": true, "_uuid": "570760024af936c5f9ccc233adbf407a180e3b3b"}}, {"outputs": [], "source": ["df = train.append(test)\n", "df.drop(\"is_churn\",axis=1,inplace=True)\n", "df.shape"], "cell_type": "code", "execution_count": null, "metadata": {"_cell_guid": "9fb21cc1-16d9-44e7-a95a-278e0d236c46", "collapsed": true, "_uuid": "7ec387048bc1389d4cfef2fdbf41e4990364fa47"}}, {"source": ["#### Get only numeric columns\n", "* note that we drop duplicates and the ID column. \n", "This will be for model training + predicting on the real data only!"], "cell_type": "markdown", "metadata": {"_cell_guid": "40154e37-5e24-446e-85ad-bbe6a56d4250", "_uuid": "dcabbcc859cfa1c5f61d9ddbb612f08b2ed09cde"}}, {"outputs": [], "source": ["df = df.select_dtypes(include=[np.number])\n", "df.reset_index( inplace = True, drop = True )  # may not be needed"], "cell_type": "code", "execution_count": null, "metadata": {"_cell_guid": "1866be4d-66e8-4fc4-a930-89306d278f41", "collapsed": true, "_uuid": "5217914707a48a2b0fe20832b6dc01e3ca6e721a"}}, {"outputs": [], "source": ["df.drop_duplicates(inplace=True)\n", "print(df.shape)\n", "df[\"is_test\"].describe()\n"], "cell_type": "code", "execution_count": null, "metadata": {"_cell_guid": "29d6c9b6-adcd-4713-a560-5a222f228fae", "collapsed": true, "scrolled": true, "_uuid": "5411012f498228fe408a32c6e9b151c49a66686e"}}, {"outputs": [], "source": ["df.isnull().sum(axis=0)"], "cell_type": "code", "execution_count": null, "metadata": {"_cell_guid": "0cf1e080-83aa-4134-8d4e-7445a141c11d", "collapsed": true, "_uuid": "bde5f2c8d20dcddf2b839154ec26fdd1b47540cb"}}, {"outputs": [], "source": [], "cell_type": "code", "execution_count": null, "metadata": {"_cell_guid": "cf70970b-4d25-42eb-9120-b5cbbb5455a9", "collapsed": true, "_uuid": "825a4d97e6324cb3a7504088bb50f615e8b33cac"}}, {"outputs": [], "source": ["# x = df.drop( ['is_test'], axis = 1 ).astype(\"float64\")\n", "# y = df.is_test.astype(int)\n", "\n", "\n", "from sklearn import cross_validation as CV\n", "from sklearn.pipeline import Pipeline\n", "from sklearn.preprocessing import Normalizer, PolynomialFeatures\n", "from sklearn.preprocessing import MaxAbsScaler, MinMaxScaler, RobustScaler, StandardScaler\n", "from sklearn.linear_model import LogisticRegression as LR\n", "from sklearn.ensemble import RandomForestClassifier as RF\n", "from sklearn.metrics import roc_auc_score as AUC\n", "from sklearn.metrics import accuracy_score as accuracy\n", "\n", "from time import ctime\n", "\n", "n_estimators = 100\n", "clf = RF( n_estimators = n_estimators, n_jobs = 1 )\n", "\n", "predictions = np.zeros( y.shape )\n", "\n", "# cv = CV.StratifiedKFold( y, n_folds = 4, shuffle = True, random_state = 5678 )\n", "\n", "# for f, ( train_i, test_i ) in enumerate( cv ):\n", "#     print(f,  train_i, test_i )\n", "#     print (\"# fold {}, {}\".format( f + 1, ctime()))\n", "\n", "#     x_train = x.iloc[train_i]\n", "#     x_test = x.iloc[test_i]\n", "#     y_train = y.iloc[train_i]\n", "#     y_test = y.iloc[test_i]\n", "\n", "#     clf.fit( x_train, y_train )\t\n", "\n", "#     p = clf.predict_proba( x_test )[:,1]\n", "\n", "#     auc = AUC( y_test, p )\n", "#     print (\"# AUC: {:.2%}\\n\".format( auc ))\t\n", "\n", "#     predictions[ test_i ] = p\n"], "cell_type": "code", "execution_count": null, "metadata": {"_cell_guid": "d67c3c91-9246-41d6-b6c8-1e6a4b168553", "collapsed": true, "_uuid": "293ddf175297d7335798af62e0c32e31722ac79b"}}, {"outputs": [], "source": ["# X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=42)\n", "\n", "# lr = LR()\n", "# lr.fit(X_train.values, y_train.values)\n", "# print (classification_report(lr.predict(X_test.values), y_test.values))"], "cell_type": "code", "execution_count": null, "metadata": {"_cell_guid": "26f0e6f5-f5a0-4575-93ff-132b8b3b87f1", "collapsed": true, "_uuid": "3a638b56bfd544bdef7e2b665fab0a2319a19565"}}, {"source": ["## Save data to disk"], "cell_type": "markdown", "metadata": {"_cell_guid": "7e648ff8-7366-48b3-8f6e-ec285ab8d547", "_uuid": "99da6c423a7932e4b9d2e37c3d29b4dd78722eac"}}, {"outputs": [], "source": ["train.to_csv(\"kkbox_churn_v21.csv.gz\",index=False,compression=\"gzip\")"], "cell_type": "code", "execution_count": null, "metadata": {"_cell_guid": "4b0e3c12-d513-4ba4-9377-60d6b37f1f8d", "collapsed": true, "_uuid": "9a0ce45b9e98f9cfc4dc0659238f04f4f3d460f9"}}, {"outputs": [], "source": ["test.to_csv(\"test-kkbox_churn_v21.csv.gz\",index=False,compression=\"gzip\")"], "cell_type": "code", "execution_count": null, "metadata": {"_cell_guid": "60a1cf9d-4827-454e-aa66-55ce2c24ec6a", "collapsed": true, "_uuid": "cd6a581cf0702353c82755e521373c16e6a56705"}}, {"outputs": [], "source": ["\n", "\n", "# cols = [c for c in train.columns if c not in ['is_churn','msno']]"], "cell_type": "code", "execution_count": null, "metadata": {"_cell_guid": "df0fd7c7-dad2-41b9-b144-0ea2d4b9fae4", "collapsed": true, "_uuid": "10a1d624dcb3c7b4a4209aa642f5a837d92a2f09"}}, {"outputs": [], "source": ["# def xgb_score(preds, dtrain):\n", "#     labels = dtrain.get_label()\n", "#     return 'log_loss', metrics.log_loss(labels, preds)\n", "\n", "# fold = 1\n", "# for i in range(fold):\n", "#     params = {\n", "#         'eta': 0.02, #use 0.002\n", "#         'max_depth': 7,\n", "#         'objective': 'binary:logistic',\n", "#         'eval_metric': 'logloss',\n", "#         'seed': i,\n", "#         'silent': True\n", "#     }\n", "#     x1, x2, y1, y2 = model_selection.train_test_split(train[cols], train['is_churn'], test_size=0.3, random_state=i)\n", "#     watchlist = [(xgb.DMatrix(x1, y1), 'train'), (xgb.DMatrix(x2, y2), 'valid')]\n", "#     model = xgb.train(params, xgb.DMatrix(x1, y1), 150,  watchlist, feval=xgb_score, maximize=False, verbose_eval=50, early_stopping_rounds=50) #use 1500\n", "#     if i != 0:\n", "#         pred += model.predict(xgb.DMatrix(test[cols]), ntree_limit=model.best_ntree_limit)\n", "#     else:\n", "#         pred = model.predict(xgb.DMatrix(test[cols]), ntree_limit=model.best_ntree_limit)\n", "# pred /= fold\n", "# test['is_churn'] = pred.clip(0.0000001, 0.999999)\n", "# test[['msno','is_churn']].to_csv('submission3.csv.gz', index=False, compression='gzip')"], "cell_type": "code", "execution_count": null, "metadata": {"_cell_guid": "00d437d9-df05-4f0c-a947-e2ca6cf0bac2", "collapsed": true, "_uuid": "cb258ccda2afac4d0e8ed0285da798c3808a5920"}}, {"outputs": [], "source": ["# import matplotlib.pyplot as plt\n", "# import seaborn as sns\n", "# %matplotlib inline\n", "\n", "# plt.rcParams['figure.figsize'] = (7.0, 7.0)\n", "# xgb.plot_importance(booster=model); plt.show()"], "cell_type": "code", "execution_count": null, "metadata": {"_cell_guid": "815bd7e8-9de2-40c1-af0f-16d6554387e5", "collapsed": true, "_uuid": "5fc20e3598d1dfbb3bd9bb006af1fc6518cba63e"}}], "nbformat": 4, "nbformat_minor": 1}