{"cells":[{"metadata":{"_uuid":"08091dff-905c-46b0-b938-ca967fae58cd","_cell_guid":"c88af7c2-64b5-4414-8aff-3290c9ed794f","trusted":true},"cell_type":"markdown","source":"Number of items of Hydrogen (H), Carbon(C) an Nitrogen(N) are playing role in deciding bond or scalr_coupling_constant for each molecule.\nI have tried here simple approach to find out count of each item in each molecule."},{"metadata":{"_uuid":"0e50c895-d331-4bba-91a0-28227c41d687","_cell_guid":"ac5d7ba4-c6bd-43ab-8c09-3a29f3f02370","trusted":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)\n\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":0,"outputs":[]},{"metadata":{"_uuid":"e5058604-92f8-49b6-a71d-57b49e192758","_cell_guid":"c049e419-4aa5-41ba-8ace-d7511d91b3bf","trusted":true},"cell_type":"code","source":"train_original = pd.read_csv(\"../input/train.csv\")\nstructures_original = pd.read_csv(\"../input/structures.csv\")\ntest_original = pd.read_csv(\"../input/test.csv\")","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"e1c0b4f4-34bb-46f7-a470-fbd0b277d5a5","_cell_guid":"a5f97757-f8d6-47a7-ad3f-a9f0d8df7698","trusted":true},"cell_type":"code","source":"train_original.head()","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"daf59f15-541d-4bba-8ab3-595521a0721b","_cell_guid":"3bc128c2-0462-4e34-9a61-bfb9340748b8","trusted":true},"cell_type":"code","source":"structures_original.head()","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"1fb7f782-67c4-4536-b301-8432d6de1e63","_cell_guid":"9d21cc64-fdd8-4968-9465-df828120c0e3","trusted":true},"cell_type":"markdown","source":"Lets see how many items are there in dsgdb9nsd_000015\n#"},{"metadata":{"_uuid":"a0ed1a16-285e-452f-8089-f8e0bbee55f8","_cell_guid":"236fb9bf-2bca-4d35-b8f1-10c10f280728","trusted":true},"cell_type":"code","source":"structures_original[structures_original['molecule_name'] == 'dsgdb9nsd_000015']","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"980b5365-6c8b-488d-a8a7-f4f6b706edd1","_cell_guid":"38bb37ad-a8aa-433a-a6b0-f68315e1954f","trusted":true},"cell_type":"markdown","source":"As per above there are total 9 items in molecule.\n\nCarbon - 2\n\nOxygen - 1\n\nHydrogen - 6"},{"metadata":{"_uuid":"978fdec2-4465-4775-9c2f-5641a8b24a96","_cell_guid":"13c6ca6e-720f-4bcd-b05d-55131854df58","trusted":true},"cell_type":"markdown","source":"Total items in each molecule can be calculated by simply grouping structures_original dataframe by molecule_name ans atom with count as a aggregate function"},{"metadata":{"_uuid":"0a1df426-7801-4c59-8ce8-3107f95ffa03","_cell_guid":"29af5d82-4331-4b43-8696-c30b68495d92","trusted":true},"cell_type":"code","source":"moleculeCount = structures_original.groupby(by=['molecule_name','atom'])[['atom']].count()\nmoleculeCount.rename(columns={'atom':'count'},inplace = True)\nmoleculeCount = moleculeCount.unstack(fill_value=0)\nmoleculeCount = moleculeCount['count'].reset_index()\n\nmoleculeCount.head()","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"b8060892-3393-467f-8b65-01e73694c3e3","_cell_guid":"96fc67ce-791f-4bf9-acb4-45d816e2e22e","trusted":true},"cell_type":"code","source":"moleculeCount[moleculeCount['molecule_name'] == 'dsgdb9nsd_000015']","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"13e68337-759b-4fbf-906f-23d67a6d5f28","_cell_guid":"d9dcaae8-f558-42af-97b1-7484f6e33790","trusted":true},"cell_type":"markdown","source":"Merge structures_original and moleculeCount in single dataframe"},{"metadata":{"_uuid":"a6467ea4-86ce-4e95-8025-5d85a27589eb","_cell_guid":"8c7c1f09-4185-41f0-b489-c6c0097c8f7f","trusted":true},"cell_type":"code","source":"structures = pd.DataFrame.merge(structures_original,moleculeCount\n                               ,how='inner'\n                               ,left_on = ['molecule_name'] \n                               ,right_on = ['molecule_name']\n                              )\n\nstructures.head()","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"52edfaf1-8bd9-464c-a602-154aaad69640","_cell_guid":"70a04f50-33e4-4163-9527-69ab7144d58d","trusted":true},"cell_type":"markdown","source":"Join structures dataframe with train and test data to include item counts in train and test data.\n\nAlso, I am using below kernel to calculate distance between 2 items in a molecule.\n \nhttps://www.kaggle.com/seriousran/just-speed-up-calculate-distance-from-benchmark\n\nThe Frobenius norm is given by [1]:\n||A||F = [\\sum{i,j} abs(a_{i,j})^2]^{1/2}"},{"metadata":{"_uuid":"7f91de19-fdfc-4068-8a5b-516fb37ab8b4","_cell_guid":"cfc7d23e-7fa5-4f98-8a58-27e49b0ecdca","trusted":true},"cell_type":"code","source":"tmp_merge = pd.DataFrame.merge(train_original,structures\n                               ,how='left'\n                               ,left_on = ['molecule_name','atom_index_0'] \n                               ,right_on = ['molecule_name','atom_index']\n                              )\n\ntmp_merge = tmp_merge.merge(structures\n                ,how='left'\n                ,left_on = ['molecule_name','atom_index_1'] \n                ,right_on = ['molecule_name','atom_index']\n               )\n\ntmp_merge.drop(columns=['atom_index_x','atom_index_y','C_x','F_x','H_x','N_x','O_x'],inplace=True)\ntmp_merge.columns = ['id' , 'molecule_name' , 'atom_0' , 'atom_1' , 'type' , 'scalar_coupling_constant' , \n                      'atom_nm_0' , 'x_0' , 'y_0' , 'z_0' , 'atom_nm_1' , 'x_1' , 'y_1' , 'z_1','C','F','H','N','O']\n\ntrain = tmp_merge[['id' , 'molecule_name' , 'atom_0' , 'atom_1' , 'type'  , 'atom_nm_0' , 'x_0' ,\n           'y_0' , 'z_0' , 'atom_nm_1' , 'x_1' , 'y_1' , 'z_1','C','F','H','N','O', 'scalar_coupling_constant']]\ntrain.sort_values(by=['id','molecule_name'],inplace=True)\ntrain.reset_index(inplace=True,drop=True)\n\ntmp_merge = None\n\ntrain['dist'] = np.linalg.norm(train[['x_0', 'y_0', 'z_0']].values - train[['x_1', 'y_1', 'z_1']].values, axis=1)\ntrain.drop(columns=['x_0', 'y_0', 'z_0','x_1', 'y_1', 'z_1'],inplace=True)\n\ntrain.head()","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"946b9642-6cb2-4f78-895a-dacca788dd01","_cell_guid":"dd1a5600-29d1-482a-b4f3-fc1ae8a461fc","trusted":true},"cell_type":"code","source":"tmp_merge = pd.DataFrame.merge(test_original,structures\n                               ,how='left'\n                               ,left_on = ['molecule_name','atom_index_0'] \n                               ,right_on = ['molecule_name','atom_index']\n                              )\n\ntmp_merge = tmp_merge.merge(structures\n                ,how='left'\n                ,left_on = ['molecule_name','atom_index_1'] \n                ,right_on = ['molecule_name','atom_index']\n               )\n\ntmp_merge.drop(columns=['atom_index_x','atom_index_y','C_x','F_x','H_x','N_x','O_x'],inplace=True)\ntmp_merge.columns = ['id' , 'molecule_name' , 'atom_0' , 'atom_1' , 'type' , \n                      'atom_nm_0' , 'x_0' , 'y_0' , 'z_0' , 'atom_nm_1' , 'x_1' , 'y_1' , 'z_1','C','F','H','N','O']\n\ntest = tmp_merge[['id' , 'molecule_name' , 'atom_0' , 'atom_1' , 'type'  , 'atom_nm_0' , 'x_0' ,\n           'y_0' , 'z_0' , 'atom_nm_1' , 'x_1' , 'y_1' , 'z_1','C','F','H','N','O']]\ntest.sort_values(by=['id','molecule_name'],inplace=True)\ntest.reset_index(inplace=True,drop=True)\n\ntmp_merge = None\n\ntest['dist'] = np.linalg.norm(test[['x_0', 'y_0', 'z_0']].values - test[['x_1', 'y_1', 'z_1']].values, axis=1)\ntest.drop(columns=['x_0', 'y_0', 'z_0','x_1', 'y_1', 'z_1'],inplace=True)\n\ntest.head()","execution_count":0,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}