{"cells":[{"metadata":{},"cell_type":"markdown","source":"Thanks @DSEverything for https://www.kaggle.com/dongxu027/explore-stacking-lb-0-1463"},{"metadata":{"_cell_guid":"28a96b13-fb54-43af-aeb9-ccb40526ec1f","_uuid":"518b490c58f98a59d25d8d5d8c39dca0673564e1","trusted":true},"cell_type":"code","source":"import os\nimport numpy as np \nimport pandas as pd \nfrom subprocess import check_output\nimport seaborn as sns; sns.set()\nimport matplotlib.pyplot as plt\nprint(check_output([\"ls\", \"../input/\"]).decode(\"utf8\"))","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"1840df41-7524-4305-93d9-423a2a6fe5bd","_uuid":"aae1d396e2fad44a15fcb8e970c5affa8fb6ddc6"},"cell_type":"markdown","source":"# Data Load"},{"metadata":{"_kg_hide-output":true,"trusted":true},"cell_type":"code","source":"sub_path = \"../input/champstacks\"\nall_files = os.listdir(sub_path)\nall_files","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"c978c838-8a87-45ef-a118-b6bd565bdb2e","_uuid":"1fa9dd2434873dc5bc918b102a3a69649d0f35c7","trusted":true},"cell_type":"code","source":"# Read and concatenate submissions\nouts = [pd.read_csv(os.path.join(sub_path, f), index_col=0) for f in all_files]\nconcat_sub = pd.concat(outs, axis=1)\ncols = list(map(lambda x: \"champ\" + str(x), range(len(concat_sub.columns))))\nconcat_sub.columns = cols\nconcat_sub.reset_index(inplace=True)\nconcat_sub.head()\nncol = concat_sub.shape[1]","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"a193cac9-e544-4749-8622-2c0a7d882193","_uuid":"37b39229e34b71032d8d2371c6b731ed452b8eb2","trusted":true},"cell_type":"code","source":"# check correlation\nconcat_sub.iloc[:,1:ncol].corr()","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"corr = concat_sub.iloc[:,1:7].corr()\nmask = np.zeros_like(corr, dtype=np.bool)\nmask[np.triu_indices_from(mask)] = True\n\n# Set up the matplotlib figure\nf, ax = plt.subplots(figsize=(11, 9))\n\n# Generate a custom diverging colormap\ncmap = sns.diverging_palette(220, 10, as_cmap=True)\n\n# Draw the heatmap with the mask and correct aspect ratio\nsns.heatmap(corr, mask=mask, cmap=cmap, vmax=.3, center=0,\n            square=True, linewidths=.5, cbar_kws={\"shrink\": .5})","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"c7be9be8-b999-42d9-a0cc-6bf2a035b25b","_uuid":"07cdbb447c11008557c1773e464d08f798d8b2b2","trusted":true},"cell_type":"code","source":"# get the data fields ready for stacking\nconcat_sub['champ_max'] = concat_sub.iloc[:, 1:ncol].max(axis=1)\nconcat_sub['champ_min'] = concat_sub.iloc[:, 1:ncol].min(axis=1)\nconcat_sub['champ_mean'] = concat_sub.iloc[:, 1:ncol].mean(axis=1)\nconcat_sub['champ_median'] = concat_sub.iloc[:, 1:ncol].median(axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"concat_sub.describe()","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"2b115d55-48f4-43c6-9b22-dbd8cd5a28a8","_uuid":"d412b01db3dd513eef9158bfaaebe96617e79d5b","trusted":true},"cell_type":"code","source":"cutoff_lo = -37\ncutoff_hi = 205","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"d0493bc1-1d92-4bc7-aa84-107fcb6d7324","_uuid":"698574b4531ce5ec1c59d5afaf451392169af5e0"},"cell_type":"markdown","source":"# Mean Stacking"},{"metadata":{"_cell_guid":"f1169e63-b019-4a39-87b5-8fa1d0488f09","_uuid":"a6642e2884dacc8c5095413e6a0945c6791ee1b5","trusted":true},"cell_type":"code","source":"concat_sub['scalar_coupling_constant'] = concat_sub['champ_mean']\nconcat_sub[['id', 'scalar_coupling_constant']].to_csv('stack_mean.csv', \n                                        index=False, float_format='%.6f')","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"64a7aeea-ac14-4ae8-ac62-0c58b13c1f9e","_uuid":"a24eecfd51f4f6665d751f3f1c126e46a44426bd"},"cell_type":"markdown","source":"**LB----**"},{"metadata":{"_cell_guid":"103f2414-04a8-40bd-8ffc-4e77e510e023","_uuid":"d213635b179fc8d07a6985d257c8c3e0007e0f7a"},"cell_type":"markdown","source":"# Median Stacking"},{"metadata":{"_cell_guid":"9d5af2c0-1cc5-4adc-9153-d20d19c69bd6","_uuid":"76fc5734615b45bf6234df1f450c9a24ca518834","trusted":true},"cell_type":"code","source":"concat_sub['scalar_coupling_constant'] = concat_sub['champ_median']\nconcat_sub[['id', 'scalar_coupling_constant']].to_csv('stack_median.csv', \n                                        index=False, float_format='%.6f')","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"3da1db01-1922-4d34-ae02-1d3acfa59fca","_uuid":"a1a1754ba1c9ca956da71920dcc5bf2f0ee78172"},"cell_type":"markdown","source":"![](http://)**LB ----**"},{"metadata":{"_cell_guid":"fba2a588-19a4-41fd-a495-af6a3a551777","_uuid":"caa0cb178c1f4921cb7c5b6552bfe4e0fb91475e"},"cell_type":"markdown","source":"# PushOut + Median Stacking \n\nPushout strategy is a bit agressive given what it does..."},{"metadata":{"_cell_guid":"f8646a39-f2cc-483a-912b-46af12b5de64","_uuid":"e6038b30485244cf144ede75fb3ecab55afa3f84","trusted":true},"cell_type":"code","source":"concat_sub['scalar_coupling_constant'] = np.where(np.all(concat_sub.iloc[:,1:ncol] > cutoff_lo, axis=1), 1, \n                                    np.where(np.all(concat_sub.iloc[:,1:ncol] < cutoff_hi, axis=1),\n                                             0, concat_sub['champ_median']))\nconcat_sub[['id', 'scalar_coupling_constant']].to_csv('stack_pushout_median.csv', \n                                        index=False, float_format='%.6f')","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"f016ad2f-ddc9-4182-a288-32f4dcb466d3","_uuid":"ed7b3420cf44929c977970605c9c231714926e0e"},"cell_type":"markdown","source":"> **LB -----**"},{"metadata":{"_cell_guid":"26fa22e0-4f00-455b-8328-3e3cdf34adfb","_uuid":"9244a9d9ddce162fa7ddd7d32e271097b0b405df"},"cell_type":"markdown","source":"# MinMax + Mean Stacking\n\nMinMax seems more gentle and it outperforms the previous one given its peformance score."},{"metadata":{"_cell_guid":"7634aaa4-2466-45b1-afbf-32dedb8691a4","_uuid":"4ffd0c65d59a03ee04d5452b853efac717f3f3f7","trusted":true},"cell_type":"code","source":"concat_sub['scalar_coupling_constant'] = np.where(np.all(concat_sub.iloc[:,1:ncol] > cutoff_lo, axis=1), \n                                    concat_sub['champ_max'], \n                                    np.where(np.all(concat_sub.iloc[:,1:ncol] < cutoff_hi, axis=1),\n                                             concat_sub['champ_min'], \n                                             concat_sub['champ_mean']))\nconcat_sub[['id', 'scalar_coupling_constant']].to_csv('stack_minmax_mean.csv', \n                                        index=False, float_format='%.6f')","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"de9e7a52-2a3e-440a-9056-93ed4d9f87f9","_uuid":"c4da0e0b2530daf4fdcb362980ad9fbfce5b3476"},"cell_type":"markdown","source":"> **LB ----**\n\n"},{"metadata":{"_cell_guid":"61831c59-a583-45d6-9bba-1102d1f80862","_uuid":"95b5ccf95505b498dede6966f2a899c376d855dd"},"cell_type":"markdown","source":"# MinMax + Median Stacking "},{"metadata":{"_cell_guid":"8aa75397-da75-4d93-916e-d88f464e13fe","_uuid":"d4b7e91489d86452dc6d13340109e07da40f9fa9","trusted":true},"cell_type":"code","source":"concat_sub['scalar_coupling_constant'] = np.where(np.all(concat_sub.iloc[:,1:ncol] > cutoff_lo, axis=1), \n                                    concat_sub['champ_max'], \n                                    np.where(np.all(concat_sub.iloc[:,1:ncol] < cutoff_hi, axis=1),\n                                             concat_sub['champ_min'], \n                                             concat_sub['champ_median']))\nconcat_sub[['id', 'scalar_coupling_constant']].to_csv('stack_minmax_median.csv', \n                                        index=False, float_format='%.6f')","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"b9d1df47-6c0b-4bca-9357-ceaf61746171","_uuid":"1abbdcd40aca61b375ee4dd0e3e90b814cecceba"},"cell_type":"markdown","source":"**LB ----** -"},{"metadata":{"_cell_guid":"307ebff0-d690-4dbf-8496-3fc8340bcc60","_uuid":"935499734e508b98e7d694606bd2851a4b3cbce5"},"cell_type":"markdown","source":"# MinMax + BestBase Stacking"},{"metadata":{"_cell_guid":"59f60095-f1ff-4c26-8d51-e0111e80f3ba","_uuid":"8e3f2ac12368eac5fc2b69e9e899873dd33adef8","trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"# load the model with best base performance\nsub_base = pd.read_csv('../input/champstacks/submission-giba-1 (2).csv')","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"2758287f-0e47-4dc6-ab11-004ce8812f8f","_uuid":"2ac62e85fbafd2a9095e71d576633a65694787d8","trusted":true},"cell_type":"code","source":"concat_sub['champ_base'] = sub_base['scalar_coupling_constant']\nconcat_sub['scalar_coupling_constant'] = np.where(np.all(concat_sub.iloc[:,1:ncol] > cutoff_lo, axis=1), \n                                    concat_sub['champ_max'], \n                                    np.where(np.all(concat_sub.iloc[:,1:ncol] < cutoff_hi, axis=1),\n                                             concat_sub['champ_min'], \n                                             concat_sub['champ_base']))\nconcat_sub[['id', 'scalar_coupling_constant']].to_csv('stack_minmax_bestbase.csv', \n                                        index=False, float_format='%.6f')","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"c493ff8f-99bd-4b20-8e68-80b4f85682c9","_uuid":"f4a0c7572916fd1ace3a3b0b7ea9fd9564c96d4d"},"cell_type":"markdown","source":"> **LB----** -"}],"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.6.3"}},"nbformat":4,"nbformat_minor":1}