{"cells":[{"metadata":{},"cell_type":"markdown","source":"# Predicting Molecular Properties"},{"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)\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nimport joblib\nfrom sklearn.model_selection import StratifiedKFold, GroupKFold, train_test_split\nfrom sklearn.preprocessing import LabelEncoder, MinMaxScaler, StandardScaler\nfrom sklearn.linear_model import LinearRegression, SGDRegressor\nfrom sklearn import metrics\nimport lightgbm as lgb\nfrom xgboost import XGBRegressor\n# Input data files are available in the \"../input/\" directory.","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Loading the data"},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"train = pd.read_csv('../input/champs-scalar-coupling/train.csv')\ntest = pd.read_csv('../input/champs-scalar-coupling/test.csv')\nstructures = pd.read_csv('../input/champs-scalar-coupling/structures.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print('Train shape: {}'.format(train.shape))\nprint('Test shape: {}'.format(test.shape))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## EDA"},{"metadata":{"trusted":true},"cell_type":"code","source":"train.info()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test.info()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.describe()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train[['molecule_name','scalar_coupling_constant']].groupby('molecule_name').mean()[:100]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train[['type','scalar_coupling_constant']].groupby('type').count()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.distplot(train['scalar_coupling_constant'])","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"> "},{"metadata":{},"cell_type":"markdown","source":"## Feature Engineering"},{"metadata":{"trusted":true},"cell_type":"code","source":"train = pd.merge(train, structures, how = 'left',left_on  = ['molecule_name', 'atom_index_0'],\nright_on = ['molecule_name',  'atom_index'])\n\ntrain = pd.merge(train, structures, how = 'left',left_on  = ['molecule_name', 'atom_index_1'],\nright_on = ['molecule_name',  'atom_index'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test = pd.merge(test, structures, how = 'left',left_on  = ['molecule_name', 'atom_index_0'],\nright_on = ['molecule_name',  'atom_index'])\n\ntest = pd.merge(test, structures, how = 'left',left_on  = ['molecule_name', 'atom_index_1'],\nright_on = ['molecule_name',  'atom_index'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train['dist'] = ((train['x_y'] - train['x_x'])**2 + (train['y_y'] - train['y_x'])**2 + \n(train['z_y'] - train['z_x'])**2 ) ** 0.5\n\ntest['dist'] = ((test['x_y'] - test['x_x'])**2 + (test['y_y'] - test['y_x'])**2 +\n(test['z_y'] - test['z_x'])**2 ) ** 0.5","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"> ## Modeling"},{"metadata":{"trusted":true},"cell_type":"code","source":"train.columns","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"features = ['atom_index_x', 'x_x', 'y_x','z_x', 'atom_index_y', 'x_y', 'y_y', 'z_y', 'dist']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_train,X_val,y_train,y_val = train_test_split(train[features], train['scalar_coupling_constant'],test_size=0.2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"xgb = XGBRegressor()\nxgb.fit(X_train,y_train)\npreds = xgb.predict(X_val)\njoblib.dump(xgb, 'xgb_model.pkl')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"np.log(metrics.mean_absolute_error(y_val,preds))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_predictions = xgb.predict(test[features])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.distplot(test_predictions)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Looks like the distribution of the scalar coupling constant in the train dataset."},{"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,"_kg_hide-input":false,"_kg_hide-output":true},"cell_type":"code","source":"submission.to_csv('usingXGBoost.csv.gz',index=False,compression='gzip')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"This is my first competition and first kernel on Kaggle. If you think I've made some mistakes or I can improve somewhere, please let me know. Thanks :)"},{"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":4}