{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"_kg_hide-input":true,"_kg_hide-output":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)\nimport xgboost as xgb\n\nimport matplotlib.pyplot as plt\nfrom ipywidgets import interact, interactive, fixed, interact_manual\nimport ipywidgets as widgets\n\nfrom sklearn.model_selection import train_test_split\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":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true,"_kg_hide-input":true,"_kg_hide-output":true},"cell_type":"code","source":"train_df = pd.read_csv(\"../input/training_set.csv\", dtype={\"object_id\": \"object\"})\ntrain_meta_df = pd.read_csv(\"../input/training_set_metadata.csv\", dtype={\"object_id\": \"object\"})\n\nsample_submission_df = pd.read_csv(\"../input/sample_submission.csv\", dtype={\"object_id\": \"object\"})","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2930fee6e2bc4b1fb63e72607d772bbd6dfced0f","_kg_hide-input":true,"_kg_hide-output":true},"cell_type":"code","source":"train_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"dc9a55ad5fc6a37e2ef8e735fbe71ef11c866239","_kg_hide-input":true,"_kg_hide-output":true},"cell_type":"code","source":"train_meta_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"586f326ef7cb644cd30b380d0600737e14154707","_kg_hide-input":true,"_kg_hide-output":true},"cell_type":"code","source":"print(\"--------------Shape------------------\")\nprint(\" Train: {}\\n Train meta: {}\".format(train_df.shape, train_meta_df.shape))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"99d9ea91fbf561c6e221da0d2b453d6f838a4df0"},"cell_type":"code","source":"ID_col = \"object_id\"\ntarget_col = \"target\"\nts_index_col = \"mjd\"\nts_cols = [\"passband\", \"flux\", \"flux_err\", \"detected\"]\nstatic_cols = [\"ra\", \"decl\", \"gal_l\", \"gal_b\", \"ddf\", \"hostgal_specz\", \n               \"hostgal_photoz\", \"hostgal_photoz_err\", \"distmod\", \"mwebv\"]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"758da3357159c6513b1adc3cb69bc99842af261e"},"cell_type":"code","source":"def preprocess(df):\n    df[target_col] = df[target_col].astype(\"category\").cat.codes\n    return df","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"aa701a8e8cbdb07cdbf7fe6ee4c7fb52cc1d4c72"},"cell_type":"code","source":"train_meta_df = preprocess(train_meta_df)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4522dbca76e2665db25498be08468eac137c2784"},"cell_type":"code","source":"# xgb\nparams = {'eta': 0.02, 'max_depth': 4, 'subsample': 0.9, 'colsample_bytree': 0.9, \n          'objective': 'multi:softprob', 'eval_metric': 'mlogloss', 'silent': True, \n          'num_class':14}\n\nX = train_meta_df.drop([ID_col, target_col], axis=1)\nfeatures = X.columns\ny = train_meta_df[target_col].values\nnrounds = 2000\n\nX_train, X_valid, y_train, y_valid = train_test_split(X, y, test_size=0.2, stratify=y,random_state=123) \n\nd_train = xgb.DMatrix(X_train, y_train) \nd_valid = xgb.DMatrix(X_valid, y_valid) \nwatchlist = [(d_train, 'train'), (d_valid, 'valid')]\nxgb_model = xgb.train(params, d_train, nrounds, watchlist, early_stopping_rounds=100, \n                      maximize=False, verbose_eval=100)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d49655565d9e6a6917a38306dbaaaac17ede50c5"},"cell_type":"code","source":"test_meta_df = pd.read_csv(\"../input/test_set_metadata.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"27f9cdeceecd9d04deb826f4a1a3af5a9c0c9da9"},"cell_type":"code","source":"## Setting up submission\n\nsub = xgb_model.predict(xgb.DMatrix(test_meta_df[features]), ntree_limit=xgb_model.best_ntree_limit+50)\nsub = pd.DataFrame(sub)\nsub['14'] = 0","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c9487ae8b290dcaa3eeeb6cb49e4e8225b1f9dc3"},"cell_type":"code","source":"sub[\"max_prob\"] = sub.values.argmax(axis=1)\nsub[\"max_prob\"] = sub[\"max_prob\"] < 0.5\nsub['14'] = sub[\"max_prob\"].astype('int')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3a4823a19e6773a5007aaa0f459858eefbd4bfeb"},"cell_type":"code","source":"sub2 = sub.div(sub.sum(axis=1), axis=0)\nsub2.drop([\"max_prob\"], axis=1, inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"27f9cdeceecd9d04deb826f4a1a3af5a9c0c9da9"},"cell_type":"code","source":"sub2.rename(columns= dict(zip(sub2.columns, sample_submission_df.iloc[:,1:].columns)), inplace=True)\nsub2[ID_col] = test_meta_df[ID_col]\nsub2 = sub2[sample_submission_df.columns]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"dde08720c2c66ff29d11efa374e872144d7d7b1c"},"cell_type":"code","source":"sub2.to_csv(\"submission2.csv\", index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0de680eedf3cae17f91b6d24bbb0b3b8a17deba3"},"cell_type":"code","source":"sub2.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2c563c6507312ee38f4b1380f3474c4070bfea04"},"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.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}