{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n\nimport os\nimport random\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom xgboost.sklearn import XGBRegressor\nfrom sklearn.pipeline import Pipeline\nfrom sklearn.model_selection import cross_val_score, GridSearchCV\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Config"},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"SEED = 31\nFOLDS = 3\nN_ESTIMATORS = 4000\nTARGET = 'scalar_coupling_constant'\nPREDICTORS = [\n    'molecule_atom_index_0_dist_mean_div',\n    'molecule_atom_index_0_dist_max_div',\n    'molecule_atom_index_1_dist_max_div',\n    'molecule_atom_index_0_dist_std_div',\n    'molecule_atom_index_0_dist_min_div',\n    'molecule_atom_index_1_dist_mean_div',\n    'molecule_atom_index_1_dist_std_div',\n    'molecule_atom_1_dist_std_diff',\n    'molecule_atom_index_0_dist_std_diff',\n    'molecule_atom_index_0_dist_mean_diff',\n    'molecule_atom_index_1_dist_max_diff',\n    'molecule_atom_index_0_dist_max_diff',\n    'molecule_type_0_dist_std_diff',\n    'molecule_atom_index_1_dist_mean_diff',\n    'molecule_atom_index_1_dist_std_diff',\n    'molecule_atom_1_dist_min_div',\n    'molecule_atom_1_dist_min_diff',\n    'type_0',\n    'type_1',\n    'molecule_type_dist_min',\n    'molecule_type_dist_mean',\n    'molecule_type_0_dist_std',\n    'dist_to_type_1_mean',\n    'dist',\n    'molecule_type_dist_max',\n    'dist_x',\n    'dist_y',\n    'dist_z'\n]\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def seed_everything(seed):\n    random.seed(seed)\n    os.environ['PYTHONHASHSEED'] = str(seed)\n    np.random.seed(seed)\n    \nseed_everything(SEED)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"file_folder = '../input/champs-scalar-coupling-preprocess'\ntrain = pd.read_csv(f'{file_folder}/train.csv')\ntest = pd.read_csv(f'{file_folder}/test.csv')\nprint('train={}, test={}'.format(repr(train.shape), repr(test.shape)))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Eval function"},{"metadata":{"trusted":true},"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    maes = np.log(maes.map(lambda x: max(x, floor)))\n    print(maes)\n    return maes.mean()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"%%time\n\ny_train = train[TARGET]\nx_train = train[PREDICTORS]\nmodel = XGBRegressor(\n    n_estimators = N_ESTIMATORS,\n    learning_rate = 0.1,\n    subsample = 1,\n    colsample_bytree = 0.75,\n    gamma = 0.01,\n    reg_alpha = 1,\n    reg_lambda = 0,\n    tree_method = 'hist'\n)\npipe = Pipeline([('model', model)])\nparam_grid = {\n    'model__max_depth': [8]\n}\ncv = GridSearchCV(pipe, cv=FOLDS, param_grid=param_grid, scoring='neg_mean_absolute_error')\ncv.fit(x_train, y_train)\nprint('best_params_={}\\nbest_score_={}'.format(repr(cv.best_params_), repr(cv.best_score_)))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y_pred_train = cv.predict(x_train)\ngmlm = group_mean_log_mae(y_train, y_pred_train, train['type'])\nprint('group_mean_log_mae={}'.format(gmlm))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"x_test = test[PREDICTORS]\n# Use the model to make predictions\npreds = cv.predict(x_test)\nprint(preds)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission = pd.DataFrame({'id': test['id'], 'scalar_coupling_constant': preds})\nsubmission.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission.to_csv('submission.csv', index=False)\ntrain = pd.DataFrame({'id': train['id'], 'type': train['type'], TARGET: train[TARGET], 'pred': y_pred_train})\ntrain.to_csv('train.csv', index=False)\nprint(os.listdir(\".\"))","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}