{"cells":[{"metadata":{},"cell_type":"markdown","source":"##### This is my first kaggle. From 2 month ago, I'm starting to learn python and machine leaning. It would be great if you could give me any advice on improving this kernel. Thank you!"},{"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)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"#### Load data"},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"df_train = pd.read_csv('../input/champs-scalar-coupling/train.csv')\ndf_test = pd.read_csv('../input/champs-scalar-coupling/test.csv')\nstruectures = pd.read_csv('../input/champs-scalar-coupling/structures.csv')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"#### Check the sample_submission"},{"metadata":{"trusted":true},"cell_type":"code","source":"sample_submission = pd.read_csv('../input/champs-scalar-coupling/sample_submission.csv')\nsample_submission.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sample_submission.to_csv('submission.csv', index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"markdown","source":"#### EDA"},{"metadata":{"trusted":true},"cell_type":"code","source":"print(df_train.shape)\nprint(df_test.shape)\nprint(sample_submission.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(df_train.columns) \nprint('*'* 20)\nprint(df_test.columns)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train.info()\ndf_test.info()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train.head(10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_test.head(10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"struectures.head(10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_tain_test = pd.concat([df_train, df_test], axis = 0, sort=False)\nprint(df_tain_test.shape)\ndf_tain_test.describe()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_tain_test.describe(include='O')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from matplotlib import pyplot as plt\nimport seaborn as sns","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.kdeplot(df_train.scalar_coupling_constant, shade=True)\nplt.legend()\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#df_train['log_scalar_coupling_constant'] = np.log10(df_train['scalar_coupling_constant'] +1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#sns.kdeplot(df_train.log_scalar_coupling_constant, shade=True)\n#plt.legend()\n#plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.hist(df_train.atom_index_0, bins=12, histtype='step', normed=True, linewidth=2)\nplt.hist(df_train.atom_index_1, bins=12, histtype='step', normed=True, linewidth=2)\nplt.legend(['atom_index_0', 'atom_index_1'])\n\nplt.title('atom_index Distribution')\nplt.xlabel('atom_index')\nplt.ylabel('Frequency')\n\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train = pd.merge(\n    struectures,\n    df_train,  \n    left_on = ['molecule_name', 'atom_index'],\n    right_on= ['molecule_name', 'atom_index_0']\n)\n\ntest = pd.merge(\n    struectures,\n    df_test,  \n    left_on = ['molecule_name', 'atom_index'],\n    right_on= ['molecule_name', 'atom_index_0']\n)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.head(10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train = pd.merge(train,\n                 struectures,\n                 left_on=['molecule_name', 'atom_index_1'],\n                 right_on=['molecule_name', 'atom_index']\n                )\ntest = pd.merge(test,\n                 struectures,\n                 left_on=['molecule_name', 'atom_index_1'],\n                 right_on=['molecule_name', 'atom_index']\n                )","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":{"trusted":true},"cell_type":"code","source":"train = train.drop(['molecule_name', 'id', 'atom_index_x', 'atom_index_y'], axis =1)\ntest = test.drop(['molecule_name', 'atom_index_x', 'atom_index_y'], axis =1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.head(10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def atom_number(atom):\n    if atom == 'H':\n        return 0\n    elif atom == 'C':\n        return 1\n    elif atom == 'N':\n        return 2\n    elif atom == 'O':\n        return 3\n    elif atom == 'F':\n        return 4","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.atom_y = [atom_number(i) for i in train.atom_y]\ntrain.atom_x = [atom_number(i) for i in train.atom_x]\ntest.atom_y = [atom_number(i) for i in test.atom_y]\ntest.atom_x = [atom_number(i) for i in test.atom_x]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train = pd.get_dummies(train, columns=['type'], drop_first=True)\ntest = pd.get_dummies(test, columns=['type'], drop_first=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.head(10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train['distance'] = (\n    (train['x_y'] - train['x_x'])**2 + \n    (train['y_y'] - train['y_x'])**2 + \n    (train['z_y'] - train['z_x'])**2 \n) ** 0.5\n\ntest['distance'] = (\n    (train['x_y'] - train['x_x'])**2 + \n    (train['y_y'] - train['y_x'])**2 + \n    (train['z_y'] - train['z_x'])**2 \n) ** 0.5","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_train = train.drop(['scalar_coupling_constant',], axis=1)\ny_train = train.scalar_coupling_constant","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.model_selection import GroupKFold, train_test_split\nfrom sklearn.metrics import accuracy_score\n\nX_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.2, random_state=42)\nX_train.shape, X_val.shape, y_train.shape, y_val.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from lightgbm import LGBMRegressor","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"lgb = LGBMRegressor()\nlgb.fit(X_train, y_train, \n        eval_set=[(X_val, y_val)],\n       early_stopping_rounds=100,\n       verbose=10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"preds = lgb.predict(X_val)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_predictions = lgb.predict(test[['atom_x',\n                                    'x_x',\n                                    'y_x',\n                                    'z_x',\n                                    'atom_index_0',\n                                    'atom_index_1',\n                                    'atom_y',\n                                    'x_y',\n                                    'y_y',\n                                    'z_y',\n                                    'type_1JHN',\n                                    'type_2JHC',\n                                    'type_2JHH',\n                                    'type_2JHN',\n                                    'type_3JHC',\n                                    'type_3JHH',\n                                    'type_3JHN',\n                                    'distance']]\n                              )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.distplot(test_predictions)\nplt.legend()\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission = pd.DataFrame()\nsubmission['id'] = test['id']\nsubmission['scalar_coupling_constant'] = test_predictions","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission.to_csv('first_sybmission.csv',index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":1}