{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport random\nimport matplotlib.pyplot as plt\nimport os\nprint(os.listdir(\"../input\"))\nimport gc\nfrom sklearn.ensemble import RandomForestClassifier","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"train_columns = ['ip', 'app', 'device', 'os', 'channel', 'click_time', 'is_attributed']\ntest_columns  = ['ip', 'app', 'device', 'os', 'channel', 'click_time', 'click_id']\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        }\ntrain = pd.read_csv(\"../input/train.csv\",skiprows=range(1,123903891), nrows=61000000,usecols=train_columns, dtype=dtypes)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cfaadc2c503e740e48e2693ab6e3e141e64cffaa"},"cell_type":"code","source":"yTrain = train['is_attributed']\ntrain.drop(['is_attributed'], axis=1, inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d2b3970d8d835013f298e63d839916ce2a5451e7"},"cell_type":"code","source":"def timeFeatures(df):\n    df['datetime'] = pd.to_datetime(df['click_time'])\n    df['dow']      = df['datetime'].dt.dayofweek\n    df[\"doy\"]      = df[\"datetime\"].dt.dayofyear\n    df.drop(['click_time', 'datetime'], axis=1, inplace=True)\n    return df","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0fd6072e1c7d43f1c0180ac3fcbace7f531efbb4"},"cell_type":"code","source":"ip_count = train.groupby(['ip'])['channel'].count().reset_index()\nip_count.columns = ['ip', 'clicks_by_ip']\ntrain = pd.merge(train, ip_count, on='ip', how='left', sort=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ec8e8c8b26a5740754c4f3b4aa599b9366140fa1"},"cell_type":"code","source":"train.drop(['ip'], axis=1, inplace=True)\ndel ip_count\ngc.collect()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9924501824207518de8117530b2ace6f5c47ebd5"},"cell_type":"code","source":"train = timeFeatures(train)\ntrain.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7ea7c343e5c3d128df74505553165fa9c70f00f1"},"cell_type":"code","source":"model = RandomForestClassifier()\nmodel.fit(train,yTrain)\nmodel.score(train,yTrain)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ce07ad3ebe74cacf163cdd5f364519ff330f2e58"},"cell_type":"code","source":"del train,yTrain\ngc.collect()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f5657ffaa71cd327dce55e842e2c75feb8d810b3"},"cell_type":"code","source":"test = pd.read_csv(\"../input/test_supplement.csv\",usecols=test_columns, dtype=dtypes)\nip_count = test.groupby(['ip'])['channel'].count().reset_index()\nip_count.columns = ['ip', 'clicks_by_ip']\ntest = pd.merge(test, ip_count, on='ip', how='left', sort=False)\ntest.drop(['ip'], axis=1, inplace=True)\ndel ip_count\ngc.collect()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"08d4efacbe78b64784f091372067675108cefecc"},"cell_type":"code","source":"test = timeFeatures(test)\ntest.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cbf97c9baf0d9934f292e16f24bb76fc27d05c0f"},"cell_type":"code","source":"features = [\"app\",\"device\",\"os\",\"channel\",\"clicks_by_ip\",\"dow\",\"doy\"]\nxTest = test[features]\npred = model.predict(xTest)\ndel xTest\ngc.collect()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3a31c1117b308f7e63f6041d991007c234993a2c"},"cell_type":"code","source":"my_submission = pd.DataFrame({'click_id': test.click_id, 'is_attributed': pred})","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cff7d20047a4d573e4f2189a6a81f5eaa914745f","_kg_hide-output":false,"_kg_hide-input":false},"cell_type":"code","source":"my_submission.to_csv('submission.csv', index=False)","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}