{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":false,"collapsed":true},"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\nimport numpy as np # linear algebra\nimport 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\nimport os\nprint(os.listdir(\"../input\"))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":false,"collapsed":true},"cell_type":"code","source":"train = pd.read_csv(\"../input/train.csv\", nrows = 10000000)\ntrain.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"1da0b94f4b0c20db97ecb17ab700404a7fdb7102","_cell_guid":"183b078b-8bd1-4a0f-9760-6df64c2055a5","trusted":false,"collapsed":true},"cell_type":"code","source":"test = pd.read_csv(\"../input/test.csv\")\ntest.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"85579cb53597e28db4979fdeb110897794b2147d","_cell_guid":"8f3333c1-defc-4b31-85f7-ac71e52401b8","trusted":false,"collapsed":true},"cell_type":"code","source":"import matplotlib.pyplot as plt\n%matplotlib inline\ncount_classes = pd.value_counts(train['is_attributed'],sort=True).sort_index()\ncount_classes.plot(kind='bar')\nplt.title(\"class histogram\")\nplt.xlabel(\"Class\")\nplt.ylabel(\"Frequency\")","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8d9e30a084ecea45acd3ca2aa5fc9b4784acbc3f","collapsed":true,"_cell_guid":"0597245a-7883-4426-9580-f4440178fd6c","trusted":false},"cell_type":"code","source":"y_train = train['is_attributed']\n\nx_train = train[['ip', 'app', 'device', 'os', 'channel']]\nx_test  = test[['ip', 'app', 'device', 'os', 'channel']]\n\ndel train\ndel test\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"5d12a19da027a93792f4c9fa2aab5ee992972e96","_cell_guid":"dbf8b5f9-d91d-4123-9e19-b04c0355b4ab","trusted":false,"collapsed":true},"cell_type":"code","source":"from imblearn.over_sampling import SMOTE\nsm = SMOTE()\nx_train, y_train = sm.fit_sample(x_train, y_train)\nprint(\"Finish balance the data, Finish normalize the data\")","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d504e89dec73a765c4385b5f30b4bccc571df667","collapsed":true,"_cell_guid":"be5d84dd-9b90-4b7e-a9cd-c7117d6dd5cd","trusted":false},"cell_type":"code","source":"from sklearn.ensemble import RandomForestClassifier\nmodel = RandomForestClassifier()\nmodel.fit(x_train, y_train)\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"ca4ab26b5dec394fd43fd2134457ea948f635a6c","collapsed":true,"_cell_guid":"c90c4a16-6b50-497c-86e5-e621e306f263","trusted":false},"cell_type":"code","source":"X_test = pd.DataFrame(x_test, columns = ['f0', 'f1', 'f2', 'f3', 'f4'])","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"838180c1637b0b7879689438d26187907e3bddb2","collapsed":true,"_cell_guid":"2abb183c-f885-4e6f-bd5d-efc54ce8b401","trusted":false},"cell_type":"code","source":"y_pred = model.predict_proba(X_test)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"ab7fe4ea310dd8d03c8a9c43a1238025f017199b","collapsed":true,"_cell_guid":"70d4f1f8-7db8-4265-b09e-f39fc47b579a","trusted":false},"cell_type":"code","source":"sub = pd.read_csv(\"../input/sample_submission.csv\")\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"27d48bce5d197fd2f6b750babd384a4af00cd417","collapsed":true,"_cell_guid":"f3183eef-7c03-4db8-b4d6-63b9a3006252","trusted":false},"cell_type":"code","source":"sub['is_attributed'] = y_pred[:,0]\nsub.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"6fa854a76872783e6f450ee7a5fb3a2990076e53","_cell_guid":"8ae222b5-6e31-4387-bdee-6424286de29c","trusted":false,"collapsed":true},"cell_type":"code","source":"sub.to_csv('sub_rf.csv', index=False)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"0a81e1974b88e63218f43edabd43c164cb7d4a7f","_cell_guid":"f17e5d70-5baa-46f2-b1e0-c4eb7c9c6716","trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"cf52162ce88a4e0a19d6443c40d49bab0ac3c194"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}