{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom sklearn.model_selection import GroupKFold\n\nimport os\nprint(os.listdir(\"../input\"))","execution_count":19,"outputs":[{"output_type":"stream","text":"['potential_energy.csv', 'mulliken_charges.csv', 'train.csv', 'scalar_coupling_contributions.csv', 'sample_submission.csv', 'structures', 'test.csv', 'magnetic_shielding_tensors.csv', 'dipole_moments.csv', 'structures.csv']\n","name":"stdout"}]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"train = pd.read_csv('../input/train.csv', index_col='id')\ntest = pd.read_csv('../input/test.csv', index_col='id')","execution_count":20,"outputs":[{"output_type":"stream","text":"/opt/conda/lib/python3.6/site-packages/numpy/lib/arraysetops.py:569: FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison\n  mask |= (ar1 == a)\n","name":"stderr"}]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.head()","execution_count":21,"outputs":[{"output_type":"execute_result","execution_count":21,"data":{"text/plain":"       molecule_name            ...             scalar_coupling_constant\nid                              ...                                     \n0   dsgdb9nsd_000001            ...                              84.8076\n1   dsgdb9nsd_000001            ...                             -11.2570\n2   dsgdb9nsd_000001            ...                             -11.2548\n3   dsgdb9nsd_000001            ...                             -11.2543\n4   dsgdb9nsd_000001            ...                              84.8074\n\n[5 rows x 5 columns]","text/html":"<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>molecule_name</th>\n      <th>atom_index_0</th>\n      <th>atom_index_1</th>\n      <th>type</th>\n      <th>scalar_coupling_constant</th>\n    </tr>\n    <tr>\n      <th>id</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>dsgdb9nsd_000001</td>\n      <td>1</td>\n      <td>0</td>\n      <td>1JHC</td>\n      <td>84.8076</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>dsgdb9nsd_000001</td>\n      <td>1</td>\n      <td>2</td>\n      <td>2JHH</td>\n      <td>-11.2570</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>dsgdb9nsd_000001</td>\n      <td>1</td>\n      <td>3</td>\n      <td>2JHH</td>\n      <td>-11.2548</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>dsgdb9nsd_000001</td>\n      <td>1</td>\n      <td>4</td>\n      <td>2JHH</td>\n      <td>-11.2543</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>dsgdb9nsd_000001</td>\n      <td>2</td>\n      <td>0</td>\n      <td>1JHC</td>\n      <td>84.8074</td>\n    </tr>\n  </tbody>\n</table>\n</div>"},"metadata":{}}]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.describe()","execution_count":32,"outputs":[{"output_type":"execute_result","execution_count":32,"data":{"text/plain":"       atom_index_0  atom_index_1      ...                y_1           z_1\ncount  4.658147e+06  4.658147e+06      ...       4.658147e+06  4.658147e+06\nmean   1.335689e+01  5.883966e+00      ...      -3.956294e-01  7.768257e-02\nstd    3.267712e+00  4.993943e+00      ...       1.767196e+00  1.255910e+00\nmin    0.000000e+00  0.000000e+00      ...      -9.254405e+00 -8.789131e+00\n25%    1.100000e+01  2.000000e+00      ...      -1.607567e+00 -6.696343e-01\n50%    1.300000e+01  5.000000e+00      ...      -5.081915e-01  1.549763e-02\n75%    1.600000e+01  8.000000e+00      ...       2.882334e-01  8.926133e-01\nmax    2.800000e+01  2.800000e+01      ...       8.653665e+00  7.460225e+00\n\n[8 rows x 9 columns]","text/html":"<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>atom_index_0</th>\n      <th>atom_index_1</th>\n      <th>scalar_coupling_constant</th>\n      <th>x_0</th>\n      <th>y_0</th>\n      <th>z_0</th>\n      <th>x_1</th>\n      <th>y_1</th>\n      <th>z_1</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>count</th>\n      <td>4.658147e+06</td>\n      <td>4.658147e+06</td>\n      <td>4.658147e+06</td>\n      <td>4.658147e+06</td>\n      <td>4.658147e+06</td>\n      <td>4.658147e+06</td>\n      <td>4.658147e+06</td>\n      <td>4.658147e+06</td>\n      <td>4.658147e+06</td>\n    </tr>\n    <tr>\n      <th>mean</th>\n      <td>1.335689e+01</td>\n      <td>5.883966e+00</td>\n      <td>1.592165e+01</td>\n      <td>1.058781e-01</td>\n      <td>-2.203691e-01</td>\n      <td>4.712888e-02</td>\n      <td>9.757184e-02</td>\n      <td>-3.956294e-01</td>\n      <td>7.768257e-02</td>\n    </tr>\n    <tr>\n      <th>std</th>\n      <td>3.267712e+00</td>\n      <td>4.993943e+00</td>\n      <td>3.494198e+01</td>\n      <td>1.759873e+00</td>\n      <td>2.121789e+00</td>\n      <td>1.564513e+00</td>\n      <td>1.479416e+00</td>\n      <td>1.767196e+00</td>\n      <td>1.255910e+00</td>\n    </tr>\n    <tr>\n      <th>min</th>\n      <td>0.000000e+00</td>\n      <td>0.000000e+00</td>\n      <td>-3.621860e+01</td>\n      <td>-9.234889e+00</td>\n      <td>-9.494160e+00</td>\n      <td>-9.134765e+00</td>\n      <td>-9.234889e+00</td>\n      <td>-9.254405e+00</td>\n      <td>-8.789131e+00</td>\n    </tr>\n    <tr>\n      <th>25%</th>\n      <td>1.100000e+01</td>\n      <td>2.000000e+00</td>\n      <td>-2.549780e-01</td>\n      <td>-9.530924e-01</td>\n      <td>-1.836966e+00</td>\n      <td>-9.642128e-01</td>\n      <td>-7.927602e-01</td>\n      <td>-1.607567e+00</td>\n      <td>-6.696343e-01</td>\n    </tr>\n    <tr>\n      <th>50%</th>\n      <td>1.300000e+01</td>\n      <td>5.000000e+00</td>\n      <td>2.281130e+00</td>\n      <td>1.341212e-01</td>\n      <td>-2.979936e-01</td>\n      <td>4.972893e-03</td>\n      <td>4.660419e-02</td>\n      <td>-5.081915e-01</td>\n      <td>1.549763e-02</td>\n    </tr>\n    <tr>\n      <th>75%</th>\n      <td>1.600000e+01</td>\n      <td>8.000000e+00</td>\n      <td>7.390655e+00</td>\n      <td>1.227746e+00</td>\n      <td>1.753418e+00</td>\n      <td>1.035102e+00</td>\n      <td>9.682240e-01</td>\n      <td>2.882334e-01</td>\n      <td>8.926133e-01</td>\n    </tr>\n    <tr>\n      <th>max</th>\n      <td>2.800000e+01</td>\n      <td>2.800000e+01</td>\n      <td>2.048800e+02</td>\n      <td>9.382240e+00</td>\n      <td>9.714469e+00</td>\n      <td>7.637578e+00</td>\n      <td>8.420893e+00</td>\n      <td>8.653665e+00</td>\n      <td>7.460225e+00</td>\n    </tr>\n  </tbody>\n</table>\n</div>"},"metadata":{}}]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.shape","execution_count":22,"outputs":[{"output_type":"execute_result","execution_count":22,"data":{"text/plain":"(4658147, 5)"},"metadata":{}}]},{"metadata":{"trusted":true},"cell_type":"code","source":"test.shape","execution_count":23,"outputs":[{"output_type":"execute_result","execution_count":23,"data":{"text/plain":"(2505542, 4)"},"metadata":{}}]},{"metadata":{"trusted":true},"cell_type":"code","source":"structures = pd.read_csv('../input/structures.csv')\ndisplay(structures.head())","execution_count":24,"outputs":[{"output_type":"display_data","data":{"text/plain":"      molecule_name  atom_index atom         x         y         z\n0  dsgdb9nsd_000001           0    C -0.012698  1.085804  0.008001\n1  dsgdb9nsd_000001           1    H  0.002150 -0.006031  0.001976\n2  dsgdb9nsd_000001           2    H  1.011731  1.463751  0.000277\n3  dsgdb9nsd_000001           3    H -0.540815  1.447527 -0.876644\n4  dsgdb9nsd_000001           4    H -0.523814  1.437933  0.906397","text/html":"<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>molecule_name</th>\n      <th>atom_index</th>\n      <th>atom</th>\n      <th>x</th>\n      <th>y</th>\n      <th>z</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>dsgdb9nsd_000001</td>\n      <td>0</td>\n      <td>C</td>\n      <td>-0.012698</td>\n      <td>1.085804</td>\n      <td>0.008001</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>dsgdb9nsd_000001</td>\n      <td>1</td>\n      <td>H</td>\n      <td>0.002150</td>\n      <td>-0.006031</td>\n      <td>0.001976</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>dsgdb9nsd_000001</td>\n      <td>2</td>\n      <td>H</td>\n      <td>1.011731</td>\n      <td>1.463751</td>\n      <td>0.000277</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>dsgdb9nsd_000001</td>\n      <td>3</td>\n      <td>H</td>\n      <td>-0.540815</td>\n      <td>1.447527</td>\n      <td>-0.876644</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>dsgdb9nsd_000001</td>\n      <td>4</td>\n      <td>H</td>\n      <td>-0.523814</td>\n      <td>1.437933</td>\n      <td>0.906397</td>\n    </tr>\n  </tbody>\n</table>\n</div>"},"metadata":{}}]},{"metadata":{"trusted":true},"cell_type":"code","source":"# molecule level EDA + stats\nprint(\"unique molecules\",structures[\"atom\"].nunique())","execution_count":25,"outputs":[{"output_type":"stream","text":"unique molecules 5\n","name":"stdout"}]},{"metadata":{"trusted":true},"cell_type":"code","source":"val = 1\nprint(f\"{val+1}\")","execution_count":28,"outputs":[{"output_type":"stream","text":"2\n","name":"stdout"}]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Map the atom structure data into train and test files\n\ndef map_atom_info(df, atom_idx):\n    df = pd.merge(df, structures, how = 'left',\n                  left_on  = ['molecule_name', f'atom_index_{atom_idx}'],\n                  right_on = ['molecule_name',  'atom_index'])\n    \n    df = df.drop('atom_index', axis=1)\n    df = df.rename(columns={'atom': f'atom_{atom_idx}',\n                            'x': f'x_{atom_idx}',\n                            'y': f'y_{atom_idx}',\n                            'z': f'z_{atom_idx}'})\n    return df\n\ntrain = map_atom_info(train, 0)\ntrain = map_atom_info(train, 1)\n\ntest = map_atom_info(test, 0)\ntest = map_atom_info(test, 1)","execution_count":29,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.head()","execution_count":30,"outputs":[{"output_type":"execute_result","execution_count":30,"data":{"text/plain":"      molecule_name  atom_index_0    ...          y_1       z_1\n0  dsgdb9nsd_000001             1    ...     1.085804  0.008001\n1  dsgdb9nsd_000001             1    ...     1.463751  0.000277\n2  dsgdb9nsd_000001             1    ...     1.447527 -0.876644\n3  dsgdb9nsd_000001             1    ...     1.437933  0.906397\n4  dsgdb9nsd_000001             2    ...     1.085804  0.008001\n\n[5 rows x 13 columns]","text/html":"<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>molecule_name</th>\n      <th>atom_index_0</th>\n      <th>atom_index_1</th>\n      <th>type</th>\n      <th>scalar_coupling_constant</th>\n      <th>atom_0</th>\n      <th>x_0</th>\n      <th>y_0</th>\n      <th>z_0</th>\n      <th>atom_1</th>\n      <th>x_1</th>\n      <th>y_1</th>\n      <th>z_1</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>dsgdb9nsd_000001</td>\n      <td>1</td>\n      <td>0</td>\n      <td>1JHC</td>\n      <td>84.8076</td>\n      <td>H</td>\n      <td>0.002150</td>\n      <td>-0.006031</td>\n      <td>0.001976</td>\n      <td>C</td>\n      <td>-0.012698</td>\n      <td>1.085804</td>\n      <td>0.008001</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>dsgdb9nsd_000001</td>\n      <td>1</td>\n      <td>2</td>\n      <td>2JHH</td>\n      <td>-11.2570</td>\n      <td>H</td>\n      <td>0.002150</td>\n      <td>-0.006031</td>\n      <td>0.001976</td>\n      <td>H</td>\n      <td>1.011731</td>\n      <td>1.463751</td>\n      <td>0.000277</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>dsgdb9nsd_000001</td>\n      <td>1</td>\n      <td>3</td>\n      <td>2JHH</td>\n      <td>-11.2548</td>\n      <td>H</td>\n      <td>0.002150</td>\n      <td>-0.006031</td>\n      <td>0.001976</td>\n      <td>H</td>\n      <td>-0.540815</td>\n      <td>1.447527</td>\n      <td>-0.876644</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>dsgdb9nsd_000001</td>\n      <td>1</td>\n      <td>4</td>\n      <td>2JHH</td>\n      <td>-11.2543</td>\n      <td>H</td>\n      <td>0.002150</td>\n      <td>-0.006031</td>\n      <td>0.001976</td>\n      <td>H</td>\n      <td>-0.523814</td>\n      <td>1.437933</td>\n      <td>0.906397</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>dsgdb9nsd_000001</td>\n      <td>2</td>\n      <td>0</td>\n      <td>1JHC</td>\n      <td>84.8074</td>\n      <td>H</td>\n      <td>1.011731</td>\n      <td>1.463751</td>\n      <td>0.000277</td>\n      <td>C</td>\n      <td>-0.012698</td>\n      <td>1.085804</td>\n      <td>0.008001</td>\n    </tr>\n  </tbody>\n</table>\n</div>"},"metadata":{}}]},{"metadata":{"trusted":true},"cell_type":"code","source":"# %%time\n# # Engineer a single feature: distance vector between atoms\n# #  (there's ways to speed this up!)\n\n# def dist(row):\n#     return ( (row['x_1'] - row['x_0'])**2 +\n#              (row['y_1'] - row['y_0'])**2 +\n#              (row['z_1'] - row['z_0'])**2 ) ** 0.5\n\n# train['dist'] = train.apply(lambda x: dist(x), axis=1)\n# test['dist'] = test.apply(lambda x: dist(x), axis=1)","execution_count":31,"outputs":[{"output_type":"error","ename":"KeyboardInterrupt","evalue":"","traceback":["\u001b[0;31m---------------------------------------------------------------------------\u001b[0m","\u001b[0;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)","\u001b[0;32m<timed exec>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n","\u001b[0;32m/opt/conda/lib/python3.6/site-packages/pandas/core/frame.py\u001b[0m in \u001b[0;36mapply\u001b[0;34m(self, func, axis, broadcast, raw, reduce, result_type, args, **kwds)\u001b[0m\n\u001b[1;32m   6012\u001b[0m                          \u001b[0margs\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   6013\u001b[0m                          kwds=kwds)\n\u001b[0;32m-> 6014\u001b[0;31m         \u001b[0;32mreturn\u001b[0m \u001b[0mop\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_result\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m   6015\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   6016\u001b[0m     \u001b[0;32mdef\u001b[0m \u001b[0mapplymap\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfunc\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n","\u001b[0;32m/opt/conda/lib/python3.6/site-packages/pandas/core/apply.py\u001b[0m in \u001b[0;36mget_result\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m    140\u001b[0m             \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mapply_raw\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    141\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 142\u001b[0;31m         \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mapply_standard\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    143\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    144\u001b[0m     \u001b[0;32mdef\u001b[0m \u001b[0mapply_empty_result\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n","\u001b[0;32m/opt/conda/lib/python3.6/site-packages/pandas/core/apply.py\u001b[0m in \u001b[0;36mapply_standard\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m    240\u001b[0m                                           \u001b[0maxis\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0maxis\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    241\u001b[0m                                           \u001b[0mdummy\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mdummy\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 242\u001b[0;31m                                           labels=labels)\n\u001b[0m\u001b[1;32m    243\u001b[0m                 \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mobj\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_constructor_sliced\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mresult\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mindex\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mlabels\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    244\u001b[0m             \u001b[0;32mexcept\u001b[0m \u001b[0mException\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n","\u001b[0;32mpandas/_libs/reduction.pyx\u001b[0m in \u001b[0;36mpandas._libs.reduction.reduce\u001b[0;34m()\u001b[0m\n","\u001b[0;32mpandas/_libs/reduction.pyx\u001b[0m in \u001b[0;36mpandas._libs.reduction.Reducer.get_result\u001b[0;34m()\u001b[0m\n","\u001b[0;32m<timed exec>\u001b[0m in \u001b[0;36m<lambda>\u001b[0;34m(x)\u001b[0m\n","\u001b[0;32m<timed exec>\u001b[0m in \u001b[0;36mdist\u001b[0;34m(row)\u001b[0m\n","\u001b[0;32m/opt/conda/lib/python3.6/site-packages/pandas/core/series.py\u001b[0m in \u001b[0;36m__getitem__\u001b[0;34m(self, key)\u001b[0m\n\u001b[1;32m    765\u001b[0m         \u001b[0mkey\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcom\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_apply_if_callable\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    766\u001b[0m         \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 767\u001b[0;31m             \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_value\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mkey\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    768\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    769\u001b[0m             \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mis_scalar\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mresult\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n","\u001b[0;32m/opt/conda/lib/python3.6/site-packages/pandas/core/indexes/base.py\u001b[0m in \u001b[0;36mget_value\u001b[0;34m(self, series, key)\u001b[0m\n\u001b[1;32m   3110\u001b[0m                     \u001b[0;32mreturn\u001b[0m \u001b[0ms\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   3111\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 3112\u001b[0;31m         \u001b[0ms\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcom\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_values_from_object\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mseries\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m   3113\u001b[0m         \u001b[0mk\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcom\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_values_from_object\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   3114\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n","\u001b[0;32mpandas/_libs/lib.pyx\u001b[0m in \u001b[0;36mpandas._libs.lib.values_from_object\u001b[0;34m()\u001b[0m\n","\u001b[0;32m/opt/conda/lib/python3.6/site-packages/pandas/core/series.py\u001b[0m in \u001b[0;36mget_values\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m    474\u001b[0m         \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_data\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mformatting_values\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    475\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 476\u001b[0;31m     \u001b[0;32mdef\u001b[0m \u001b[0mget_values\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    477\u001b[0m         \u001b[0;34m\"\"\" same as values (but handles sparseness conversions); is a view \"\"\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    478\u001b[0m         \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_data\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_values\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n","\u001b[0;31mKeyboardInterrupt\u001b[0m: "]}]},{"metadata":{"trusted":true},"cell_type":"code","source":"%%time\n# This block is SPPED UP\n\ntrain_p_0 = train[['x_0', 'y_0', 'z_0']].values\ntrain_p_1 = train[['x_1', 'y_1', 'z_1']].values\ntest_p_0 = test[['x_0', 'y_0', 'z_0']].values\ntest_p_1 = test[['x_1', 'y_1', 'z_1']].values\n\ntrain['dist_speedup'] = np.linalg.norm(train_p_0 - train_p_1, axis=1)\ntest['dist_speedup'] = np.linalg.norm(test_p_0 - test_p_1, axis=1)","execution_count":33,"outputs":[{"output_type":"stream","text":"CPU times: user 272 ms, sys: 392 ms, total: 664 ms\nWall time: 660 ms\n","name":"stdout"}]},{"metadata":{},"cell_type":"markdown","source":"### export before model running"},{"metadata":{"trusted":true},"cell_type":"code","source":"train[\"index_diff\"] =( (train[\"atom_index_0\"]- train[\"atom_index_1\"]).abs()-1)\ntest[\"index_diff\"] =( (test[\"atom_index_0\"]- test[\"atom_index_1\"]).abs()-1)","execution_count":36,"outputs":[{"output_type":"execute_result","execution_count":36,"data":{"text/plain":"      molecule_name  atom_index_0    ...      dist_speedup index_diff\n0  dsgdb9nsd_000001             1    ...          1.091953          0\n1  dsgdb9nsd_000001             1    ...          1.783120          0\n2  dsgdb9nsd_000001             1    ...          1.783147          1\n3  dsgdb9nsd_000001             1    ...          1.783157          2\n4  dsgdb9nsd_000001             2    ...          1.091952          1\n\n[5 rows x 15 columns]","text/html":"<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>molecule_name</th>\n      <th>atom_index_0</th>\n      <th>atom_index_1</th>\n      <th>type</th>\n      <th>scalar_coupling_constant</th>\n      <th>atom_0</th>\n      <th>x_0</th>\n      <th>y_0</th>\n      <th>z_0</th>\n      <th>atom_1</th>\n      <th>x_1</th>\n      <th>y_1</th>\n      <th>z_1</th>\n      <th>dist_speedup</th>\n      <th>index_diff</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>dsgdb9nsd_000001</td>\n      <td>1</td>\n      <td>0</td>\n      <td>1JHC</td>\n      <td>84.8076</td>\n      <td>H</td>\n      <td>0.002150</td>\n      <td>-0.006031</td>\n      <td>0.001976</td>\n      <td>C</td>\n      <td>-0.012698</td>\n      <td>1.085804</td>\n      <td>0.008001</td>\n      <td>1.091953</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>dsgdb9nsd_000001</td>\n      <td>1</td>\n      <td>2</td>\n      <td>2JHH</td>\n      <td>-11.2570</td>\n      <td>H</td>\n      <td>0.002150</td>\n      <td>-0.006031</td>\n      <td>0.001976</td>\n      <td>H</td>\n      <td>1.011731</td>\n      <td>1.463751</td>\n      <td>0.000277</td>\n      <td>1.783120</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>dsgdb9nsd_000001</td>\n      <td>1</td>\n      <td>3</td>\n      <td>2JHH</td>\n      <td>-11.2548</td>\n      <td>H</td>\n      <td>0.002150</td>\n      <td>-0.006031</td>\n      <td>0.001976</td>\n      <td>H</td>\n      <td>-0.540815</td>\n      <td>1.447527</td>\n      <td>-0.876644</td>\n      <td>1.783147</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>dsgdb9nsd_000001</td>\n      <td>1</td>\n      <td>4</td>\n      <td>2JHH</td>\n      <td>-11.2543</td>\n      <td>H</td>\n      <td>0.002150</td>\n      <td>-0.006031</td>\n      <td>0.001976</td>\n      <td>H</td>\n      <td>-0.523814</td>\n      <td>1.437933</td>\n      <td>0.906397</td>\n      <td>1.783157</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>dsgdb9nsd_000001</td>\n      <td>2</td>\n      <td>0</td>\n      <td>1JHC</td>\n      <td>84.8074</td>\n      <td>H</td>\n      <td>1.011731</td>\n      <td>1.463751</td>\n      <td>0.000277</td>\n      <td>C</td>\n      <td>-0.012698</td>\n      <td>1.085804</td>\n      <td>0.008001</td>\n      <td>1.091952</td>\n      <td>1</td>\n    </tr>\n  </tbody>\n</table>\n</div>"},"metadata":{}}]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.head()","execution_count":34,"outputs":[{"output_type":"execute_result","execution_count":34,"data":{"text/plain":"      molecule_name  atom_index_0     ...            z_1 dist_speedup\n0  dsgdb9nsd_000001             1     ...       0.008001     1.091953\n1  dsgdb9nsd_000001             1     ...       0.000277     1.783120\n2  dsgdb9nsd_000001             1     ...      -0.876644     1.783147\n3  dsgdb9nsd_000001             1     ...       0.906397     1.783157\n4  dsgdb9nsd_000001             2     ...       0.008001     1.091952\n\n[5 rows x 14 columns]","text/html":"<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>molecule_name</th>\n      <th>atom_index_0</th>\n      <th>atom_index_1</th>\n      <th>type</th>\n      <th>scalar_coupling_constant</th>\n      <th>atom_0</th>\n      <th>x_0</th>\n      <th>y_0</th>\n      <th>z_0</th>\n      <th>atom_1</th>\n      <th>x_1</th>\n      <th>y_1</th>\n      <th>z_1</th>\n      <th>dist_speedup</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>dsgdb9nsd_000001</td>\n      <td>1</td>\n      <td>0</td>\n      <td>1JHC</td>\n      <td>84.8076</td>\n      <td>H</td>\n      <td>0.002150</td>\n      <td>-0.006031</td>\n      <td>0.001976</td>\n      <td>C</td>\n      <td>-0.012698</td>\n      <td>1.085804</td>\n      <td>0.008001</td>\n      <td>1.091953</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>dsgdb9nsd_000001</td>\n      <td>1</td>\n      <td>2</td>\n      <td>2JHH</td>\n      <td>-11.2570</td>\n      <td>H</td>\n      <td>0.002150</td>\n      <td>-0.006031</td>\n      <td>0.001976</td>\n      <td>H</td>\n      <td>1.011731</td>\n      <td>1.463751</td>\n      <td>0.000277</td>\n      <td>1.783120</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>dsgdb9nsd_000001</td>\n      <td>1</td>\n      <td>3</td>\n      <td>2JHH</td>\n      <td>-11.2548</td>\n      <td>H</td>\n      <td>0.002150</td>\n      <td>-0.006031</td>\n      <td>0.001976</td>\n      <td>H</td>\n      <td>-0.540815</td>\n      <td>1.447527</td>\n      <td>-0.876644</td>\n      <td>1.783147</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>dsgdb9nsd_000001</td>\n      <td>1</td>\n      <td>4</td>\n      <td>2JHH</td>\n      <td>-11.2543</td>\n      <td>H</td>\n      <td>0.002150</td>\n      <td>-0.006031</td>\n      <td>0.001976</td>\n      <td>H</td>\n      <td>-0.523814</td>\n      <td>1.437933</td>\n      <td>0.906397</td>\n      <td>1.783157</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>dsgdb9nsd_000001</td>\n      <td>2</td>\n      <td>0</td>\n      <td>1JHC</td>\n      <td>84.8074</td>\n      <td>H</td>\n      <td>1.011731</td>\n      <td>1.463751</td>\n      <td>0.000277</td>\n      <td>C</td>\n      <td>-0.012698</td>\n      <td>1.085804</td>\n      <td>0.008001</td>\n      <td>1.091952</td>\n    </tr>\n  </tbody>\n</table>\n</div>"},"metadata":{}}]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.to_csv(\"MolecularProperties_train_v1.csv.gz\",compression=\"gzip\")\ntest.to_csv(\"MolecularProperties_test_v1.csv.gz\",compression=\"gzip\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## prep data for model running"},{"metadata":{"trusted":true},"cell_type":"code","source":"molecules = train.pop('molecule_name')\ntest = test.drop('molecule_name', axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train['fold'] = 0","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"n_splits = 3\ngkf = GroupKFold(n_splits=n_splits) # we're going to split folds by molecules\n\n\nfor fold, (in_index, oof_index) in enumerate(gkf.split(train, groups=molecules)):\n    train.loc[oof_index, 'fold'] = fold","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import h2o\nprint(h2o.__version__)\nfrom h2o.automl import H2OAutoML\n\nh2o.init(max_mem_size='14G')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train = h2o.H2OFrame(train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test = h2o.H2OFrame(test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train['type'] = train['type'].asfactor()\ntrain['atom_0'] = train['atom_0'].asfactor()\ntrain['atom_1'] = train['atom_1'].asfactor()\n\ntest['type'] = test['type'].asfactor()\ntest['atom_0'] = test['atom_0'].asfactor()\ntest['atom_1'] = test['atom_1'].asfactor()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"x = test.columns\ny = 'scalar_coupling_constant'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"aml = H2OAutoML(max_models=10, seed=47, max_runtime_secs=100) # 50 models,  max_runtime_secs=26000\naml.train(x=x, y=y, training_frame=train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# View the AutoML Leaderboard\nlb = aml.leaderboard\nlb.head()  # Print all rows instead of default (10 rows)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# The leader model is stored here\naml.leader","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"preds = aml.predict(test)\nsample_submission = pd.read_csv('../input/sample_submission.csv')\nsample_submission['scalar_coupling_constant'] = preds.as_data_frame().values.flatten()\nsample_submission.to_csv('h2o_submission_2.csv', index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"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.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}