{"cells":[{"metadata":{"_uuid":"ea4d9ca0dac800dba50b67277675bf9f7facc8c7"},"cell_type":"markdown","source":"I have been struggling with this competition to find an angle to use traditional machine learning techniques rather than using kalman filters and hough transforms\n\nThe following shows that one *could***** do this challenge by grouping predictions by  the angular momentum along the z axis\n\nI noticed the track ml stuff is broken - nice one Kaggle ;(\n\nThanks to CPMP for providing alternative functions"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"%matplotlib inline\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport numpy as np\nimport pandas as pd\nimport os\nfrom sklearn.preprocessing import StandardScaler, LabelEncoder\n","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","collapsed":true,"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"def score_event_fast(truth, submission):\n    truth = truth[['hit_id', 'particle_id', 'weight']].merge(submission, how='left', on='hit_id')\n    df = truth.groupby(['track_id', 'particle_id']).hit_id.count().to_frame('count_both').reset_index()\n    truth = truth.merge(df, how='left', on=['track_id', 'particle_id'])\n    \n    df1 = df.groupby(['particle_id']).count_both.sum().to_frame('count_particle').reset_index()\n    truth = truth.merge(df1, how='left', on='particle_id')\n    df1 = df.groupby(['track_id']).count_both.sum().to_frame('count_track').reset_index()\n    truth = truth.merge(df1, how='left', on='track_id')\n    truth.count_both *= 2\n    score = truth[(truth.count_both > truth.count_particle) & (truth.count_both > truth.count_track)].weight.sum()\n    return score\n\ndef create_one_event_submission(event_id, hits, labels):\n    sub_data = np.column_stack(([event_id]*len(hits), hits.hit_id.values, labels))\n    submission = pd.DataFrame(data=sub_data, columns=[\"event_id\", \"hit_id\", \"track_id\"]).astype(int)\n    return submission","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"08bf85d8ad9b0d664b385996b0acbc58e689648e"},"cell_type":"code","source":"path_to_train = \"../input/train_1\"\nevent_prefix = \"event000001000\"\n\nevent_id = 0\nhits = pd.read_csv('../input/train_1/event00000100%d-hits.csv' % event_id)\nparticles = pd.read_csv('../input/train_1/event00000100%d-particles.csv' % event_id)\ntruth = pd.read_csv('../input/train_1/event00000100%d-truth.csv' % event_id)\ncell = pd.read_csv('../input/train_1/event00000100%d-cells.csv' % event_id)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d14ced7432b3436c2595ea400029b6dcc5d331fd"},"cell_type":"code","source":"hits = hits.merge(truth,on='hit_id')\nprint(hits.shape)\nhits = hits.merge(particles,on='particle_id',how='left')\nprint(hits.shape)\nhits['target'] = np.sqrt(hits.px**2+hits.py**2)\nhits = hits.fillna(0)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"613f1753326a4fa3e74b021be9217fed6ab06155"},"cell_type":"code","source":"le = LabelEncoder()\nsubmission = create_one_event_submission(0, hits, le.fit_transform(hits.target))\nscore = score_event_fast(truth, submission)\nscore","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}