{"cells":[{"metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","collapsed":true,"trusted":true},"cell_type":"code","source":"import matplotlib.pyplot as plt\n%matplotlib inline\nimport numpy as np \nimport pandas as pd\nimport seaborn as sns\nimport gc","execution_count":31,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","scrolled":false,"trusted":true,"collapsed":true},"cell_type":"code","source":"train_col = ['ip', 'app', 'device', 'os', 'channel', 'click_time', 'is_attributed']\ndtypes = {\n        'ip'            : 'uint32',\n        'app'           : 'uint16',\n        'device'        : 'uint16',\n        'os'            : 'uint16',\n        'channel'       : 'uint16',\n        'is_attributed' : 'uint8',\n        'click_id'      : 'uint32'\n        }\ndf = pd.read_csv('../input/train.csv', nrows=30000000,\n                 usecols=train_col, dtype=dtypes)\ndf.head()","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"f4ad0c6a-d79d-4f0b-8197-96540b23854a","_uuid":"075dc3f45b673af576b9ad1df217b510789ea011","trusted":true,"collapsed":true},"cell_type":"code","source":"df.describe()","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"4fad152e-83e0-42ac-ac33-8e7cebdcdb6f","_uuid":"32f75c28cd57b6f10f27e6b2e308523c1a201e42","collapsed":true,"trusted":true},"cell_type":"code","source":"df['hour'] = pd.to_datetime(df.click_time).dt.hour.astype('uint8')\ndf['minute'] = pd.to_datetime(df.click_time).dt.minute.astype('uint8')\ndf['day'] = pd.to_datetime(df.click_time).dt.day.astype('uint8')\ndf['dw'] = pd.to_datetime(df.click_time).dt.dayofweek.astype('uint8')\n\ndf_test = pd.read_csv('../input/test.csv')\nclick_id = df_test['click_id']\ndf_test['hour'] = pd.to_datetime(df_test.click_time).dt.hour.astype('uint8')\ndf_test['minute'] = pd.to_datetime(df_test.click_time).dt.minute.astype('uint8')\ndf_test['day'] = pd.to_datetime(df_test.click_time).dt.day.astype('uint8')\ndf_test['dw'] = pd.to_datetime(df_test.click_time).dt.dayofweek.astype('uint8')","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"4dfb2e38-4e72-4222-ad10-e56a95a21f7b","_uuid":"d80b060f8edba80c89f6ae94a8c78768bee9dd23","trusted":true,"collapsed":true},"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\n\nX = pd.concat((df[['ip', 'app', 'device', 'hour', 'minute', 'os', 'channel', 'day', 'dw']],\n               df_test[['ip', 'app', 'device', 'hour', 'minute', 'os', 'channel', 'day', 'dw']]))\ndel df_test; gc.collect()\n\ngroup = X[['ip','day','hour', 'minute','channel']].groupby(by=['ip','day','hour', 'minute'])['channel'].count().\\\nreset_index().rename(index=str, columns={'channel': 'ip_day_time'})\nX = X.merge(group, on=['ip','day','hour', 'minute'], how='left')\ndel group; gc.collect()\n\ngroup = X[['ip', 'app', 'os', 'channel']].groupby(by=['ip', 'app', 'os'])['channel'].count().\\\nreset_index().rename(index=str, columns={'channel': 'ip_app_os'})\nX = X.merge(group, on=['ip', 'app', 'os'], how='left')\ndel group; gc.collect()\n\n\nX[['app','device','os', 'channel', 'hour', 'minute', 'day', 'dw']].apply(LabelEncoder().fit_transform)\n\nlen(X)","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"a5ad5400-9dee-4ec5-bbe3-d3d469e2c628","_uuid":"38c19bdca78472289442138f4fe9914bf857c528","collapsed":true,"trusted":true},"cell_type":"code","source":"max_app = np.max(X.app)+1\nmax_device = np.max(X.device)+1\nmax_hour = np.max(X.hour)+1\nmax_minute = np.max(X.minute)+1\nmax_os = np.max(X.os)+1\nmax_channel = np.max(X.channel)+1\nmax_day = np.max(X.day)+1\nmax_dw = np.max(X.dw)+1\nmax_ip_day_time = np.max(X.ip_day_time)+1\nmax_ip_app_os = np.max(X.ip_app_os)+1","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"74ece0a0-a78d-4216-9038-f69e6f661539","_uuid":"3bd9c52ac43bae37a331eafbbd817e089b03dcf3","trusted":true,"collapsed":true},"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX_test = X[len(df):]\nX = X[:len(df)]\ny = df.is_attributed\nX_train, X_valid, y_train, y_valid = train_test_split(X, y, random_state=42, train_size=0.95)","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"30055d20-b62f-490b-8367-368d1f2003bb","_uuid":"b5817deb07d65d8ece619dd6701a32e448bb2ed9","trusted":true},"cell_type":"code","source":"print(np.sum(y_train) / len(y_train))\nprint(np.sum(y_valid) / len(y_valid))","execution_count":8,"outputs":[]},{"metadata":{"_cell_guid":"a4591bdf-6614-4881-95a2-1e0dc0342ae6","_uuid":"e827415c0d048e407dbb660791093d8c92808833","collapsed":true,"trusted":true},"cell_type":"code","source":"#from keras.preprocessing.sequence import pad_sequences\n\ndel df; gc.collect()\ndef get_keras_data(data):\n    X = {\n        'app': np.array(data.app),\n        'device': np.array(data.device),\n        'hour': np.array(data.hour),\n        'minute': np.array(data.minute),\n        'os': np.array(data.os),\n        'channel': np.array(data.channel),\n        'day': np.array(data.day),\n        'dw': np.array(data.dw),\n        'ip_day_time': np.array(data.ip_day_time),\n        'ip_app_os': np.array(data.ip_app_os)\n    }\n    return X\nX_train = get_keras_data(X_train)\nX_valid = get_keras_data(X_valid)\nX_test = get_keras_data(X_test)","execution_count":9,"outputs":[]},{"metadata":{"_cell_guid":"d69896cf-db1d-44a9-a8dc-e04f35201d04","_uuid":"1dd583b31d1704655a3eea98b95b85d5dd7bbd81","trusted":true},"cell_type":"code","source":"from keras.layers import Dense, Input, Embedding, Dropout, concatenate, Flatten, GRU\nfrom keras.callbacks import Callback\nfrom keras.optimizers import Adam\nfrom keras.models import Model\n\ndef get_model():\n    \n    app = Input(shape=[1], name='app')\n    device = Input(shape=[1], name='device')\n    hour = Input(shape=[1], name='hour')\n    minute = Input(shape=[1], name='minute')\n    os = Input(shape=[1], name='os')\n    channel = Input(shape=[1], name='channel')\n    day = Input(shape=[1], name='day')\n    dw = Input(shape=[1], name='dw')\n    ip_day_time = Input(shape=[1], name='ip_day_time')\n    ip_app_os = Input(shape=[1], name='ip_app_os')\n        \n    emb_app = Embedding(max_app, 30)(app)\n    emb_device = Embedding(max_device, 30)(device)\n    emb_hour = Embedding(max_hour, 30)(hour)\n    emb_minute = Embedding(max_minute, 30)(minute)\n    emb_os = Embedding(max_os, 30)(os)\n    emb_channel = Embedding(max_channel, 30)(channel)\n    emb_day = Embedding(max_day, 30)(day)\n    emb_dw = Embedding(max_dw, 30)(dw)\n    emb_ip_day_time = Embedding(max_ip_day_time, 30)(ip_day_time)\n    emb_ip_app_os = Embedding(max_ip_app_os, 30)(ip_app_os)\n    \n    main_l = concatenate([Flatten()(emb_app), Flatten()(emb_hour), Flatten()(emb_minute),\n                          Flatten()(emb_os), Flatten()(emb_device),\n                          Flatten()(emb_day), Flatten()(emb_dw), Flatten()(emb_channel),\n                          Flatten()(emb_ip_day_time), Flatten()(emb_ip_app_os)])\n    main_l = Dropout(0.1)(Dense(1000, activation='relu')(main_l))\n    main_l = Dropout(0.1)(Dense(1000, activation='relu')(main_l))\n    \n    output = Dense(1, activation='sigmoid')(main_l)\n    \n    model = Model([app, device, hour, minute, os, channel, day, dw, ip_day_time, ip_app_os], output)\n    model.compile(loss='binary_crossentropy', metrics=['accuracy'], optimizer='adam')\n    \n    return model\n\nmodel = get_model()\nmodel.summary()\n    ","execution_count":10,"outputs":[]},{"metadata":{"_cell_guid":"e7634ec7-6a9b-4a91-bd31-e2f5bac5af58","_uuid":"fabb8ceeedb9a91db9c24e60b0e38a0b14d8f01a","scrolled":false,"trusted":true},"cell_type":"code","source":"batch_size = 20000\nepochs = 1\n\nmodel.fit(X_train, np.array(y_train), epochs=epochs, batch_size=batch_size, verbose=1)","execution_count":11,"outputs":[]},{"metadata":{"_cell_guid":"e5853bd4-3393-4324-8d3c-3657207a57df","_uuid":"5aa20fb3cbc877b64876838942510c16faab1181","collapsed":true,"trusted":true},"cell_type":"code","source":"pred = model.predict(X_valid)","execution_count":12,"outputs":[]},{"metadata":{"_cell_guid":"2bcc8873-eaeb-4153-904d-6ef66fe45c9d","_uuid":"c3d39f1cbfeaeee288abc057e357e1aa96de82ec","trusted":true},"cell_type":"code","source":"from sklearn.metrics import auc, roc_auc_score, roc_curve\n\nfalse_positive_rate, recall, thresholds = roc_curve(y_valid, pred)\nroc_auc = auc(false_positive_rate, recall)\nplt.figure()\nplt.title('Receiver Operating Characteristic (ROC)')\nplt.plot(false_positive_rate, recall, 'b', label = 'AUC = %0.3f' %roc_auc)\nplt.legend(loc='lower right')\nplt.plot([0,1], [0,1], 'r--')\nplt.xlim([0.0,1.0])\nplt.ylim([0.0,1.0])\nplt.ylabel('Recall')\nplt.xlabel('Fall-out (1-Specificity)')\nplt.show()","execution_count":13,"outputs":[]},{"metadata":{"_cell_guid":"ebc43b8d-5123-4b0f-aab1-c86a020a38eb","_uuid":"0648908f9849875de07de668d74fc48da04f5579","trusted":true},"cell_type":"code","source":"del X; gc.collect()","execution_count":14,"outputs":[]},{"metadata":{"_cell_guid":"90254968-e0fb-4634-8a39-582c870c803d","_uuid":"35f5013faea5cd1a0415a044a06345265e1e2b00","trusted":true},"cell_type":"code","source":"pred_test = model.predict(X_test)","execution_count":15,"outputs":[]},{"metadata":{"_cell_guid":"ef61c4fb-a6fb-4f82-89c4-3edf5c482237","_uuid":"e7645551f6bb5bc64b8ebefac051233eb20fd856","trusted":true},"cell_type":"code","source":"pred_test = pd.Series(pred_test.reshape(-1), name='is_attributed')\nsub = pd.concat([click_id, pred_test], axis=1)\nsub.to_csv('sub.csv', index=False)","execution_count":29,"outputs":[]},{"metadata":{"_cell_guid":"719c5f2a-ef54-43b4-80e4-a0b97a601694","_uuid":"7aca1749107ff4073031d39f5f272039c75e2ac7","trusted":true},"cell_type":"code","source":"sub.head()","execution_count":30,"outputs":[]},{"metadata":{"_cell_guid":"42968ca7-4f22-4a6b-9cd5-1a203b71a4ff","_uuid":"a69f8be11cd9ac9f841e6b40d0d24ef1092cd3d8","collapsed":true,"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"3f548c88-dfb0-463d-92e8-fde1a95b166e","_uuid":"7b29f2f6d7d13e3d5039bdc38180ec7ee253ce7b","collapsed":true,"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"3334db76-f43c-45e4-be1f-afb6c8e0b406","_uuid":"945b3dcc5ab92606de9f3bbe6f7a33142e01b5af","collapsed":true,"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"295cc153-3a6d-49e7-8190-1e9cb936b621","_uuid":"9102ef7f4583f5c80579b61be9b58e9bafd50da6","collapsed":true,"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"689c5a27-2f29-4cc9-8083-17258ecd839b","_uuid":"08752206da24e5d4fe1d5fe46163a341fc9132bf","collapsed":true,"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"9680bdab-62e7-43dd-9d5c-4fe03c95e519","_uuid":"9760e7e9bbf42b72425af99bca12c314fb9fd746","collapsed":true,"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"46b255e2-60e4-4060-8e85-f35e94d53c3e","_uuid":"4f2d61e9e6550eccfcf44b0fca8431b2b9b9c009","collapsed":true,"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"b4208655-f3d4-4fbc-9096-4e83ffc319e4","_uuid":"dc27a57761a09679dbdebe82298e298f3e5ebf18","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}