{"nbformat": 4, "cells": [{"cell_type": "code", "source": ["#import required packages\n", "import pandas as pd\n", "import matplotlib.pyplot as plt#load data sets\n", "\n", "train = pd.read_csv('../input/train.csv')\n", "transactions = pd.read_csv('../input/transactions.csv')"], "outputs": [], "metadata": {"_cell_guid": "d8589253-6d4c-4685-9847-babdb1d22247", "collapsed": true, "_uuid": "b8d2b11326714aadd5f9d01e7609aaf2dd32ba2a"}, "execution_count": null}, {"cell_type": "code", "source": ["train.sample(5)  \n", "train.info()"], "outputs": [], "metadata": {"_cell_guid": "69097edd-75d3-4046-8f12-948404a213e4", "_uuid": "4e4a32d6a22c76913713250e61b5ed6d0e173da4"}, "execution_count": null}, {"cell_type": "code", "source": ["#change is_churn to categorical variable\n", "train['is_churn'] = train['is_churn'].astype('category')"], "outputs": [], "metadata": {"_cell_guid": "c84fbbf0-2dc2-48aa-9e57-868c1c28cddb", "collapsed": true, "_uuid": "cee9c02b9a007ad21c6b5356ce0ec22738ec4aa6"}, "execution_count": null}, {"cell_type": "code", "source": ["# take a look is_churn\n", "train['is_churn'].value_counts().plot(kind='bar')\n", "# As we can see, most members are renewal their memberships"], "outputs": [], "metadata": {"_cell_guid": "dfb67e30-07a5-4d84-b2fd-53e079e61b2f", "_uuid": "984b71b8437b92ebdbed5a27197c50c67a014901"}, "execution_count": null}, {"cell_type": "code", "source": ["#cross table to see exact number of is_churn\n", "ischurn_tab = pd.crosstab(index=train['is_churn'],  # Make a crosstab\n", "                              columns='Count')      # Name the count column\n", "ischurn_tab"], "outputs": [], "metadata": {"_cell_guid": "47f4a7d6-92fa-4c68-8a51-ba60b7f4801d", "_uuid": "368ad0dc1331acf1892f6f5064ba3d3c6e51f1c9"}, "execution_count": null}, {"cell_type": "code", "source": ["#Look at transactions data\n", "transactions.sample(10)"], "outputs": [], "metadata": {"_cell_guid": "321ed5dd-6c95-46b7-aa05-ef597e365060", "_uuid": "68db9b2865924636baa5cdf8a75dde62d3cd719f"}, "execution_count": null}, {"cell_type": "code", "source": ["transactions.info()\n", "# 9 columns total 21,547,746 records"], "outputs": [], "metadata": {"_cell_guid": "728d284b-e23c-490d-b3d6-899f90901447", "_uuid": "b79797112b1bb1fd337bb5bd8ed2cfd93150f1e9"}, "execution_count": null}, {"cell_type": "code", "source": ["#examine msno\n", "msno_distinct_id = transactions.drop_duplicates().msno.value_counts()"], "outputs": [], "metadata": {"_cell_guid": "04de35c0-338d-48ef-8ac2-dd6e19b0e132", "collapsed": true, "_uuid": "4ca1e574d1fb3c659269f9aaeed3e9bb8945d106"}, "execution_count": null}, {"cell_type": "code", "source": ["msno_distinct_id \n", "# As you can see, some user made more than one transaction"], "outputs": [], "metadata": {"_cell_guid": "c1ad6728-ef10-452b-a32c-1afeeac64dae", "_uuid": "9507db8db7a93de8cdf4cd32e2acef7851c615e9"}, "execution_count": null}, {"cell_type": "code", "source": ["#merge two data sets \n", "train_transactions = pd.merge(train, transactions, on='msno')"], "outputs": [], "metadata": {"_cell_guid": "4a1f369e-5ec2-4b4c-adce-d651ef3fb650", "collapsed": true, "_uuid": "6349119447a3adc2f7cf3d8f102003ec2b009b4a"}, "execution_count": null}, {"cell_type": "code", "source": ["train_transactions.sample(20)"], "outputs": [], "metadata": {"_cell_guid": "420cb1a5-3c73-4ab9-a0ae-7e6c1758820d", "_uuid": "844f1409f5855264e93df8556ff5d7d2ed89439d"}, "execution_count": null}, {"cell_type": "code", "source": ["#change is_auto_renew and is_cancel to categorical variables\n", "transactions['is_auto_renew'] = transactions['is_auto_renew'].astype('category')\n", "transactions['is_cancel'] = transactions['is_cancel'].astype('category')"], "outputs": [], "metadata": {"_cell_guid": "d2c6802e-7030-44a8-b692-0b9c122fd4bf", "collapsed": true, "_uuid": "bdbefb548cfc5b7d414dc3b78e4fabcd0c978cdb"}, "execution_count": null}, {"cell_type": "code", "source": ["# Table of is_churn vs is_auto_renew\n", "ischurn_isautorenew = pd.crosstab(index=train_transactions['is_auto_renew'],  \n", "                              columns=train_transactions['is_churn'],\n", "                              margins=True)      \n", "ischurn_isautorenew.columns = ['No','Yes', 'isChurnTotal']\n", "ischurn_isautorenew.index = ['NoAutoRenew','YesAutoRenew', 'Total']\n", "ischurn_isautorenew\n"], "outputs": [], "metadata": {"_cell_guid": "faa2e442-e2b6-4614-925b-0cb2daa24d94", "_uuid": "b907529ebc24d87c02bb1ee8f6aa6dba646cc710"}, "execution_count": null}, {"cell_type": "code", "source": ["ischurn_iscancel = pd.crosstab(index=train_transactions['is_cancel'],  \n", "                              columns=train_transactions['is_churn'],\n", "                              margins=True)    \n", "ischurn_iscancel.index = ['NoCancel','YesCancel', 'Total']\n", "ischurn_iscancel\n"], "outputs": [], "metadata": {"_cell_guid": "840bf586-8bc3-467f-8c05-6c1efdd604ed", "_uuid": "12ae13aaa1672d2f5cdd33c5649165463215156a"}, "execution_count": null}], "nbformat_minor": 1, "metadata": {"kernelspec": {"display_name": "Python 3", "language": "python", "name": "python3"}, "language_info": {"nbconvert_exporter": "python", "codemirror_mode": {"version": 3, "name": "ipython"}, "pygments_lexer": "ipython3", "mimetype": "text/x-python", "file_extension": ".py", "version": "3.6.1", "name": "python"}}}