{"cells":[{"metadata":{},"cell_type":"markdown","source":"# Naive Kernel for Magnetic Interaction Prediction\nThis is a work-in-progress kernel."},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","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)\nfrom sklearn.linear_model import HuberRegressor\nfrom sklearn import metrics\nfrom sklearn.preprocessing import PolynomialFeatures\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":1,"outputs":[{"output_type":"stream","text":"['test.csv', 'mulliken_charges.csv', 'dipole_moments.csv', 'train.csv', 'structures.csv', 'magnetic_shielding_tensors.csv', 'potential_energy.csv', 'sample_submission.csv', 'scalar_coupling_contributions.csv', 'structures']\n","name":"stdout"}]},{"metadata":{},"cell_type":"markdown","source":"## Load data sets"},{"metadata":{"trusted":true},"cell_type":"code","source":"trainSet = pd.read_csv('../input/train.csv')\ndisplay(trainSet.head())","execution_count":2,"outputs":[{"output_type":"display_data","data":{"text/plain":"   id           ...            scalar_coupling_constant\n0   0           ...                             84.8076\n1   1           ...                            -11.2570\n2   2           ...                            -11.2548\n3   3           ...                            -11.2543\n4   4           ...                             84.8074\n\n[5 rows x 6 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>id</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  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>0</td>\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>1</td>\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>2</td>\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>3</td>\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>4</td>\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":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"testSet = pd.read_csv('../input/test.csv')\ndisplay(testSet.head())","execution_count":3,"outputs":[{"output_type":"display_data","data":{"text/plain":"        id     molecule_name  atom_index_0  atom_index_1  type\n0  4658147  dsgdb9nsd_000004             2             0  2JHC\n1  4658148  dsgdb9nsd_000004             2             1  1JHC\n2  4658149  dsgdb9nsd_000004             2             3  3JHH\n3  4658150  dsgdb9nsd_000004             3             0  1JHC\n4  4658151  dsgdb9nsd_000004             3             1  2JHC","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>id</th>\n      <th>molecule_name</th>\n      <th>atom_index_0</th>\n      <th>atom_index_1</th>\n      <th>type</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>4658147</td>\n      <td>dsgdb9nsd_000004</td>\n      <td>2</td>\n      <td>0</td>\n      <td>2JHC</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>4658148</td>\n      <td>dsgdb9nsd_000004</td>\n      <td>2</td>\n      <td>1</td>\n      <td>1JHC</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>4658149</td>\n      <td>dsgdb9nsd_000004</td>\n      <td>2</td>\n      <td>3</td>\n      <td>3JHH</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>4658150</td>\n      <td>dsgdb9nsd_000004</td>\n      <td>3</td>\n      <td>0</td>\n      <td>1JHC</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>4658151</td>\n      <td>dsgdb9nsd_000004</td>\n      <td>3</td>\n      <td>1</td>\n      <td>2JHC</td>\n    </tr>\n  </tbody>\n</table>\n</div>"},"metadata":{}}]},{"metadata":{"trusted":true},"cell_type":"code","source":"structures = pd.read_csv('../input/structures.csv')\ndisplay(structures.head())","execution_count":4,"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":{},"cell_type":"markdown","source":"### Atomic distance\nhttps://www.kaggle.com/inversion/atomic-distance-benchmark/"},{"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\ntrainSet = map_atom_info(trainSet, 0)\ntrainSet = map_atom_info(trainSet, 1)\n\ntestSet = map_atom_info(testSet, 0)\ntestSet = map_atom_info(testSet, 1)\n","execution_count":5,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"display(trainSet.head())\ndisplay(testSet.head())","execution_count":6,"outputs":[{"output_type":"display_data","data":{"text/plain":"   id     molecule_name  atom_index_0    ...          x_1       y_1       z_1\n0   0  dsgdb9nsd_000001             1    ...    -0.012698  1.085804  0.008001\n1   1  dsgdb9nsd_000001             1    ...     1.011731  1.463751  0.000277\n2   2  dsgdb9nsd_000001             1    ...    -0.540815  1.447527 -0.876644\n3   3  dsgdb9nsd_000001             1    ...    -0.523814  1.437933  0.906397\n4   4  dsgdb9nsd_000001             2    ...    -0.012698  1.085804  0.008001\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>id</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>0</td>\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>1</td>\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>2</td>\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>3</td>\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>4</td>\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":{}},{"output_type":"display_data","data":{"text/plain":"        id     molecule_name  atom_index_0 ...        x_1  y_1  z_1\n0  4658147  dsgdb9nsd_000004             2 ...   0.599539  0.0  1.0\n1  4658148  dsgdb9nsd_000004             2 ...  -0.599539  0.0  1.0\n2  4658149  dsgdb9nsd_000004             2 ...   1.661639  0.0  1.0\n3  4658150  dsgdb9nsd_000004             3 ...   0.599539  0.0  1.0\n4  4658151  dsgdb9nsd_000004             3 ...  -0.599539  0.0  1.0\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>id</th>\n      <th>molecule_name</th>\n      <th>atom_index_0</th>\n      <th>atom_index_1</th>\n      <th>type</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>4658147</td>\n      <td>dsgdb9nsd_000004</td>\n      <td>2</td>\n      <td>0</td>\n      <td>2JHC</td>\n      <td>H</td>\n      <td>-1.661639</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>C</td>\n      <td>0.599539</td>\n      <td>0.0</td>\n      <td>1.0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>4658148</td>\n      <td>dsgdb9nsd_000004</td>\n      <td>2</td>\n      <td>1</td>\n      <td>1JHC</td>\n      <td>H</td>\n      <td>-1.661639</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>C</td>\n      <td>-0.599539</td>\n      <td>0.0</td>\n      <td>1.0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>4658149</td>\n      <td>dsgdb9nsd_000004</td>\n      <td>2</td>\n      <td>3</td>\n      <td>3JHH</td>\n      <td>H</td>\n      <td>-1.661639</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>H</td>\n      <td>1.661639</td>\n      <td>0.0</td>\n      <td>1.0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>4658150</td>\n      <td>dsgdb9nsd_000004</td>\n      <td>3</td>\n      <td>0</td>\n      <td>1JHC</td>\n      <td>H</td>\n      <td>1.661639</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>C</td>\n      <td>0.599539</td>\n      <td>0.0</td>\n      <td>1.0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>4658151</td>\n      <td>dsgdb9nsd_000004</td>\n      <td>3</td>\n      <td>1</td>\n      <td>2JHC</td>\n      <td>H</td>\n      <td>1.661639</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>C</td>\n      <td>-0.599539</td>\n      <td>0.0</td>\n      <td>1.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"},"metadata":{}}]},{"metadata":{"trusted":true},"cell_type":"code","source":"# https://www.kaggle.com/jazivxt/all-this-over-a-dog\n# https://www.kaggle.com/artgor/molecular-properties-eda-and-models\ntrain_p0 = trainSet[['x_0', 'y_0', 'z_0']].values\ntrain_p1 = trainSet[['x_1', 'y_1', 'z_1']].values\ntest_p0 = testSet[['x_0', 'y_0', 'z_0']].values\ntest_p1 = testSet[['x_1', 'y_1', 'z_1']].values\n\ntrainSet['dist'] = np.linalg.norm(train_p0 - train_p1, axis=1)\ntestSet['dist'] = np.linalg.norm(test_p0 - test_p1, axis=1)\n\ntrainSet['dist_to_type_mean'] = trainSet['dist'] / trainSet.groupby('type')['dist'].transform('mean')\ntestSet['dist_to_type_mean'] = testSet['dist'] / testSet.groupby('type')['dist'].transform('mean')","execution_count":7,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Atom types"},{"metadata":{"trusted":true},"cell_type":"code","source":"# All atom_0 are hydrogens\nassert all(trainSet[\"atom_0\"].astype('category').cat.categories == ['H'])\nassert all(testSet[\"atom_0\"].astype('category').cat.categories == ['H'])","execution_count":8,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# atom_1 are carbon, hydrogen or nitrogen\nprint(trainSet[\"atom_1\"].astype('category').cat.categories)\nprint(testSet[\"atom_1\"].astype('category').cat.categories)","execution_count":9,"outputs":[{"output_type":"stream","text":"Index(['C', 'H', 'N'], dtype='object')\nIndex(['C', 'H', 'N'], dtype='object')\n","name":"stdout"}]},{"metadata":{"trusted":true},"cell_type":"code","source":"# We use the interaction types, that already include the type of atoms involved\nprint(testSet[\"type\"].astype('category').cat.categories)\nprint(trainSet[\"type\"].astype('category').cat.categories)","execution_count":10,"outputs":[{"output_type":"stream","text":"Index(['1JHC', '1JHN', '2JHC', '2JHH', '2JHN', '3JHC', '3JHH', '3JHN'], dtype='object')\nIndex(['1JHC', '1JHN', '2JHC', '2JHH', '2JHN', '3JHC', '3JHH', '3JHN'], dtype='object')\n","name":"stdout"}]},{"metadata":{"trusted":true},"cell_type":"code","source":"for i in trainSet[\"type\"].astype('category').cat.categories.values:\n    trainSet['type_'+str(i)] = (trainSet['type'] == i)\n    testSet['type_'+str(i)] = (testSet['type'] == i)","execution_count":11,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Huber regression\nRobust linear regression (tries to ignore outliers)"},{"metadata":{"trusted":true},"cell_type":"code","source":"model = HuberRegressor()","execution_count":12,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Features to include (regressors)\nregressors = ['type_1JHC', 'type_1JHN', 'type_2JHC', 'type_2JHH', 'type_2JHN', \n                                       'type_3JHC', 'type_3JHH', 'dist', 'dist_to_type_mean']","execution_count":13,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Add bias, interaction term and quadratic and cubic terms\npolyFeat = PolynomialFeatures(degree=3, interaction_only=False, include_bias=True)","execution_count":14,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"trainX = polyFeat.fit_transform(np.array(trainSet[regressors]))","execution_count":15,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Some features are uninformative:\n# Interaction type features don't (statistically) interact as they are mutually exclusive\nusefulFeatures = [i for i,x in enumerate(np.abs(np.sum(trainX, axis = 0))) if x > 0]\ntrainX = trainX[:,usefulFeatures]\ntrainX.shape","execution_count":17,"outputs":[{"output_type":"execute_result","execution_count":17,"data":{"text/plain":"(4658147, 80)"},"metadata":{}}]},{"metadata":{"trusted":true},"cell_type":"code","source":"# NB: no need to include type_3JHN as this is redundant: this is always true when all other types are false\nfitDist = model.fit(trainX, \n                    trainSet['scalar_coupling_constant'])","execution_count":18,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Display factors to learn what is important for the prediction\nfitDist.coef_","execution_count":19,"outputs":[{"output_type":"execute_result","execution_count":19,"data":{"text/plain":"array([ 3.63569422,  5.84504608,  2.09238279, -1.8242938 , -1.74029367,\n       -0.3457709 ,  0.16912838,  0.69360345,  0.05552272,  3.97611851,\n        5.84504608,  6.11673802,  5.59679817,  2.09238279,  2.09527947,\n        2.06866565, -1.8242938 , -2.17663709, -0.99371819, -1.74029367,\n       -2.67520433, -1.5072463 , -0.3457709 , -0.89100497, -0.41715252,\n        0.16912838, -0.09581594, -0.03111868,  0.69360345,  0.46723723,\n        0.17291493, -4.93992349,  2.12156263,  4.75937427,  5.84504608,\n        6.11673802,  5.59679817,  6.39253145,  5.84914839,  5.35195441,\n        2.09238279,  2.09527947,  2.06866565,  2.09795829,  2.07131044,\n        2.04500106, -1.8242938 , -2.17663709, -0.99371819, -0.6657286 ,\n       -0.3039306 , -0.13875596, -1.74029367, -2.67520433, -1.5072463 ,\n       -3.99272563, -2.24955562, -1.26743006, -0.3457709 , -0.89100497,\n       -0.41715252, -2.22822347, -1.0432142 , -0.48841415,  0.16912838,\n       -0.09581594, -0.03111868,  0.33275323,  0.10807014,  0.03509855,\n        0.69360345,  0.46723723,  0.17291493, -1.3244239 , -0.49014216,\n       -0.18139157, -1.08722623,  3.50274157,  5.31903243,  5.94282798])"},"metadata":{}}]},{"metadata":{},"cell_type":"markdown","source":"### Evaluate performance\n"},{"metadata":{"trusted":true},"cell_type":"code","source":"# See https://www.kaggle.com/uberkinder/efficient-metric\ndef group_mean_log_mae(y_true, y_pred, types, floor=1e-9):\n    maes = (y_true-y_pred).abs().groupby(types).mean()\n    return np.log(maes.map(lambda x: max(x, floor))).mean()","execution_count":22,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"group_mean_log_mae(trainSet['scalar_coupling_constant'], \n                   model.predict(trainX), trainSet['type'])","execution_count":23,"outputs":[{"output_type":"execute_result","execution_count":23,"data":{"text/plain":"1.1736439116975004"},"metadata":{}}]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Control: this should perform better than outputing the same overfitted value for all interactions\nprint(group_mean_log_mae(trainSet['scalar_coupling_constant'], trainSet['scalar_coupling_constant'].median(), trainSet['type']))\nprint(group_mean_log_mae(trainSet['scalar_coupling_constant'], 0.85, trainSet['type']))","execution_count":24,"outputs":[{"output_type":"stream","text":"1.9912352863119418\n1.9398503397862858\n","name":"stdout"}]},{"metadata":{"trusted":true},"cell_type":"code","source":"testX = polyFeat.transform(np.array(testSet[regressors]))[:,usefulFeatures]\nresultSet = pd.DataFrame( { \"id\" : testSet['id'],\n                            \"scalar_coupling_constant\" : model.predict(testX)} )","execution_count":27,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Export results"},{"metadata":{"trusted":true},"cell_type":"code","source":"resultSet.to_csv(\"results.csv\", index = False, header = True)","execution_count":28,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Check content of the output file\nwith open(\"results.csv\", \"r\") as f:\n    for i, line in enumerate(f):\n        print(line)\n        if i > 5:\n            break","execution_count":29,"outputs":[{"output_type":"stream","text":"id,scalar_coupling_constant\n\n4658147,-0.06845633415777419\n\n4658148,85.34186653935089\n\n4658149,7.898494382693768\n\n4658150,85.34186653935089\n\n4658151,-0.06845633415777419\n\n4658152,89.46557882396796\n\n","name":"stdout"}]}],"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}