{"cells":[{"metadata":{},"cell_type":"markdown","source":"In this kernel I present my version of Abishek's optimizer https://www.kaggle.com/abhishek/optimizer-for-quadratic-weighted-kappa  \nIn my experiments this gives a bit better CV and LB scores.\n\nThe difference is that it is based on golden-section method https://en.wikipedia.org/wiki/Golden-section_search instead of Nelder-Mead.  \nI believe that Nelder-Mead stops converging too early because the optimized function is not continious (due to finite dataset).  \nFirst-order methods (such as GS) are usually slower, but they don't care about the smoothness of the function.  \nSo, GS doesn't get stuck on a plato.  "},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\nimport scipy as sp\nfrom functools import partial\nfrom sklearn import metrics\nfrom collections import Counter\nimport json","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"# put numerical value to bins\ndef to_bins(x, tresholds):\n    if x <= tresholds[0]:\n        return 0\n    for i in range(1, len(tresholds)):\n        if x > tresholds[i - 1] and x <= tresholds[i]:\n            return i\n    return len(tresholds)\n\nclass OptimizedRounder(object):\n    def __init__(self):\n        self.coef_ = 0\n    \n    def _loss(self, coef, X, y, idx):\n        X_p = np.array([to_bins(pred, coef) for pred in X])\n        ll = -metrics.cohen_kappa_score(y, X_p, weights='quadratic')\n        return ll\n\n    def fit(self, X, y):\n        coefs = []\n        nsplits = 4\n        for split_i in range(nsplits):\n            coef = [1.5, 2.0, 2.5, 3.0]\n            golden1 = 0.618\n            golden2 = 1 - golden1\n            ab_start = [(1, 2), (1.5, 2.5), (2, 3), (2.5, 3.5)]\n            for it1 in range(10):\n                for idx in range(4):\n                    # golden section search\n                    a, b = ab_start[idx]\n                    # calc losses\n                    coef[idx] = a\n                    la = self._loss(coef, X[split_i::nsplits], y[split_i::nsplits], idx)\n                    coef[idx] = b\n                    lb = self._loss(coef, X[split_i::nsplits], y[split_i::nsplits], idx)\n                    for it in range(20):\n                        # choose value\n                        if la > lb:\n                            a = b - (b - a) * golden1\n                            coef[idx] = a\n                            la = self._loss(coef, X[split_i::nsplits], y[split_i::nsplits], idx)\n                        else:\n                            b = b - (b - a) * golden2\n                            coef[idx] = b\n                            lb = self._loss(coef, X[split_i::nsplits], y[split_i::nsplits], idx)\n            coefs.append(coef)\n        coef = list(np.array(coefs).mean(axis=0))\n        self.coef_ = {'x': coef}\n    \n    def predict(self, X, coef):\n        X_p = np.array([to_bins(pred, coef) for pred in X])\n        return X_p\n\n    def coefficients(self):\n        return self.coef_['x'] ","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"How to use? (this class is compatible with original one)"},{"metadata":{"_kg_hide-output":true,"trusted":true},"cell_type":"code","source":"optR = OptimizedRounder()\noptR.fit(valid_predictions, targets)\ncoefficients = optR.coefficients()\nvalid_predictions = optR.predict(valid_predictions, coefficients)\ntest_predictions = optR.predict(test_predictions, coefficients)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"In case of any questions, feel free to ask! :)"}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}