{"cells":[{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","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)\nimport os\nimport matplotlib.pylab as plt\nimport seaborn as sns\nfrom sklearn import metrics\nimport warnings\nwarnings.simplefilter(action='ignore', category=FutureWarning)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df = pd.read_csv('../input/train.csv')\ntest_df = pd.read_csv('../input/test.csv')\n\nprint('train_df.shape',train_df.shape)\nprint('test_df.shape',test_df.shape)\nprint('train_df.columns',train_df.columns.values)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\nstructures = pd.read_csv('../input/structures.csv')\nprint('structures.shape',structures.shape)\nprint('structures.columns',structures.columns.values)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print('train_df',train_df.shape)\ndisplay(train_df.head(2))\nprint('')\n\nprint('test_df',test_df.shape)\ndisplay(test_df.head(2))\nprint('')\n\nprint('structures',structures.shape)\ndisplay(structures.head(2))\nprint('')\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Show histogram of items in train.csv\n     From the histogram of scalar_coup_constant, there are peaks near zero and around 80.\nLooking at the type, there are many cases where hydrogen and carbon are taken as the target atom pair, and the ratio between hydrogen and nitrogen is small."},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.rcParams['font.size'] = 20\nfig, ax = plt.subplots(2, 2, figsize=(20, 10))\n\ntrain_df['scalar_coupling_constant'].plot(kind='hist',ax=ax.flat[0],  bins=1000, title='scalar_coupling_constant')\ntrain_df['type'].value_counts().plot(kind='bar',ax=ax.flat[1], title='type')\ntrain_df['atom_index_0'].plot(kind='hist',  bins=25, ax=ax.flat[2],title='atom_index_0')\ntrain_df['atom_index_1'].plot(kind='hist', bins=25, ax=ax.flat[3],title='atom_index_1')\nplt.tight_layout() \nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"The atomic data of the target atom and the atomic number information of the molecule are added to the train data."},{"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}',\n                            'x': f'x_{atom_idx}',\n                            'y': f'y_{atom_idx}',\n                            'z': f'z_{atom_idx}'})\n    return df\n\ntrain_df = map_atom_info(train_df, 0)\ntrain_df = map_atom_info(train_df, 1)\n\ntest_df = map_atom_info(test_df, 0)\ntest_df = map_atom_info(test_df, 1)\n\natom_count_dict = structures.groupby('molecule_name').count()['atom_index'].to_dict()\n\ntrain_df['atom_count'] = train_df['molecule_name'].map(atom_count_dict)\ntest_df['atom_count'] = test_df['molecule_name'].map(atom_count_dict)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print('train_df.columns',train_df.columns.values)\ntrain_df.head(3)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Position histogram of target atom of train  and test data\n"},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.rcParams['font.size'] = 20\nfig, ax = plt.subplots(2, 3, figsize=(20, 10))\ntrain_df['x_0'].plot(kind='hist', ax=ax.flat[0], bins=500, title='Atom_0_X')\ntrain_df['y_0'].plot(kind='hist', ax=ax.flat[1], bins=500, title='Atom_0_Y')\ntrain_df['z_0'].plot(kind='hist', ax=ax.flat[2], bins=500, title='Atom_0_Z')\ntrain_df['x_1'].plot(kind='hist', ax=ax.flat[3], bins=500, title='Atom_1_X')\ntrain_df['y_1'].plot(kind='hist', ax=ax.flat[4], bins=500, title='Atom_1_Y')\ntrain_df['z_1'].plot(kind='hist', ax=ax.flat[5], bins=500, title='Atom_1_Z')\nplt.tight_layout()\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.rcParams['font.size'] = 20\nfig, ax = plt.subplots(2, 3, figsize=(20, 10))\ntest_df['x_0'].plot(kind='hist', ax=ax.flat[0], bins=500, title='Atom_0_X')\ntest_df['y_0'].plot(kind='hist', ax=ax.flat[1], bins=500, title='Atom_0_Y')\ntest_df['z_0'].plot(kind='hist', ax=ax.flat[2], bins=500, title='Atom_0_Z')\ntest_df['x_1'].plot(kind='hist', ax=ax.flat[3], bins=500, title='Atom_1_X')\ntest_df['y_1'].plot(kind='hist', ax=ax.flat[4], bins=500, title='Atom_1_Y')\ntest_df['z_1'].plot(kind='hist', ax=ax.flat[5], bins=500, title='Atom_1_Z')\nplt.tight_layout()\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"atom_0 was all hydrogen. atom_1 was 80% or more carbon, and the others were hydrogen and nitrogen. Apart from these, the constituent elements of the molecules are oxygen and fluorine."},{"metadata":{"trusted":true},"cell_type":"code","source":"print('train atom_0:',train_df['atom_0'].value_counts())\nprint('test atom_0:',test_df['atom_0'].value_counts())\n\nprint('train atom_1:',train_df['atom_1'].value_counts())\nprint('test atom_1:',test_df['atom_1'].value_counts())\n\nplt.rcParams['font.size'] = 10\nfig, ax = plt.subplots(1, 2, figsize=(5, 3))\n\ntrain_df['atom_1'].value_counts().plot(kind='bar', ax=ax.flat[0], title='train atom_1')\ntest_df['atom_1'].value_counts().plot(kind='bar', ax=ax.flat[1],  title='test atom_1')\nplt.tight_layout()\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Atomic species contained in the molecule\nprint('structures_atom:',structures['atom'].value_counts())\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Histogram of all atom positions\n\nplt.rcParams['font.size'] = 10\n\nfig, ax = plt.subplots(1, 3, figsize=(10, 3))\nstructures['x'].plot(kind='hist', ax=ax.flat[0], bins=500, title='Atom_X')\nstructures['y'].plot(kind='hist', ax=ax.flat[1], bins=500, title='Atom_Y')\nstructures['z'].plot(kind='hist', ax=ax.flat[2], bins=500, title='Atom_Z')\n\nplt.tight_layout()\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Add distance information of atom0 and atom1 to train_df and test_df\n\ntrain_p_0 = train_df[['x_0', 'y_0', 'z_0']].values\ntrain_p_1 = train_df[['x_1', 'y_1', 'z_1']].values\ntest_p_0 = test_df[['x_0', 'y_0', 'z_0']].values\ntest_p_1 = test_df[['x_1', 'y_1', 'z_1']].values\n\ntrain_df['dist'] = np.linalg.norm(train_p_0 - train_p_1, axis=1)\ntest_df['dist'] = np.linalg.norm(test_p_0 - test_p_1, axis=1)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"It is a very interesting result. In 1JHC and 1JHN in which atoms of atom_0 and atom_1 are directly bonded to each other, the interatomic distance is almost constant, and the strength of interaction is largely different. It is thought that it is influenced by surrounding atoms. The other types differ greatly in distance, but their correlation with the magnitude of interaction is likely to be weak.\n"},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.pairplot(data=train_df.sample(5000), hue='type',\n             vars=['scalar_coupling_constant',  'dist']).savefig('seaborn_pairplot_vars.png')","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}