{"cells":[{"metadata":{},"cell_type":"markdown","source":"# CERN TrackML test submission"},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"!pip install https://github.com/LAL/trackml-library/archive/master.zip","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.cluster import DBSCAN\nfrom trackml.dataset import load_dataset","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def dbcluster(eps):\n    x = hits.x.values\n    y = hits.y.values\n    z = hits.z.values\n    r = np.sqrt(x**2 + y**2 + z**2)\n    hits['x2'] = x/r\n    hits['y2'] = y/r\n    r = np.sqrt(x**2 + y**2)\n    hits['z2'] = z/r\n    ss = StandardScaler()\n    X = ss.fit_transform(hits[['x2', 'y2', 'z2']].values)\n    cl = DBSCAN(eps=eps, min_samples=1, algorithm='kd_tree')\n    labels = cl.fit_predict(X)   \n    return labels","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"path_to_test = \"../input/trackml-particle-identification/test\"\ntest_dataset_submissions = []\n\nfor event_id, hits, cells in load_dataset(path_to_test, parts=['hits', 'cells']):\n    labels = dbcluster(0.00715)\n    sub_data = np.column_stack(([event_id]*len(hits), hits.hit_id.values, labels))\n    one_submission = pd.DataFrame(data=sub_data, columns=[\"event_id\", \"hit_id\", \"track_id\"]).astype(int)\n    test_dataset_submissions.append(one_submission)\n    print(event_id)\n\nsubmission = pd.concat(test_dataset_submissions, axis=0)\nsubmission.to_csv('submission.csv.gz', index=False, compression='gzip')","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":1}