{"cells":[{"metadata":{"trusted":false},"cell_type":"code","source":"import gc\nimport pandas as pd\nimport numpy as np\nfrom scipy.sparse import csr_matrix\nfrom sklearn.preprocessing import LabelEncoder, StandardScaler\nfrom scipy.sparse import csr_matrix\nfrom sklearn.decomposition import TruncatedSVD\nimport lightgbm as lgb\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.model_selection import StratifiedKFold, KFold, RepeatedKFold\nfrom sklearn import metrics\n# Graph Stuff\nimport matplotlib.pyplot as plt\n%matplotlib inline\nimport seaborn as sns","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"df = pd.read_csv('../input/train.csv')\ndf.atom_index_0+=1\ndf.atom_index_1+=1","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"df.molecule_name.value_counts().tail()","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"trainmolecules = df.molecule_name.unique()\nle = LabelEncoder()\ndf.type= le.fit_transform(df.type)+1\ndf.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"x = df.atom_index_0-df.atom_index_1\nprint(x.min())\nprint(x.max())","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"df_structures = pd.read_csv('../input/structures.csv')\ndf_structures.atom_index+=1\ndf_structures.head(20)","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"le = LabelEncoder()\ndf_structures.atom = le.fit_transform(df_structures.atom)+1","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"df_structures.head(20)","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"z = None\nfor i in np.arange(0,5,1):\n    print(i)\n    if(z is None):\n        z = df[['molecule_name','atom_index_0','atom_index_1','type','scalar_coupling_constant']].copy()\n    y = df_structures.copy()\n    y.atom_index -= i\n   \n    z = z.merge(y, how='left',left_on=['molecule_name','atom_index_0'], right_on=['molecule_name','atom_index'],suffixes=('', '_neg_'+str(i)))\n    if(z.columns.contains('atom_index_neg_'+str(i))):\n        del z['atom_index_neg_'+str(i)]\n\n    if(i!=0):\n        z['dist_neg_'+str(i)] = 1./np.linalg.norm(z[['x', 'y', 'z']].values - z[['x_neg_'+str(i), 'y_neg_'+str(i), 'z_neg_'+str(i)]].values, axis=1)\n        del z['x_neg_'+str(i)]\n        del z['y_neg_'+str(i)]\n        del z['z_neg_'+str(i)]\n\n    \nfor i in np.arange(1,5,1):\n    print(i)\n    \n    y = df_structures.copy()\n    y.atom_index += i\n\n    \n    z = z.merge(y, how='left',left_on=['molecule_name','atom_index_0'], right_on=['molecule_name','atom_index'],suffixes=('', '_pos_'+str(i)))    \n    if(z.columns.contains('atom_index')):\n        del z['atom_index']\n    if(z.columns.contains('atom_index_pos_'+str(i))):\n        del z['atom_index_pos_'+str(i)]\n    z['dist_pos_'+str(i)] = 1./np.linalg.norm(z[['x', 'y', 'z']].values - z[['x_pos_'+str(i), 'y_pos_'+str(i), 'z_pos_'+str(i)]].values, axis=1)\n    del z['x_pos_'+str(i)]\n    del z['y_pos_'+str(i)]\n    del z['z_pos_'+str(i)]\ndel z['x']\ndel z['y']\ndel z['z']    \n\nfor i in np.arange(0,5,1):\n    print(i)\n    y = df_structures.copy()\n    y.atom_index -= i\n   \n    z = z.merge(y, how='left',left_on=['molecule_name','atom_index_1'], right_on=['molecule_name','atom_index'],suffixes=('', '_1_neg_'+str(i)))\n    if(z.columns.contains('atom_index_1_neg_'+str(i))):\n        del z['atom_index_1_neg_'+str(i)]\n\n    if(i!=0):\n        z['dist_1_neg_'+str(i)] = 1./np.linalg.norm(z[['x', 'y', 'z']].values - z[['x_1_neg_'+str(i), 'y_1_neg_'+str(i), 'z_1_neg_'+str(i)]].values, axis=1)\n        del z['x_1_neg_'+str(i)]\n        del z['y_1_neg_'+str(i)]\n        del z['z_1_neg_'+str(i)]\n\n    \nfor i in np.arange(1,5,1):\n    print(i)\n\n    y = df_structures.copy()\n    y.atom_index += i\n   \n    z = z.merge(y, how='left',left_on=['molecule_name','atom_index_1'], right_on=['molecule_name','atom_index'],suffixes=('', '_1_pos_'+str(i)))    \n    if(z.columns.contains('atom_index')):\n        del z['atom_index']\n    if(z.columns.contains('atom_index_1_pos_'+str(i))):\n        del z['atom_index_1_pos_'+str(i)]\n    z['dist_1_pos_'+str(i)] = 1./np.linalg.norm(z[['x', 'y', 'z']].values - z[['x_1_pos_'+str(i), 'y_1_pos_'+str(i), 'z_1_pos_'+str(i)]].values, axis=1)\n    del z['x_1_pos_'+str(i)]\n    del z['y_1_pos_'+str(i)]\n    del z['z_1_pos_'+str(i)]    \n  \ndel z['x']\ndel z['y']\ndel z['z']","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"df.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"z.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"z.dropna(axis = 1, how ='all', inplace = True)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"sc = z.scalar_coupling_constant.values\ndel z['scalar_coupling_constant']\nz['scalar_coupling_constant'] = sc","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"def group_mean_log_mae(y_true, y_pred, types, floor=1e-9):\n    \"\"\"\n    Fast metric computation for this competition: https://www.kaggle.com/c/champs-scalar-coupling\n    Code is from this kernel: https://www.kaggle.com/uberkinder/efficient-metric\n    \"\"\"\n    maes = (y_true-y_pred).abs().groupby(types).mean()\n    return np.log(maes.map(lambda x: max(x, floor))).mean()","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"n_fold = 5\nfolds = KFold(n_splits=n_fold, shuffle=True, random_state=11)","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"df = z","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"feats = df.columns[1:-1]\nX = csr_matrix(df[feats].fillna(0).values)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"y = df.scalar_coupling_constant.values","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"params = {'num_leaves': 128,\n          'min_child_samples': 79,\n          'objective': 'regression',\n          'max_depth': -1,\n          'learning_rate': 0.2,\n          \"boosting_type\": \"gbdt\",\n          \"subsample_freq\": 1,\n          \"subsample\": 0.9,\n          \"bagging_seed\": 11,\n          \"metric\": 'mae',\n          \"verbosity\": -1,\n          'reg_alpha': 0.1,\n          'reg_lambda': 0.3,\n          'colsample_bytree': 1.0\n          \n         }\n\n# out-of-fold predictions on train data\noof = np.zeros(X.shape[0])\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"for fold_n, (train_index, valid_index) in enumerate(folds.split(X)):\n    print(fold_n)\n    X_train, X_valid = X[train_index], X[valid_index]\n    y_train, y_valid = y[train_index], y[valid_index]\n    model = lgb.LGBMRegressor(**params, n_estimators = 1500)\n    model.fit(X_train, y_train, \n            eval_set=[(X_train, y_train), (X_valid, y_valid)], eval_metric='mae',\n            verbose=500, early_stopping_rounds=200)\n\n    y_pred_valid = model.predict(X_valid)\n    oof[valid_index] = y_pred_valid.reshape(-1,)\n    break","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"x = pd.DataFrame()\nx['target'] = y\nx['predictions'] = oof\nx['type'] = df.type.values\nx = x[x.predictions!=0]\n\ngroup_mean_log_mae(x.target, x.predictions, x.type, floor=1e-9)","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"plot_data = pd.DataFrame()\nplot_data['yhat'] = x['predictions'].values\nplot_data['y'] = x['target'].values\nplot_data['type'] = x['type'].values","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for t in sorted([1, 4, 2, 5, 3, 7, 6, 8]):\n    plt.scatter(plot_data[plot_data.type==t].y,\n                plot_data[plot_data.type==t].yhat,s=1)\n    plt.show()","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.7.3"}},"nbformat":4,"nbformat_minor":1}