{"cells":[{"metadata":{"_cell_guid":"cc1c811f-ca8d-4e5e-a413-049ad73d980a","_kg_hide-input":true,"_uuid":"ae60886058b67825c6e672b010fc55e6d1857448","collapsed":true,"trusted":true},"cell_type":"code","source":"import pandas as pd\nimport numpy as np\ntrain=pd.read_csv('../input/train_sample.csv', parse_dates=True )\n#train=pd.read_csv(\"/Users/johnluo/Desktop/Python/Dataset/train_sample.csv\")\nprint (train.dtypes)\nnb_attributed=len(train.loc[train['is_attributed']==1])\nprint (nb_attributed)\npercent=nb_attributed/len(train)*100\nprint (\"percentage : \"+ str(percent)+\"%\")\n","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"c1a57dae-330c-4ce2-8fa8-d8f34a10cfb9","_uuid":"e3f402d5b62900630e80f0882054a9eeec937752","collapsed":true,"trusted":true},"cell_type":"code","source":"#http://strftime.org/\n\nfrom datetime import datetime\n\ndate=[]\ntime=[]\nfor i in train[\"click_time\"]:\n    a=datetime.strptime(i,'%Y-%m-%d %H:%M:%S').date()\n    b=datetime.strptime(i,'%Y-%m-%d %H:%M:%S').time()\n    date.append(a)\n    time.append(b)\n\ndate=pd.DataFrame(date,columns=[\"run_date\"])\ntime=pd.DataFrame(time,columns=[\"time\"])\ntrain2=pd.concat([train,date,time],axis=1)\ntrain2[\"click\"]=1","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"88f5825e-7dd5-4f4b-b5e3-8c627ba26198","_uuid":"6f0b5c28178748717bdfa588243df1ff32d9e605","collapsed":true,"trusted":true},"cell_type":"code","source":"train2.head()","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"54294dcd-fb61-4ae4-ac1e-ebadbd6fa1b0","_uuid":"66ec2dbcf5dad81e9a95ca4aa6733738bc696da6","collapsed":true,"trusted":true},"cell_type":"code","source":"# sample file contains 4 days data 6Nov-9Nov\ntrain2.head()\n\nprint(train2.run_date.unique())","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"03a9a569-ec65-4277-aa6b-84dd48a8ae10","_uuid":"fbad5763db957b162233e0ac18f6eb392bc0531a"},"cell_type":"markdown","source":"Only .2% of clicks contributed "},{"metadata":{"_cell_guid":"70b7aafd-877d-4d17-a60e-82f5ff664f5b","_uuid":"a597602db874910ec1192c50d63112d0b40201c4","collapsed":true,"trusted":true},"cell_type":"code","source":"train2.describe()","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"e5a6d9cd-adfd-4016-907b-d40c27f35da9","_uuid":"faf2473c79bfa39de566e046bcab2709ca39d048"},"cell_type":"markdown","source":"Out of 100,000 clicks, 34,857 unique IP, 130 differnt Operation system"},{"metadata":{"_cell_guid":"e5d29040-25e3-4af6-b0dc-f57f9b06b359","_uuid":"e4463ae25b7b495e4ee4ab4d586b0becf5e3868a","collapsed":true,"trusted":true},"cell_type":"code","source":"print (\"Total \"+str(len(train2.ip.unique()))+\" clicks\")\nprint(\"Total \"+str(len(train2.os.unique()))+\" type of OSs\")\nprint(\"Total \"+str(len(train2.device.unique()))+\" of devices\")","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"0638b4ee-07e5-4383-a150-ae3e29c6d150","_uuid":"71538629a92acb9219bed41094cb0a5e30e8b331","collapsed":true,"trusted":true},"cell_type":"code","source":"#data.pivot_table(index=\"IP\", values())\n\n","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"427f1b93-985c-46f8-b0c7-a9403df81226","_uuid":"72be7079c34f22400319ae4ed3f6eca2784278df"},"cell_type":"markdown","source":"#### Data Visualisation"},{"metadata":{"_cell_guid":"7c915efc-c2eb-4e6d-bdec-5405e739b48b","_uuid":"8df485b63182c375136f584f56daaed87535c892","collapsed":true,"trusted":true},"cell_type":"code","source":"import matplotlib.pyplot as plt\n%matplotlib inline\n\ndays=list(train2.run_date.unique())\ndays.sort()\nfig = plt.figure(figsize=(25,30),dpi=50)\n#char1 = fig.add_subplot(4,1,1)\n\nfor day in range(0,4):\n    ax = fig.add_subplot(4,1,day+1)\n    click_day=train2[train2.run_date==days[day]].pivot_table(index=\"time\", values=\"click\",aggfunc=np.sum)\n    ax.scatter(click_day.index, click_day.click,color=\"blue\", label=days[day])\n    ax.legend(loc=\"upper left\")\n    #plt.set_xticks()\n\nplt.show()\n","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"5d918282-8e33-49c3-9178-c4956c472bc7","_uuid":"30eb807728123cc4352eb14c3e188725456c166c","collapsed":true,"scrolled":false,"trusted":true},"cell_type":"code","source":"from sklearn import linear_model\nfrom sklearn import cross_validation\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\nfrom matplotlib import pyplot as plt\nfrom pandas.tools.plotting import scatter_matrix\nfrom sklearn.linear_model import LinearRegression\nfrom sklearn.cross_validation import train_test_split\nimport collections\n\ntrain=pd.read_csv('../input/train_sample.csv', parse_dates=True )\n\ncols_drop=['is_attributed','click_time','attributed_time']\n\nlist_attributed=train.loc[train['is_attributed']==1].drop(cols_drop,1)\n\ntrain_test=list_attributed\n\ncol_dic_total={}\n\nfor col in train_test:\n    col_dic_total[col]=train_test[col]\n    \ncol_dic_attributed={}\n\nfor col in list_attributed:\n    col_dic_attributed[col]=list_attributed[col].unique()\n    \n#vote for the 5 features\n#{col,{col_value,col_frequency}}\nfor key,value in col_dic_total.items():\n    print (key)\n    print (len(value))\n    counter=collections.Counter(value)\n    print(counter.most_common(3))\n\n\n\n#create a python list of feature names  \nfeature_cols = ['ip', 'app', 'device','os','channel']  \n","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"b12b20ee-ee0b-43be-81e7-55b6f968f3cb","_uuid":"774ab1993b02a510511bea3d8dcc77637e3f3405","collapsed":true,"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"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.6.4"}},"nbformat":4,"nbformat_minor":1}