{"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 matplotlib.pyplot as plt\nimport seaborn as sns","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"cf16ab64522ca74839900c06762d7ababe348565"},"cell_type":"markdown","source":"Import train set"},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"df = pd.read_csv('../input/train.csv', nrows=10_000_000)\ndf.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"7d6e682ceb0f64e97c0e60509e8a9c75ee134054"},"cell_type":"markdown","source":"Feature exploration"},{"metadata":{"trusted":true,"_uuid":"cd209002fb648931e7938e3e080de9ad6abb6637"},"cell_type":"code","source":"df.time_to_failure.nunique()  # oh...","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"623a0d452d835f48f7a0810a810f6cf154109fb7"},"cell_type":"code","source":"df.acoustic_data.unique()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"f131198524ae61ca7a7690fb69355133f9453fc0"},"cell_type":"markdown","source":"How do submissions look?"},{"metadata":{"trusted":true,"_uuid":"1a9a726a73b90595a1ce6b73607e256361a4f4ca"},"cell_type":"code","source":"sub = pd.read_csv('../input/sample_submission.csv')\nsub.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"527e48d8ce1470d66a2adfe612c2527b027f96d1"},"cell_type":"code","source":"sub.shape","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d765fe839a24d0aff723dc05dd5b97e45cd3ca19"},"cell_type":"markdown","source":"What about the test sets?"},{"metadata":{"trusted":true,"_uuid":"17b0f18aa178e974efe14486387e88a7d8ef3b2a"},"cell_type":"code","source":"test1 = pd.read_csv('../input/test/seg_00030f.csv')\ntest1.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"55228b316acf45e72b2888b900825534021174d4"},"cell_type":"code","source":"test1.acoustic_data.nunique(), test1.acoustic_data.unique()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d08aaa0d8b78762e894027689f3ed934c91f67ad"},"cell_type":"code","source":"test1.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"f16a005cc161d0d7d2c0eb3cb3c42a3eab5c030e"},"cell_type":"markdown","source":"So, what are we predicting?"},{"metadata":{"trusted":true,"_uuid":"6839b3652ba3aab1bc69ca1e45681d5236d2ec0a"},"cell_type":"code","source":"import os\ntest_files = list(os.listdir(\"../input/test\"))\nlen(test_files), test_files[:5]","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"0a4bc671a4100fd0943620242de2615d0ca9b288"},"cell_type":"markdown","source":"Need some changes"},{"metadata":{"trusted":true,"_uuid":"294d6d53c055c7ed16459ce28f6951bc4acdf2c3"},"cell_type":"code","source":"test1.acoustic_data.unique().mean()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"32b91b8accbe9e048bd1164caf5e63d66791d0a3"},"cell_type":"markdown","source":"Make a new Dataframe from all the test data"},{"metadata":{"trusted":true,"_uuid":"612cf49b9e6c3398996f650bf7191bac3182c863"},"cell_type":"code","source":"%%time\n\nfolder = '../input/test/'\nmedians = []\ntest_files_feat = []\n\nfor file_path in test_files:\n    test_files_feat.append(file_path[:-4])\n    path = folder + file_path\n    test_df = pd.read_csv(path)\n    medians.append(int(test_df.acoustic_data.unique().mean()))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5bc7ba996fc4db9a15697876d39ab20af4ad53e7"},"cell_type":"code","source":"len(medians), medians[:5], len(test_files_feat), test_files_feat[:5]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1e742396949a818b827249b7a1df0b33aa2aed2f"},"cell_type":"code","source":"test = pd.DataFrame({ 'seg_id' : test_files_feat, 'acoustic_data' : medians })\ntest.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8ae6827aa996c4635563a2f08eec381802679608"},"cell_type":"markdown","source":"Model"},{"metadata":{"trusted":true,"_uuid":"a971f8e251d3638f9132a6b2506e037da2b5a916"},"cell_type":"code","source":"from xgboost import XGBRegressor\n#from sklearn.ensemble import RandomForestRegressor\nfrom sklearn.metrics import mean_absolute_error as mae","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"339267fe0e30e3401733e2a6a104108cc5501c13"},"cell_type":"code","source":"X, y = df.drop('time_to_failure', axis=1), df.time_to_failure","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"883a31fe3dd7dece658fd2b5d2de441dc7e8e6bd"},"cell_type":"code","source":"%%time\n#rf = RandomForestRegressor(n_estimators=20)  #, criterion='mae')\n#rf.fit(X, y)\nxgb = XGBRegressor(max_depth=5, n_estimators=30, n_jobs=-1)\nxgb.fit(X, y)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a8f205cb9f539b95e380b5d3101a092f83068b2c"},"cell_type":"code","source":"#rf.predict(test.drop('seg_id', axis=1))\nxgb.predict(test.drop('seg_id', axis=1))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2040ba0d0974fa1b105d567d5e0e3c7e0b82cca4"},"cell_type":"code","source":"#test['time_to_failure'] = pd.Series(rf.predict(test.drop('seg_id', axis=1)))\ntest['time_to_failure'] = pd.Series(xgb.predict(test.drop('seg_id', axis=1)))\ntest.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"37491ebc28c00d9b640855e125dd363adbcfd420"},"cell_type":"code","source":"subm = pd.merge(\n    sub.drop('time_to_failure', axis=1),\n    test.drop('acoustic_data', axis=1),\n    on='seg_id'\n)\nsubm.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7dacad10182292199713360f4178e4c841051a05"},"cell_type":"code","source":"subm.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}