{"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 sys\nimport 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":{"trusted":true},"cell_type":"code","source":"SEED = 31\nTRIALS = 200\nTARGET = 'scalar_coupling_constant'\nPREDICTION = 'pred'","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":{},"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    return maes.mean()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"lasso = pd.read_csv(f'../input/champs-scalar-coupling-lasso/submission.csv')\nrf = pd.read_csv(f'../input/champs-scalar-coupling-rf/submission.csv')\nxgb = pd.read_csv(f'../input/champs-scalar-coupling-xgb/submission.csv')\nlgb = pd.read_csv(f'../input/champs-scalar-coupling-lgb/submission.csv')\nkeras = pd.read_csv(f'../input/champs-scalar-coupling-keras/submission.csv')\ntest_sets = [lgb, keras, xgb, rf, lasso]\nprint(f'lasso={lasso.shape}, rf={rf.shape}, xgb={xgb.shape}, lgb={lgb.shape}, keras={keras.shape}')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"lasso_train = pd.read_csv(f'../input/champs-scalar-coupling-lasso/train.csv')\nrf_train = pd.read_csv(f'../input/champs-scalar-coupling-rf/train.csv')\nxgb_train = pd.read_csv(f'../input/champs-scalar-coupling-xgb/train.csv')\nlgb_train = pd.read_csv(f'../input/champs-scalar-coupling-lgb/train.csv')\nkeras_train = pd.read_csv(f'../input/champs-scalar-coupling-keras/train.csv')\ntrain_sets = [lgb_train, keras_train, xgb_train, rf_train, lasso_train]\nprint(f'Train sets\\nlasso={lasso_train.shape}, rf={rf_train.shape}, xgb={xgb_train.shape}, lgb={lgb_train.shape}, keras={keras_train.shape}')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Trials"},{"metadata":{"trusted":true},"cell_type":"code","source":"%%time\n\ndef weights(n, min_weight=0.01, max_allocation=0.5):\n    if n < 1:\n        raise ValueError('n must not be less than 1')\n    remainder = 1 - (n * min_weight)\n    if remainder <= 0:\n        raise ValueError('min weight exceeds budget of 1')\n    res = []\n    for _ in range(n - 1):\n        a = random.uniform(0.01, max_allocation) * remainder\n        res.append(a + min_weight)\n        remainder -= a\n    res.append(remainder + min_weight)\n    return res\n\n\ndef trial(train_sets, prediction_column, target_column):\n    ws = weights(len(train_sets), min_weight=0.05, max_allocation=0.9)\n    df = train_sets[0].copy()\n    df[prediction_column] = 0\n    for i, t in enumerate(train_sets):\n        df[prediction_column] += t[prediction_column] * ws[i]\n    score = group_mean_log_mae(df[target_column], df[prediction_column], df['type'])\n    return score, ws\n\n\nbest = sys.maxsize\nbest_weights = []\nfor _ in range(TRIALS):\n    score, ws = trial(train_sets=train_sets, prediction_column=PREDICTION, target_column=TARGET)\n    if score < best:\n        best = score\n        best_weights = ws\n        \nprint(f'best={best:.4f}')\nprint(f'''best weights (sum={sum(best_weights)})\n  lgb={best_weights[0]:.4f}\n  keras={best_weights[1]:.4f}\n  xgb={best_weights[2]:.4f}\n  rf={best_weights[3]:.4f}\n  lasso={best_weights[4]:.4f}\n''')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission = test_sets[0].copy()\nsubmission[TARGET] = 0\nfor i, t in enumerate(test_sets):\n    submission[TARGET] += t[TARGET] * best_weights[i]\nsubmission.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission.to_csv('submission.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}