{"cells":[{"metadata":{},"cell_type":"markdown","source":"Importng Libraries and Data Overview"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\nfrom IPython.display import HTML\nimport json\nimport altair as alt\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\n#%matplotlib inline\n#from tqdm import tqdm_notebook\n#from sklearn.preprocessing import StandardScaler\n#from sklearn.svm import NuSVR, SVR\n#from sklearn.metrics import mean_absolute_error\n#pd.options.display.precision = 15\n\n#import lightgbm as lgb\n#import xgboost as xgb\n#import time\n#import datetime\n#from catboost import CatBoostRegressor\n#from sklearn.preprocessing import LabelEncoder\n#from sklearn.model_selection import StratifiedKFold, KFold, RepeatedKFold\n#from sklearn import metrics\n#from sklearn import linear_model\n#import gc\n\n#import warnings\n#warnings.filterwarnings(\"ignore\")\n#\n#\n#import networkx as nx\n\n#alt.renderers.enable('notebook')\n\nfile_folder = '../input/champs-scalar-coupling' if 'champs-scalar-coupling' in os.listdir('../input/') else '../input'\nos.listdir(file_folder)\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Import files and see structure"},{"metadata":{"trusted":true},"cell_type":"code","source":"train = pd.read_csv(f'{file_folder}/train.csv')\ntest = pd.read_csv(f'{file_folder}/test.csv')\nsub = pd.read_csv(f'{file_folder}/sample_submission.csv')\nstructures = pd.read_csv(f'{file_folder}/structures.csv')\nscc = pd.read_csv(f'{file_folder}/scalar_coupling_contributions.csv')\n\ntrain.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"structures.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"scc.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(f\"Taining Data Set \\n{train.nunique()}\\n\\n\")\nprint(f\"Unique atoms:{structures['atom'].nunique()}  {structures['atom'].unique()}\")\nprint(f\"Unique types: {train['type'].nunique()}  {train['type'].unique()}\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Add the structure of Atom in Train and Test Set"},{"metadata":{"trusted":true},"cell_type":"code","source":"def 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}','x': f'x_{atom_idx}','y': f'y_{atom_idx}','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)\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"*Calculate Distance between the atoms in a molecule*"},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"\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'] = np.linalg.norm(train_p_0 - train_p_1, axis=1)\ntest['dist'] = np.linalg.norm(test_p_0 - test_p_1, axis=1)\n\ngrid = sns.FacetGrid(train[['type', 'dist']], col='type', hue='type', col_wrap=4)\ngrid.map(sns.distplot, 'dist')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"We can clearly see that distance is almost fixed for 1J type since the atoms are neighours of each other. But for 2J & 3J, distance cannot be the only field to predict scaler coupling constant."},{"metadata":{},"cell_type":"markdown","source":"Add scaler coupling contribution to training dataset"},{"metadata":{"trusted":true},"cell_type":"code","source":"train = pd.merge(train, scc, how = 'left',\n                  left_on  = ['molecule_name', 'atom_index_0', 'atom_index_1', 'type'],\n                  right_on = ['molecule_name', 'atom_index_0', 'atom_index_1', 'type'])\nprint(\"\")\nprint(f\"{train['atom_0'].unique()}\")\nprint(f\"{test['atom_0'].unique()}\")\nprint(f\"{train['atom_1'].unique()}\")\nprint(f\"{test['atom_1'].unique()}\")\nprint(f\"Unique types: {test['type'].nunique()}  {test['type'].unique()}\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.scatter(train['fc'],train['scalar_coupling_constant'])\nplt.xlabel('fc')\nplt.ylabel('scalar_coupling_constant')\nplt.title('Correlation between SCC and fc')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"To check correlation between scaler coupling and fc i have used this [kernal](https://www.kaggle.com/artgor/using-meta-features-to-improve-model/notebook)"},{"metadata":{"trusted":true},"cell_type":"code","source":"fig, ax = plt.subplots(figsize = (20, 10))\nfor i, t in enumerate(train['type'].unique()):\n    plt.subplot(2, 4, i + 1);\n    plt.scatter(train.loc[train['type'] == t, 'fc'], train.loc[train['type'] == t, 'scalar_coupling_constant'], label=t);\n    plt.title(f'fc vs target \\n for {t} type');","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Wooo....\nWe can see a strong correlation between Scaler Coupling Constant and fc.**\n\n**We can create a training model containing fc**"},{"metadata":{"trusted":true},"cell_type":"code","source":"train_new=train[['id','molecule_name','atom_index_0','atom_index_1','type','scalar_coupling_constant','dist','fc']].copy()\ntrain_new.head()","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}