{"nbformat": 4, "metadata": {"kernelspec": {"name": "python3", "language": "python", "display_name": "Python 3"}, "language_info": {"name": "python", "codemirror_mode": {"version": 3, "name": "ipython"}, "mimetype": "text/x-python", "pygments_lexer": "ipython3", "version": "3.6.3", "file_extension": ".py", "nbconvert_exporter": "python"}}, "cells": [{"execution_count": null, "cell_type": "code", "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."], "metadata": {"_cell_guid": "d9cbf4bf-dd3e-49c9-94f1-acf16708481e", "_uuid": "a8828c8072602e889174a8f08da7082f6068372e"}, "outputs": []}, {"execution_count": null, "cell_type": "code", "source": ["import seaborn as sb\n", "import matplotlib.pyplot as plt\n", "import sklearn\n", "\n", "from pandas import Series, DataFrame\n", "from pylab import rcParams\n", "from sklearn import preprocessing\n", "from sklearn.linear_model import LogisticRegression\n", "from sklearn.cross_validation import train_test_split\n", "from sklearn import metrics \n", "from sklearn.metrics import classification_report\n", "\n", "%matplotlib inline\n", "rcParams['figure.figsize'] = 10, 8\n", "sb.set_style('whitegrid')"], "metadata": {"_cell_guid": "3fbc80eb-d806-4230-a169-4b5dd7340e92", "_uuid": "89225581d17c09b402a4adb58a27e692d5c1eddd"}, "outputs": []}, {"execution_count": null, "cell_type": "code", "source": ["train = pd.read_csv('../input/train.csv')\n", "members = pd.read_csv('../input/members_v2.csv')\n", "\n", "print(train.shape)\n", "print(members.shape)"], "metadata": {"_cell_guid": "e2ec8fe1-a545-4b27-8439-d4783723f233", "_uuid": "3ad6980b684d30893de024cd009b127ee3575117"}, "outputs": []}, {"execution_count": null, "cell_type": "code", "source": ["Data = pd.merge(train,members,on='msno', how='inner')\n", "print(Data.shape)"], "metadata": {"_cell_guid": "34561b8d-be60-44ce-a20f-272ad5f8336e", "_uuid": "87d13a7a18226ccd5c08372e5bae84b424ab7e94"}, "outputs": []}, {"execution_count": null, "cell_type": "code", "source": ["sb.countplot(x='is_churn',data=Data, palette='hls')"], "metadata": {"_cell_guid": "d9e48c97-2fee-47d7-a834-d5282957255b", "_uuid": "09cb7fc7fb4f069ee35d5344c0ff44f506fa4aa0"}, "outputs": []}, {"execution_count": null, "cell_type": "code", "source": ["Data.isnull().sum()"], "metadata": {"_cell_guid": "7689223b-adc6-49b3-9652-1f69fd2abd59", "_uuid": "49747560aee7a7d151c1bbc87b9e0300dc145a0b"}, "outputs": []}, {"execution_count": null, "cell_type": "code", "source": ["Data.info()"], "metadata": {"_cell_guid": "b784dcfe-8db0-4f7c-aca2-ac7022704000", "_uuid": "96dfa4206a43a8f8cdc361f7623f69309ba38991"}, "outputs": []}, {"execution_count": null, "cell_type": "code", "source": ["Data1 = Data.drop(['gender'],1)\n", "#Data1 = Data.replace(r'\\s+', np.nan, regex=True)"], "metadata": {"_cell_guid": "9672f0bc-514c-479d-9cfa-b690988dc21e", "_uuid": "31ed5eefc8b915ea649e005a8d455f85e2be1685", "collapsed": true}, "outputs": []}, {"execution_count": null, "cell_type": "code", "source": ["print(Data1)"], "metadata": {"_cell_guid": "596a0551-27c8-46bb-ada3-cef7623a15c7", "_uuid": "a0cb1e644a67d68a9f9c45c408874ad275b57481"}, "outputs": []}, {"execution_count": null, "cell_type": "code", "source": ["#Data1.dropna(inplace=True)\n", "Data1.isnull().sum()"], "metadata": {"_cell_guid": "850d0551-3987-4ce6-9afb-cd99e95deced", "_uuid": "abd17eaea4a80132576f65640d42dab10be129c3"}, "outputs": []}, {"execution_count": null, "cell_type": "code", "source": ["print(Data1)"], "metadata": {}, "outputs": []}, {"execution_count": null, "cell_type": "code", "source": ["sb.heatmap(Data1.corr())"], "metadata": {"_cell_guid": "1b3549ea-48aa-4ef2-9556-d59241a66c0f", "_uuid": "4f31e00886d9616b2d009e369b0516c54189f2e3"}, "outputs": []}, {"execution_count": null, "cell_type": "code", "source": ["#Data2 = Data1.drop(['msno'],1)\n", "Data2 = Data1.msno\n", "print(Data2)"], "metadata": {"_cell_guid": "54f6ab0a-763d-4f73-b233-6802ff74eee3", "_uuid": "6889c2b891a714e9c2869f0a1f8b7fb3b72b8400"}, "outputs": []}, {"execution_count": null, "cell_type": "code", "source": ["Data3 = pd.DataFrame(Data2)\n", "ID = []\n", "for i in range(1,len(Data3)+1):\n", "    #print(i)\n", "    ID.append(i)\n", "#print(len(Data3))"], "metadata": {"_cell_guid": "0bfa7db6-2b67-4b5d-b9f6-9c3b998a35fb", "_uuid": "71882be578b7a2237314c2f47ec8aecc13d7db56", "collapsed": true}, "outputs": []}, {"execution_count": null, "cell_type": "code", "source": ["Data4 = pd.DataFrame(ID)\n", "Data4.columns = ['ID']\n", "print(Data4)"], "metadata": {"_cell_guid": "2b99a3d7-7ab5-416b-a3b0-c8c7627a26ab", "_uuid": "98e189d1e614e420e82c4379605c1f0aa66d07bc"}, "outputs": []}, {"execution_count": null, "cell_type": "code", "source": ["IdData = pd.concat([Data3,Data4],axis=1)"], "metadata": {"_cell_guid": "e2a69f51-2e14-44ca-bada-3b3e216f9768", "_uuid": "459196719266a6ec5d52f1da066dd7964364a184", "collapsed": true}, "outputs": []}, {"execution_count": null, "cell_type": "code", "source": ["print(IdData)"], "metadata": {"_cell_guid": "ac93dbea-bd8e-4d32-b674-1276761aa05e", "_uuid": "3c70e8b99bff046b3cda0101d9df24a8a0d6ae15"}, "outputs": []}, {"execution_count": null, "cell_type": "code", "source": ["FinalData = pd.merge(Data1,IdData, on='msno',how='inner')"], "metadata": {"_cell_guid": "dac07899-4ef7-4756-9563-f92b23af5b55", "_uuid": "9afd8322bf3b3ca8798b496affb83d3e8bbc196c", "collapsed": true}, "outputs": []}, {"execution_count": null, "cell_type": "code", "source": ["print(FinalData)"], "metadata": {"_cell_guid": "abe66d42-4717-4dd5-90e5-901113274ead", "_uuid": "4e9fd08c212be2d6ef23f34414e546e34e0e5672"}, "outputs": []}, {"execution_count": null, "cell_type": "code", "source": ["X = FinalData.ix[:,(2,3,4,5,6)].values\n", "y = FinalData.ix[:,1].values"], "metadata": {"_cell_guid": "2159adcd-b410-4585-b955-f733b55d9224", "_uuid": "1ace6bb3b972d38ef7f199692ec120a30e3125d1"}, "outputs": []}, {"execution_count": null, "cell_type": "code", "source": ["print(X)"], "metadata": {"_cell_guid": "015c9db7-950a-4b0f-9304-aa95e8d120f9", "_uuid": "7d86c5c77bdd51a1cc06107b4425d168c8b23c90"}, "outputs": []}, {"execution_count": null, "cell_type": "code", "source": ["print(y)"], "metadata": {"_cell_guid": "76cb63e6-c1ba-4c51-bba1-edf7def29bf8", "_uuid": "0e821fc76802cbc03d161dc9c780743cd1175f84"}, "outputs": []}, {"execution_count": null, "cell_type": "code", "source": ["X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = .3, random_state=25)"], "metadata": {"_cell_guid": "349ce5ae-8d00-4833-ac8e-ed8b1aeb9993", "_uuid": "d668785f8f4685e1e02938a25a506ebdf8ff57da", "collapsed": true}, "outputs": []}, {"execution_count": null, "cell_type": "code", "source": ["print(len(X_train))\n", "print(len(X_test))"], "metadata": {"_cell_guid": "7a15a68f-175a-4db4-b7ec-d286ae7c9126", "_uuid": "2370cecedb81438fc9801e9c9f95751c1e812184"}, "outputs": []}, {"execution_count": null, "cell_type": "code", "source": ["print(len(y_train))\n", "print(len(y_test))"], "metadata": {"_cell_guid": "82f6e914-b7ab-4eb9-8b24-64b8bc3a0daa", "_uuid": "b32517a2edf488cf5f30e7fb0aa6186702bf8769"}, "outputs": []}, {"execution_count": null, "cell_type": "code", "source": ["from sklearn import tree\n", "clf = tree.DecisionTreeClassifier()"], "metadata": {"_cell_guid": "a2fd878b-c07b-4009-9981-7e08a36acf34", "_uuid": "454c39012305c1a2214d9c4a788b15f235f47d47", "collapsed": true}, "outputs": []}, {"execution_count": null, "cell_type": "code", "source": ["clf.fit(X,y)"], "metadata": {"_cell_guid": "2fc86ee2-7845-466f-afbc-bc50b300e50e", "_uuid": "3c13500e2504671c73fdca55cd4b8c9ccb02971f"}, "outputs": []}, {"execution_count": null, "cell_type": "code", "source": ["results = clf.predict(X_test)\n", "print(results[0:1000])"], "metadata": {"_cell_guid": "b71f62e9-a8aa-4382-8965-de9cabe8fdf6", "_uuid": "d1ab238ca9bd64b75bf97fed3db1c5fdb2929b26"}, "outputs": []}, {"execution_count": null, "cell_type": "code", "source": ["from sklearn.metrics import confusion_matrix\n", "confusion_matrix = confusion_matrix(y_test, results)\n", "confusion_matrix"], "metadata": {"_cell_guid": "3ea0fc9a-8d07-4ae8-a59d-59bf5f59db49", "_uuid": "c6939edecbf3efb7fa09396c9a390865c6ac7c89"}, "outputs": []}, {"execution_count": null, "cell_type": "code", "source": ["from sklearn.metrics import mean_squared_error\n", "mean_squared_error(results, y_test)"], "metadata": {"_cell_guid": "ca7496f2-0aca-46e3-a0a5-8175593e82ad", "_uuid": "ea256001c27be6ff257e51755902c7e9292d0291"}, "outputs": []}, {"execution_count": null, "cell_type": "code", "source": ["#from sklearn.ensemble import RandomForestRegressor\n", "#model = RandomForestRegressor(n_estimators=100, min_samples_leaf=10, random_state=1)"], "metadata": {"_cell_guid": "8483aadd-b95a-470f-bd4e-5606562fcb60", "_uuid": "4217d4ff539f0d728a87379e5ebffc0354b779e9", "collapsed": true}, "outputs": []}, {"execution_count": null, "cell_type": "code", "source": ["#model.fit(X,y)"], "metadata": {"_cell_guid": "b70e9021-3f5a-44be-836f-0947aaacc17e", "_uuid": "8b14204c327efe38b6e9736a9b4d20b51e4c2dd6", "collapsed": true}, "outputs": []}, {"execution_count": null, "cell_type": "code", "source": ["#predictions = model.predict(X_test)"], "metadata": {"_cell_guid": "670795f9-64fa-4c6d-9fe4-cd971e117fdf", "_uuid": "c938abb60df749ac98fcc8b05dc0427402944cc7", "collapsed": true}, "outputs": []}, {"execution_count": null, "cell_type": "code", "source": ["#print(predictions[1:1000])"], "metadata": {"_cell_guid": "1700f2e5-3711-41b3-993b-e093b1b08bd9", "_uuid": "faa5fcf21405682899e4473a48e78b29f34c7337", "collapsed": true}, "outputs": []}, {"execution_count": null, "cell_type": "code", "source": ["#mean_squared_error(predictions, y_test)"], "metadata": {"_cell_guid": "959c64bb-f639-472c-9a99-fb87cfab76a8", "_uuid": "57eb21e4f4f54228f0196afe2ca3f06251c8d5c2", "collapsed": true}, "outputs": []}, {"execution_count": null, "cell_type": "code", "source": ["Submit = FinalData.ix[:,(2,3,46)].values"], "metadata": {"_cell_guid": "33f67a58-8b37-4c95-98e5-df9c6c712e00", "_uuid": "0c8ab49288b06a714051c85e2136013d28522da9", "collapsed": true}, "outputs": []}, {"execution_count": null, "cell_type": "code", "source": ["Results = clf.predict(Submit)"], "metadata": {"_cell_guid": "7acd81ba-d409-40f4-a781-5a629f18e7c8", "_uuid": "ccce7c29b6f99379ec9be69b6cd06460d180fca1", "collapsed": true}, "outputs": []}, {"execution_count": null, "cell_type": "code", "source": ["submission = pd.DataFrame(Results)\n", "submission.columns = ['is_Churn_Prediction']\n", "print(submission)"], "metadata": {"_cell_guid": "438c407f-4c1a-4a33-bc60-e6fb66be15f5", "_uuid": "84123c070f2a4832013ffcd3085a5c07d20e2fa7", "collapsed": true}, "outputs": []}, {"execution_count": null, "cell_type": "code", "source": ["Final_Submission = pd.concat([FinalData,submission],axis=1)\n", "print(Final_Submission)"], "metadata": {"_cell_guid": "59cc2376-7113-4404-864c-0e38d49e42f9", "_uuid": "29f0ebdbc4b4aa7020485bd3d1ab592b07c1ca9e", "collapsed": true}, "outputs": []}, {"execution_count": null, "cell_type": "code", "source": ["Final_Submission.to_csv('D:/Kaggle/data/churn_comp_refresh/submission.csv',index=False)"], "metadata": {"_cell_guid": "93222c03-fad3-4f54-9fa3-7ea714edbb8c", "_uuid": "2bb79790884ca49c5fc95f04cd0415c6a9c9b2b3", "collapsed": true}, "outputs": []}], "nbformat_minor": 1}