{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from sklearn.datasets import load_iris\n",
    "from sklearn.model_selection import StratifiedKFold as skStratifiedKFold"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "class StratifiedKFold():\n",
    "    def __init__(self, n_splits=5, shuffle=False, random_state=0):\n",
    "        self.n_splits = n_splits\n",
    "        self.shuffle = shuffle\n",
    "        self.random_state = random_state\n",
    "\n",
    "    def _kfold(self, count, rng):\n",
    "        indices = np.arange(count)\n",
    "        if self.shuffle:\n",
    "            rng.shuffle(indices)\n",
    "        fold_sizes = np.full(self.n_splits, count // self.n_splits)\n",
    "        fold_sizes[:count % self.n_splits] += 1\n",
    "        current = 0\n",
    "        for fold_size in fold_sizes:\n",
    "            test_mask = np.zeros(count, dtype=bool)\n",
    "            test_mask[current:current + fold_size] = True\n",
    "            yield indices[test_mask]\n",
    "            current += fold_size\n",
    "\n",
    "    def _make_test_folds(self, X, y):\n",
    "        rng = np.random.RandomState(self.random_state)\n",
    "        unique_y, y_inversed = np.unique(y, return_inverse=True)\n",
    "        y_counts = np.bincount(y_inversed)\n",
    "        test_folds = np.zeros(X.shape[0])\n",
    "        per_cls_cvs = [self._kfold(count, rng) for count in y_counts]\n",
    "        test_folds = np.zeros(X.shape[0])\n",
    "        for test_fold_indices, per_cls_splits in enumerate(zip(*per_cls_cvs)):\n",
    "            for cls, test_split in zip(unique_y, per_cls_splits):\n",
    "                cls_test_folds = test_folds[y == cls]\n",
    "                cls_test_folds[test_split] = test_fold_indices\n",
    "                test_folds[y == cls] = cls_test_folds\n",
    "        return test_folds\n",
    "\n",
    "    def _iter_test_masks(self, X, y):\n",
    "        test_folds = self._make_test_folds(X, y)\n",
    "        for i in range(self.n_splits):\n",
    "            yield test_folds == i\n",
    "\n",
    "    def split(self, X, y):\n",
    "        indices = np.arange(X.shape[0])\n",
    "        for test_index in self._iter_test_masks(X, y):\n",
    "            yield indices[~test_index], indices[test_index]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "X, y = load_iris(return_X_y=True)\n",
    "cv1 = StratifiedKFold(n_splits=5)\n",
    "cv2 = skStratifiedKFold(n_splits=5)\n",
    "for (train1, test1), (train2, test2) in zip(cv1.split(X, y), cv2.split(X, y)):\n",
    "    assert np.array_equal(train1, train2)\n",
    "    assert np.array_equal(test1, test2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "X, y = load_iris(return_X_y=True)\n",
    "cv1 = StratifiedKFold(n_splits=5, shuffle=True, random_state=0)\n",
    "cv2 = skStratifiedKFold(n_splits=5, shuffle=True, random_state=0)\n",
    "for (train1, test1), (train2, test2) in zip(cv1.split(X, y), cv2.split(X, y)):\n",
    "    assert np.array_equal(train1, train2)\n",
    "    assert np.array_equal(test1, test2)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "dev",
   "language": "python",
   "name": "dev"
  },
  "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.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}