{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\n\nimport warnings\nwarnings.simplefilter('ignore')\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"submission = pd.read_csv('../input/cat-in-the-dat-ii/sample_submission.csv', index_col='id')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"kernels = pd.read_csv('../input/cat-in-dat-2-public-kernels/kernels.csv', index_col='id')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"kernels.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import glob\n\ndef make_filename(idx):\n    return glob.glob('../input/cat-in-dat-2-public-kernels/' + str(idx) + '__submission.csv')[0]\n\ndef read_predictions(idx):\n    temp = pd.read_csv(make_filename(idx), index_col='id')\n    temp.columns = [str(idx)]\n    return temp\n\n\npredictions = pd.concat([read_predictions(idx) for idx in kernels.index], axis=1)\npredictions.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"predictions.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Correlation matrix"},{"metadata":{"trusted":true},"cell_type":"code","source":"# From https://seaborn.pydata.org/examples/many_pairwise_correlations.html\n\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n\ncorr = predictions.corr()\n\nmask = np.zeros_like(corr, dtype=np.bool)\nmask[np.triu_indices_from(mask)] = True\n\nf, ax = plt.subplots(figsize=(12, 12))\n\nsns.heatmap(corr, mask=mask, cmap='Blues', vmin=0.95, center=0, linewidths=1, annot=True, fmt='.4f')\n\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Stack"},{"metadata":{"trusted":true},"cell_type":"code","source":"submission['target'] = predictions.mean(axis=1)\nsubmission.to_csv('stack-mean.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Weighted sum"},{"metadata":{"trusted":true},"cell_type":"code","source":"scores = kernels['score']\n\nsum_scores = sum(scores)\n\nweights = [x / sum_scores for x in scores]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"weighted_sum_prediction = predictions.dot(pd.Series(weights, index=predictions.columns))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"weighted_sum_prediction.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission['target'] = weighted_sum_prediction\nsubmission.to_csv('stack-weighted-sum.csv')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Blend by ranking"},{"metadata":{"trusted":true},"cell_type":"code","source":"scores = kernels['score']\n\nsum_scores = sum(scores)\n\nweights = [x / sum_scores for x in scores]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from scipy.stats import rankdata\n\n\ndef blend_by_ranking(data, weights):\n    out = np.zeros(data.shape[0])\n    for idx,column in enumerate(data.columns):\n        out += weights[idx] * rankdata(data[column].values)\n    out /= np.max(out)\n    return out","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"blend_by_ranking_prediction = blend_by_ranking(predictions, weights)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"blend_by_ranking_prediction","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission['target'] = blend_by_ranking_prediction\nsubmission.to_csv('stack-blend-by-ranking.csv')","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}