{"cells":[{"metadata":{},"cell_type":"markdown","source":"* **General Information**\n\nDevelop an algorithm that can predict the magnetic interaction between two atoms in a molecule (i.e., the scalar coupling constant)."},{"metadata":{},"cell_type":"markdown","source":"**Importing Libraries**"},{"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)\n\nimport gc\nimport os\n\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\nimport lightgbm as lgb\nfrom sklearn.model_selection import cross_val_score\nfrom sklearn.linear_model import LinearRegression\nfrom sklearn.model_selection import train_test_split\nfrom sklearn import metrics\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"* **Accessing Data and Preperaing Data**"},{"metadata":{"trusted":true},"cell_type":"code","source":"print(os.listdir(\"../input\"))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"We will be using train.csv, test.csv and structures.csv files for our analysis and model preperation.\n\nLets load those files in dataframe."},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","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":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Lets have a look at some records from each dataframe"},{"metadata":{"trusted":true},"cell_type":"code","source":"train_original.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"structures_original.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_original.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"How many items are there in dsgdb9nsd_000015?"},{"metadata":{"trusted":true},"cell_type":"code","source":"structures_original[structures_original['molecule_name'] == 'dsgdb9nsd_000015']","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"OK.\n\nAs per above there are total 9 items in molecule dsgdb9nsd_000015.\n\nCarbon - 2\n\nOxygen - 1\n\nHydrogen - 6\n\nTotal 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":{"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":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"moleculeCount[moleculeCount['molecule_name'] == 'dsgdb9nsd_000015']","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Merge moleculeCount dataframe in original structures dataframe.So tha later we can use that for enriching train and test data."},{"metadata":{"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":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Join structures dataframe with train and test data to include item counts in train and test data."},{"metadata":{"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.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"tmp_merge = pd.DataFrame.merge(test_original,structures\n                               ,how='inner'\n                               ,left_on = ['molecule_name','atom_index_0'] \n                               ,right_on = ['molecule_name','atom_index']\n                              )\ntmp_merge = tmp_merge.merge(structures\n                ,how='inner'\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\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']]\n\n\ntest.sort_values(by=['id','molecule_name'],inplace=True)\ntest.reset_index(inplace=True,drop=True)\n\ntmp_merge = None\n\ntest.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\ntrain_original = None\ndel train_original\nstructures_original = None\ndel structures_original\ntest_original = None\ndel test_original\nstructures = None\ndel structures\ngc.collect()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"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\n||A||F = [\\sum{i,j} abs(a_{i,j})^2]^{1/2}"},{"metadata":{"trusted":true},"cell_type":"code","source":"train['dist'] = np.linalg.norm(train[['x_0', 'y_0', 'z_0']].values - train[['x_1', 'y_1', 'z_1']].values, axis=1)\ntest['dist'] = np.linalg.norm(test[['x_0', 'y_0', 'z_0']].values - test[['x_1', 'y_1', 'z_1']].values, axis=1)\n\ntrain.drop(columns=['x_0', 'y_0', 'z_0','x_1', 'y_1', 'z_1'],inplace=True)\ntest.drop(columns=['x_0', 'y_0', 'z_0','x_1', 'y_1', 'z_1'],inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train['type'] = pd.Categorical(train['type'])\ntrain['atom_nm_1'] = pd.Categorical(train['atom_nm_1'])\ntest['type'] = pd.Categorical(test['type'])\ntest['atom_nm_1'] = pd.Categorical(test['atom_nm_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":"test.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"* **Model**"},{"metadata":{"trusted":true},"cell_type":"code","source":"X = train[['atom_0' ,  'atom_1' , 'type', 'atom_nm_1', 'C' ,  'F' ,  'H' ,  'N' ,  'O' , 'dist' ]]\n\ny = train['scalar_coupling_constant']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(\n    X, y, test_size=0.4, random_state=420)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"lgb_train = lgb.Dataset(X_train,y_train,free_raw_data=True)\nlgb_eval = lgb.Dataset(X_test,y_test,free_raw_data=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"params = {\n    'boosting_type': 'gbdt',\n    'objective': 'regression',\n    'learning_rate': 0.05,\n    'num_leaves': 50, \n    'feature_fraction': 0.9,\n    'bagging_fraction': 0.8,\n    'bagging_freq': 5,\n    'verbose': 0,\n    'num_boost_round':5000,\n    'reg_alpha': 0.1,\n    'reg_lambda': 0.3,\n'early_stopping_rounds':5\n         }","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"gbm = lgb.train(\n    params,\n    lgb_train,\n\n    valid_sets=lgb_eval\n)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y_predict = gbm.predict(X_test)\nmse = np.sqrt(metrics.mean_squared_error(y_predict,y_test))\n\nprint('Mean Squared Error is : '+str(mse))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"* **Sumbission:**"},{"metadata":{"trusted":true},"cell_type":"code","source":"submission_df = pd.DataFrame(columns=['id', 'scalar_coupling_constant'])\nsubmission_df['id'] = test['id']\nsubmission_df['scalar_coupling_constant'] = gbm.predict(test[['atom_0' ,  'atom_1' , 'type', 'atom_nm_1', 'C' ,  'F' ,  'H' ,  'N' ,  'O' , 'dist' ]])\nsubmission_df.to_csv('submissions.csv', header=True, index=False)\nsubmission_df.head(10)","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}