{"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","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"meta_data = pd.read_csv('../input/training_set_metadata.csv')\ntest_meta_data = pd.read_csv('../input/test_set_metadata.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":true,"_uuid":"2eec377e2ae127bb44b7d0f98bf169f372b897fb"},"cell_type":"code","source":"classes = np.unique(meta_data['target'])\nclasses_all = np.hstack([classes, [99]])\n\n# create a dictionary {class : index} to map class number with the index \n# (index will be used for submission columns like 0, 1, 2 ... 14)\ntarget_map = {j:i for i, j in enumerate(classes_all)}\n\n# create 'target_id' column to map with 'target' classes\ntarget_ids = [target_map[i] for i in meta_data['target']]\nmeta_data['target_id'] = target_ids","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"eeb61301f53a373a6114389d785f00027ae5f523","scrolled":true},"cell_type":"code","source":"# Build probability arrays for both the galactic and extragalactic groups\ngalactic_cut = meta_data['hostgal_specz'] == 0\ngalactic_data = meta_data[galactic_cut]\nextragalactic_data = meta_data[~galactic_cut]\n\ngalactic_classes = np.unique(galactic_data['target_id'])\nextragalactic_classes = np.unique(extragalactic_data['target_id'])\n\n# add class_99 (index = 14)\ngalactic_classes = np.append(galactic_classes, 14)\nextragalactic_classes = np.append(extragalactic_classes, 14)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8c0fd28998e6129404bdc622a1535755d7da5ab2"},"cell_type":"markdown","source":"***"},{"metadata":{"_uuid":"9230565e6601845f490801f3a96d1705186bf891"},"cell_type":"markdown","source":"# Weights\n\nWeights are based on this discussion: https://www.kaggle.com/c/PLAsTiCC-2018/discussion/67194 , but, apparently, we have different weights for Galactic and Extragalactic groups for the class_99!\n\nIt is also good to check this kernel for more precise calculation of weights: https://www.kaggle.com/ganfear/calculate-exact-class-weights"},{"metadata":{"trusted":true,"_uuid":"c464a70b9c725867d0dbc70b8fb2b3d2ac96f85d"},"cell_type":"code","source":"# Weighted probabilities for Milky Way galaxy\ngalactic_probabilities = np.zeros(15)\nfor x in galactic_classes:\n    if(x == 14):\n        galactic_probabilities[x] = 0.014845745\n        continue\n    if(x == 5):\n        galactic_probabilities[x] = 0.196867058\n        continue\n    galactic_probabilities[x] = 0.197071799\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"371292863929044eff3ae3602b710b9341a0312b"},"cell_type":"code","source":"# Weighted probabilities for Extra Galaxies\nextragalactic_probabilities = np.zeros(15)\nfor x in extragalactic_classes:\n    if(x == 14):\n        extragalactic_probabilities[x] = 0.148880461\n        continue\n    if(x == 7):\n        extragalactic_probabilities[x] = 0.155069005\n        continue\n    if(x == 1):\n        extragalactic_probabilities[x] = 0.154666479\n        continue\n    extragalactic_probabilities[x] = 0.077340579","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"80a693faa4fa1bba7b05fb774dac4252b38ae4d1"},"cell_type":"markdown","source":"***"},{"metadata":{"trusted":true,"_uuid":"04fa08c03cf8bb61d4811c316200b95c1d90df18","scrolled":true},"cell_type":"code","source":"# Apply this prediction to test_meta_data table\nimport tqdm\ndef do_prediction(table):\n    probs = []\n    for index, row in tqdm.tqdm(table.iterrows(), total=len(table)):\n        if row['hostgal_photoz'] == 0:\n            prob = galactic_probabilities\n        else:\n            prob = extragalactic_probabilities\n        probs.append(prob)\n    return np.array(probs)\n\ntest_pred = do_prediction(test_meta_data)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"554ef771214b68cd54053d3d7c5a1557f8e45d54"},"cell_type":"code","source":"test_df = pd.DataFrame(index=test_meta_data['object_id'], data=test_pred, columns=['class_%d' % i for i in classes_all])\ntest_df.to_csv('./naive_benchmark_weighted.csv')","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}