{"componentChunkName":"component---src-templates-post-tsx","path":"/dataset_optimization/split_data_the_right_way/","result":{"data":{"logo":{"childImageSharp":{"gatsbyImageData":{"layout":"fixed","backgroundColor":"#f8f8f8","images":{"fallback":{"src":"/static/e43c6530b7b2b056de6bfee951c9e393/b181d/compasslogo.png","srcSet":"/static/e43c6530b7b2b056de6bfee951c9e393/b181d/compasslogo.png 1920w","sizes":"1920px"},"sources":[{"srcSet":"/static/e43c6530b7b2b056de6bfee951c9e393/e02c9/compasslogo.webp 1920w","type":"image/webp","sizes":"1920px"}]},"width":1920,"height":472}}},"mdx":{"body":"const _excluded = [\"components\"];\n\nfunction _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }\n\nfunction _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }\n\nfunction _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }\n\n/* @jsxRuntime classic */\n\n/* @jsx mdx */\nconst _frontmatter = {\n  \"layout\": \"post\",\n  \"title\": \"How to Split Your Dataset the Right Way\",\n  \"author\": [\"Boris Giba\"],\n  \"tags\": [\"Dataset Optimization\"],\n  \"image\": \"img/SplitDataTheRightWay/SplitDataTheRightWay_Thumbnail.png\",\n  \"date\": \"2021-05-01T18:09:26.968Z\",\n  \"dateModified\": \"2021-05-01T18:09:26.968Z\",\n  \"changelog\": [],\n  \"draft\": false,\n  \"imgCaption\": \"Background image by Mario Gogh\",\n  \"imgSource\": \"https://unsplash.com/photos/lTXhxtaqF3c\",\n  \"timeToRead\": 15,\n  \"uniquePostId\": 8,\n  \"excerpt\": \"If you are just starting out in machine learning and building your first real models, you will have to split your dataset into a train set as well as a test set. But what benefits does this splitting yield? How can you split your dataset optimally? In this article, we will go through these questions and explore why splitting your dataset makes sense and how you can split your dataset properly.\",\n  \"dependencies\": [],\n  \"proficiencyLevel\": \"Beginner\"\n};\n\nconst makeShortcode = name => function MDXDefaultShortcode(props) {\n  console.warn(\"Component \" + name + \" was not imported, exported, or provided by MDXProvider as global scope\");\n  return mdx(\"div\", props);\n};\n\nconst ToolTip = makeShortcode(\"ToolTip\");\nconst ConditionalLazyPlot = makeShortcode(\"ConditionalLazyPlot\");\nconst ConditionalLazyPlotInteractive = makeShortcode(\"ConditionalLazyPlotInteractive\");\nconst SmoothScrollLink = makeShortcode(\"SmoothScrollLink\");\nconst WarningTip = makeShortcode(\"WarningTip\");\nconst Collapsible = makeShortcode(\"Collapsible\");\nconst layoutProps = {\n  _frontmatter\n};\nconst MDXLayout = \"wrapper\";\nreturn function MDXContent(_ref) {\n  let {\n    components\n  } = _ref,\n      props = _objectWithoutProperties(_ref, _excluded);\n\n  return mdx(MDXLayout, _extends({}, layoutProps, props, {\n    components: components,\n    mdxType: \"MDXLayout\"\n  }), mdx(\"h2\", {\n    \"id\": \"outline\"\n  }, `Outline`), mdx(\"p\", null, `In this article, you will learn why you should split your dataset into a training and testing subset,\nhow you can do so with just a single line of code and how this can increase the robustness of your machine learning model.\nLet’s begin!`), mdx(\"h2\", {\n    \"id\": \"prerequisites\"\n  }, `Prerequisites`), mdx(\"p\", null, `For this article, I’ll assume that you are familiar with at least one (supervised) machine learning model.\nIt does not matter that much which one it is, but you should know at least one of them.\nIf you don’t know a machine learning model, I’d recommend you read `, mdx(\"a\", {\n    parentName: \"p\",\n    \"href\": \"/machine_learning_models/linear_regression\"\n  }, `Linear Regression Explained, Step by Step`), ` and then come back to this article. In the linked article,\nyou will learn everything you need to know about linear regression and implement it step by step\nin raw Python and with scikit-learn.\nAlso, I highly recommend you read the `, mdx(\"a\", {\n    parentName: \"p\",\n    \"href\": \"/model_optimization/bias_and_variance\"\n  }, `article about bias and variance`), ` prior to reading this\narticle, as it lays the groundwork for this post. By reading that article, you will learn everything you need to know about\nbias and variance, two fundamental concepts necessary to understand and compare machine learning models.`), mdx(\"h2\", {\n    \"id\": \"training-and-testing\"\n  }, `Training and Testing`), mdx(\"p\", null, `Now, why do we even need to split our dataset?\nGenerally speaking, our machine learning model takes in data,\nmakes some predictions, and then we somehow tell our\nmodel how good or bad its predictions were.\nThen we compare the predictions of our model\nwith our labels and then we calculate `, mdx(\"em\", {\n    parentName: \"p\"\n  }, `by how much the predictions differ from our labels`), ` based on some metric like the mean squared error or the cross entropy.`), mdx(\"div\", null, mdx(\"p\", null, `The more data we use to train our model, the more opportunities it has to learn from\nits mistakes, the more insights it can extract from the data it has been given, and the better the resulting model will be at predicting our labels`, mdx(ToolTip, {\n    trigger: \"*\",\n    mdxType: \"ToolTip\"\n  }, `Assuming that our dataset is reasonable and does not contain a lot of very similar entries or a lot of unrepresentative data points.`), `.\nSo if our final goal is to make our model as good as possible at predicting our labels,\nwhy don’t we just take the entire dataset to train our model?\nIn theory, if we take the entire dataset to train our model, it will perform better\nthan if we just use 70% or 80% of the data for training.\nThe problem is that if we use all the data for training,\nwe can no longer evaluate the true performance of our model in an unbiased fashion.\nSure, we can evaluate the model on the data that it was trained on,\nbut this would be problematic. To understand why, let’s look at a practical example.`)), mdx(\"h3\", {\n    \"id\": \"a-practical-example\"\n  }, `A Practical Example`), mdx(\"p\", null, `Let’s say we have a dataset of exam scores where every\nentry in the dataset contains the number of hours a particular student spent studying for that exam\nas well as the number of points that student achieved in said exam.\nThe dataset looks like this:`), mdx(ConditionalLazyPlot, _extends({}, polynomialData, {\n    mdxType: \"ConditionalLazyPlot\"\n  }), mdx(\"p\", null, mdx(\"span\", {\n    parentName: \"p\",\n    \"className\": \"gatsby-resp-image-wrapper\",\n    \"style\": {\n      \"position\": \"relative\",\n      \"display\": \"block\",\n      \"marginLeft\": \"auto\",\n      \"marginRight\": \"auto\",\n      \"maxWidth\": \"1088px\"\n    }\n  }, `\n      `, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"gatsby-resp-image-background-image\",\n    \"style\": {\n      \"paddingBottom\": \"63.800000000000004%\",\n      \"position\": \"relative\",\n      \"bottom\": \"0\",\n      \"left\": \"0\",\n      \"backgroundImage\": \"url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAYAAACpUE5eAAAACXBIWXMAABYlAAAWJQFJUiTwAAABnUlEQVQ4y41T207CQBBtLJQW6J0WeoNewYIYxBg1UR9M/AD//2OOmVlaS7DEh5Od3c6enjk7Iw3tFRpM55XAQmBgLdH9fg1NrkSBbIqNFdUwwg2verBuk2i9gJnwKhsRx02eRIE6y6HNMshmcoZrijS/wmiWYxrvOR5YIl8yw1tYcY2JX0Bx/lmeGSPYf8LffiB9/YYe30E2Y0FIZFTm2MswtK95lrblGcsD3OoZIzc/U8clk0J9sYYebKB6Re9D0CUrO8IpnzFebDreJlCcFEOKG4VEaMZbWOkDtPn6gpRUTYIafv2OabRjpX0vLVG508UamptiEtZQnKwtgUB/JwK3euHybvQQQ1LUR2gn25OHOauzi6dWpWzEsLNHRIcv+PUb+yjQ34sSNbLmFUxIPhjJPZtNseJmsNIjVK/kFmm8Ui7IfvtVavyitlHdDIq9FOR8VmLkphg5Kz6jXDpTeF9A88RPSNSgeZTu2HR948vzki+N/ZKJaADoAel7A8ojIZ3RW/05XpRMLeWcPLYiMQDOcsfE8mn0qNzuiP4AEw5BfZogZ7EAAAAASUVORK5CYII=')\",\n      \"backgroundSize\": \"cover\",\n      \"display\": \"block\"\n    }\n  }), `\n  `, mdx(\"picture\", {\n    parentName: \"span\"\n  }, `\n          `, mdx(\"source\", {\n    parentName: \"picture\",\n    \"srcSet\": [\"/static/98a01f36e59b031e4efee96823a1bc4a/601b1/examDatasetBlank.webp 500w\", \"/static/98a01f36e59b031e4efee96823a1bc4a/80c60/examDatasetBlank.webp 1000w\", \"/static/98a01f36e59b031e4efee96823a1bc4a/62aa4/examDatasetBlank.webp 1088w\"],\n    \"sizes\": \"(max-width: 1088px) 100vw, 1088px\",\n    \"type\": \"image/webp\"\n  }), `\n          `, mdx(\"source\", {\n    parentName: \"picture\",\n    \"srcSet\": [\"/static/98a01f36e59b031e4efee96823a1bc4a/0eb09/examDatasetBlank.png 500w\", \"/static/98a01f36e59b031e4efee96823a1bc4a/1263b/examDatasetBlank.png 1000w\", \"/static/98a01f36e59b031e4efee96823a1bc4a/347c0/examDatasetBlank.png 1088w\"],\n    \"sizes\": \"(max-width: 1088px) 100vw, 1088px\",\n    \"type\": \"image/png\"\n  }), `\n          `, mdx(\"img\", {\n    parentName: \"picture\",\n    \"className\": \"gatsby-resp-image-image\",\n    \"src\": \"/static/98a01f36e59b031e4efee96823a1bc4a/347c0/examDatasetBlank.png\",\n    \"alt\": \"Exam Scores Dataset\",\n    \"title\": \"Exam Scores Dataset\",\n    \"loading\": \"lazy\",\n    \"decoding\": \"async\",\n    \"style\": {\n      \"width\": \"100%\",\n      \"height\": \"100%\",\n      \"margin\": \"0\",\n      \"verticalAlign\": \"middle\",\n      \"position\": \"absolute\",\n      \"top\": \"0\",\n      \"left\": \"0\"\n    }\n  }), `\n        `), `\n    `))), mdx(\"div\", null, mdx(\"p\", null, `We now use polynomial regression`, mdx(ToolTip, {\n    width: \"20em\",\n    trigger: \"*\",\n    mdxType: \"ToolTip\"\n  }, `Don’t worry if you are not familiar with polynomial regression. It is very similar to linear regression, with the exception that you\nsquare/cube/etc. the input features before plugging them into the linear regression model.`), ` to predict the number of points\nachieved based on the number of hours studied. Now we might choose to use a very complicated model and see how it performs.\nAs an example, here’s how a polynomial regression with a degree of 15 looks like: `)), mdx(ConditionalLazyPlot, _extends({}, polynomialPredOverFit, {\n    mdxType: \"ConditionalLazyPlot\"\n  }), mdx(\"p\", null, mdx(\"span\", {\n    parentName: \"p\",\n    \"className\": \"gatsby-resp-image-wrapper\",\n    \"style\": {\n      \"position\": \"relative\",\n      \"display\": \"block\",\n      \"marginLeft\": \"auto\",\n      \"marginRight\": \"auto\",\n      \"maxWidth\": \"1096px\"\n    }\n  }, `\n      `, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"gatsby-resp-image-background-image\",\n    \"style\": {\n      \"paddingBottom\": \"62.2%\",\n      \"position\": \"relative\",\n      \"bottom\": \"0\",\n      \"left\": \"0\",\n      \"backgroundImage\": \"url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAMCAYAAABiDJ37AAAACXBIWXMAABYlAAAWJQFJUiTwAAABz0lEQVQoz2VT246bMBBNN8vNAcwl4WYwmEsaspBqq3TVblVppapv/f/fOSs7CQvZh5FhxnPmzPHMSvNLSHv0ONzsKzzWw897eOygfPO4OuX/zH8fX8mPm5GoWdg8dkuavmmBtVvgwWaL+EoPKpBdCyfrYESVurSmhQIM+ROcdA/9jo3mlXCKE9xyRDT8hhV3qoBiaMcdQj7AYR1I1uCRcmhB+amlqTVawNgK8J//EI9/IN7+w0qveRJwE7dKt03awEwqxW6h2aQXx9pl0EOB3dMLHD7gwZHtFjDT+kPDTXQBpMUBVIzQfAF9W0FKMTELSuiBADv/RXT6BTNuLi0qfwWLNTPAuIXPethZh/D4DJv3ILyBldZKK3nxC0lAxTcE++8XnwKrpmILQHJluEkaeO0Avx9Bctl+rRKl4F79jPj0CjNqJ/Hn7O8YdvDzowIkrEb64xVuNcBIKpW8Pb6And8QdOfFXN5e+xOg1MDc1rDTFq7YgyQtjLACYQ3MXQ2vGaHRHLrHYQSViskc42bXu4vBXlMOIxSwolptgZxBGSRxq15XFrwNuhwzecrHNEOhAO2k+wCcr85iVLzysop5r07KDqDZAUFxVMXnWzTflHfI5iuz2Pa3dAAAAABJRU5ErkJggg==')\",\n      \"backgroundSize\": \"cover\",\n      \"display\": \"block\"\n    }\n  }), `\n  `, mdx(\"picture\", {\n    parentName: \"span\"\n  }, `\n          `, mdx(\"source\", {\n    parentName: \"picture\",\n    \"srcSet\": [\"/static/8976ae5433aa16ddbc4181031d8a7dc6/601b1/overFitScreenshot.webp 500w\", \"/static/8976ae5433aa16ddbc4181031d8a7dc6/80c60/overFitScreenshot.webp 1000w\", \"/static/8976ae5433aa16ddbc4181031d8a7dc6/d2558/overFitScreenshot.webp 1096w\"],\n    \"sizes\": \"(max-width: 1096px) 100vw, 1096px\",\n    \"type\": \"image/webp\"\n  }), `\n          `, mdx(\"source\", {\n    parentName: \"picture\",\n    \"srcSet\": [\"/static/8976ae5433aa16ddbc4181031d8a7dc6/0eb09/overFitScreenshot.png 500w\", \"/static/8976ae5433aa16ddbc4181031d8a7dc6/1263b/overFitScreenshot.png 1000w\", \"/static/8976ae5433aa16ddbc4181031d8a7dc6/f7f62/overFitScreenshot.png 1096w\"],\n    \"sizes\": \"(max-width: 1096px) 100vw, 1096px\",\n    \"type\": \"image/png\"\n  }), `\n          `, mdx(\"img\", {\n    parentName: \"picture\",\n    \"className\": \"gatsby-resp-image-image\",\n    \"src\": \"/static/8976ae5433aa16ddbc4181031d8a7dc6/f7f62/overFitScreenshot.png\",\n    \"alt\": \"overfitted Model\",\n    \"title\": \"overfitted Model\",\n    \"loading\": \"lazy\",\n    \"decoding\": \"async\",\n    \"style\": {\n      \"width\": \"100%\",\n      \"height\": \"100%\",\n      \"margin\": \"0\",\n      \"verticalAlign\": \"middle\",\n      \"position\": \"absolute\",\n      \"top\": \"0\",\n      \"left\": \"0\"\n    }\n  }), `\n        `), `\n    `))), mdx(\"p\", null, `As you see, this model has a very low error (it has a root mean squared error, or RMSE, of about 4.93).\nIn other terms, it has very low bias. However, this model also has a very high variance. After reading\nthe `, mdx(\"a\", {\n    parentName: \"p\",\n    \"href\": \"/model_optimization/bias_and_variance\"\n  }, `article about bias and variance`), `, we can say that this model is `, mdx(\"em\", {\n    parentName: \"p\"\n  }, `overfit`), `.`), mdx(\"p\", null, `This becomes even clearer when we split our dataset into a training portion and a testing portion.\nWe still use our overfit model, but this time we train it only on the training data\nand then evaluate its performance both on the train set as well as the test set.\nThis allows us to reason about the variance of this particular model.\nTake a look at the following plot:`), mdx(ConditionalLazyPlotInteractive, _extends({}, overFitTrainTest, {\n    mdxType: \"ConditionalLazyPlotInteractive\"\n  }), mdx(\"p\", null, mdx(\"span\", {\n    parentName: \"p\",\n    \"className\": \"gatsby-resp-image-wrapper\",\n    \"style\": {\n      \"position\": \"relative\",\n      \"display\": \"block\",\n      \"marginLeft\": \"auto\",\n      \"marginRight\": \"auto\",\n      \"maxWidth\": \"1110px\"\n    }\n  }, `\n      `, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"gatsby-resp-image-background-image\",\n    \"style\": {\n      \"paddingBottom\": \"65.4%\",\n      \"position\": \"relative\",\n      \"bottom\": \"0\",\n      \"left\": \"0\",\n      \"backgroundImage\": \"url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAYAAACpUE5eAAAACXBIWXMAABYlAAAWJQFJUiTwAAAB6UlEQVQ4y3VTaY+bMBBlcxAI5gqEy+HGJLBNsoe2VbWqVFXq//9JrxoTsmGb/TBiPNjPz2/eKAsnw5LCzaFuCqw8ilLm47/Fp6C9H5FNcoWScaOdHOCmPbzsEQ7vroengJe1e1OzU8ztVObKeEjzS6h+cYfBvRj2rehVdDZooQeNzBV9W8OOD7C4gJ5UWNhfA5IMM3OHsD4iffmFx99/EXVnsLBFUJ2lVIoZtXB3PcykGQC/YEj1B8aheiXE95/Y1if4zRksbzE3OdRNjgUxNMIGVrwHixuskxpzKx1AnCmYvq0QNkcE/QsYP2DGuNRNi6tBV3dooMIiIZvhpAck/RPW8R5rXkP1isszc8ztDEn/jOrtHapfSqDlppB79KSeyKQYoZAMjaBCdDzBajpoSQktqiTLmZXC2BbgpzcoBr9aTF52D1Ay5B1YVMPrvmHbP8OpOqhBiaW1g3j9AfH+B4F4knJQYz4A8/8B10ENFgqswxp2LRAezjB4CzUowBKBmGSIBFZ+PTWxk10Y3jhj9CEFNYelQhZ1v4IWlDCTVnpTI73cDBrVvVLag4jQVJm8nTjjOinX7ya/PovkIJ+acSu/tKaLqasEKs3tlbjFUG6tMTxpYExsaPzsZA/rEs6ukw0cx+x2LMf8H7WxRCQe61JuAAAAAElFTkSuQmCC')\",\n      \"backgroundSize\": \"cover\",\n      \"display\": \"block\"\n    }\n  }), `\n  `, mdx(\"picture\", {\n    parentName: \"span\"\n  }, `\n          `, mdx(\"source\", {\n    parentName: \"picture\",\n    \"srcSet\": [\"/static/770f28e148443b7b67d42c1d6d5842b1/601b1/examDatasetToggleSets.webp 500w\", \"/static/770f28e148443b7b67d42c1d6d5842b1/80c60/examDatasetToggleSets.webp 1000w\", \"/static/770f28e148443b7b67d42c1d6d5842b1/d209b/examDatasetToggleSets.webp 1110w\"],\n    \"sizes\": \"(max-width: 1110px) 100vw, 1110px\",\n    \"type\": \"image/webp\"\n  }), `\n          `, mdx(\"source\", {\n    parentName: \"picture\",\n    \"srcSet\": [\"/static/770f28e148443b7b67d42c1d6d5842b1/0eb09/examDatasetToggleSets.png 500w\", \"/static/770f28e148443b7b67d42c1d6d5842b1/1263b/examDatasetToggleSets.png 1000w\", \"/static/770f28e148443b7b67d42c1d6d5842b1/ffc38/examDatasetToggleSets.png 1110w\"],\n    \"sizes\": \"(max-width: 1110px) 100vw, 1110px\",\n    \"type\": \"image/png\"\n  }), `\n          `, mdx(\"img\", {\n    parentName: \"picture\",\n    \"className\": \"gatsby-resp-image-image\",\n    \"src\": \"/static/770f28e148443b7b67d42c1d6d5842b1/ffc38/examDatasetToggleSets.png\",\n    \"alt\": \"Exam Dataset with Train- and Test-Set\",\n    \"title\": \"Exam Dataset with Train- and Test-Set\",\n    \"loading\": \"lazy\",\n    \"decoding\": \"async\",\n    \"style\": {\n      \"width\": \"100%\",\n      \"height\": \"100%\",\n      \"margin\": \"0\",\n      \"verticalAlign\": \"middle\",\n      \"position\": \"absolute\",\n      \"top\": \"0\",\n      \"left\": \"0\"\n    }\n  }), `\n        `), `\n    `))), mdx(\"p\", null, `Above the plot, you can see the RMSE for the polynomial regression model with regard to the currently displayed dataset\nas well as the relative difference (R.DIFF) between the initial RMSE and the current RMSE.\nWhen you load the plot, R.DIFF will be equal to 0, since the current RMSE is also\nthe initial RMSE. By pressing the button at the top, you can toggle between the train and test set.\nOnce you switch to the test set, you will notice a pretty significant increase in the RMSE.\nTo be more exact, you will see an increase of 17.16%. If you’ve read the article about bias and variance,\nyou will recognize this as a key sign of overfitting.`), mdx(\"p\", null, `We would have no way of diagnosing this overfitting if we did not have a dedicated\ntest set. If we did not have a test set, we would only find out that our model is overfit\nonce it was actually being used to predict new data!\nSo by splitting our dataset into a train set and a test set, we can evaluate our model in a realistic scenario\nand prevent it from overfitting.`), mdx(\"h3\", {\n    \"id\": \"an-analogy\"\n  }, `An Analogy`), mdx(\"p\", null, `I really like analogies, and maybe you do to. If you do, then this might be a paragraph worth reading.\nIf you don’t, feel free to skip ahead to the next section, `, mdx(SmoothScrollLink, {\n    scrollid: \"common-questions\",\n    mdxType: \"SmoothScrollLink\"\n  }, `Common Questions`), `.`), mdx(\"p\", null, `I like to think of a student preparing for their mathematics exam (or really, any other subject).\nThe professor provides the student with\nexercise sheets and corresponding solutions which the student can use to prepare for the exam.\nHowever, when the student takes the exam, they are presented with the exact same exercises\nas the ones they were given on the exercise sheets. Of course, the student aces the exam\nbecause they have trained on exactly these questions. But the professor can’t actually\ntell whether the student really understood the material and learned to solve these problems efficiently,\nor whether they just memorized every single possible question and its corresponding answer without learning anything.\nThis happens when the professor does not have a “test set” of exercises ready to evaluate the performance\nof their “model”, or in this case, their student.\nThe professor needs to have some sort of “final exam” for the student so that they can test whether or not the student truly understood the material.\nThe final exam needs to have questions similar to the ones\nin the practice material, but it also has to contain some questions\nthe student has never seen before.`), mdx(\"p\", null, `In our case, you are the professor, and your machine learning model is your student.\nYou have to take care of your pool of exercises and make sure that you teach\nyour student to truly understand the material, and not just to memorize all the answers.`), mdx(\"h2\", {\n    \"id\": \"common-questions\"\n  }, `Common Questions`), mdx(\"p\", null, `When the train and test sets are first introduced in a machine learning class,\nthere are usually a number of questions that arise,\nand some additional questions that arise after you first try to implement\ndataset splitting in your own machine learning projects.\nHere, I want to go over some of the most common questions asked.`), mdx(\"h3\", {\n    \"id\": \"how-large-should-the-testing-set-be\"\n  }, `How Large Should the Testing Set be?`), mdx(\"p\", null, `I want to tell you the most important thing upfront:\nThere is no clear answer to this question.\nThis is because test set size is a hyperparameter\nwhich we have to determine ourselves before training our model. `), mdx(\"p\", null, `The optimal value for the size of your testing set depends on the problem you are\ntrying to solve, the model you are using, as well as the dataset itself.`), mdx(\"p\", null, `If you have enough time on your hands,\nyou could just try out a 60-40-split (that is, use 60% of your data for training,\nand 40% of it for testing), a 70-30-split, an 80-20-split, and so on.\nThen you could compare the test scores for each of these splits and\ntake the split where the test error is the lowest. However, this can take\na lot of time and computing power, and in most cases it won’t even make a huge difference in your testing error.\nSo with that being said, what are some general rules we can follow to decide how large our test set should be?`), mdx(\"p\", null, `Usually, you can estimate how much data you will need for testing based on the amount of data that you have available.\nIf you have a dataset with anything between 1.000 and 50.000 samples,\na good rule of thumb is to take 80% for training, and 20% for testing. The more data you have,\nthe smaller your test set can be. If you have 1.000.000 samples, you would probably be fine by reserving just 1% for testing and using the remaining 99% for training.\nBut when you have `, mdx(\"em\", {\n    parentName: \"p\"\n  }, `less`), ` data than that, things start to get more tricky.\nOn one hand, you want to use as many samples as possible from your already small dataset to train your model.\nOn the other hand, you also `, mdx(\"em\", {\n    parentName: \"p\"\n  }, `need`), ` to use a higher percentage of your data for testing to get a realistic performance estimate, exactly because your dataset is so small.\nThe only thing in this scenario that is (almost) guaranteed to increase your model performance is to “just get more data”, as bluntly as it sounds.\nIf applicable to the problem, you can also try to synthetically generate more data using something like `, mdx(\"a\", {\n    parentName: \"p\",\n    \"href\": \"/dataset_optimization/data_augmentation\"\n  }, `Data Augmentation`), `.`), mdx(\"h3\", {\n    \"id\": \"which-part-of-the-dataset-should-be-used-for-testing\"\n  }, `Which Part of the Dataset Should be Used For Testing?`), mdx(\"p\", null, `If you have never split your dataset into a training and testing subset before,\nyou might ask yourself which part of the dataset should be used for training and which part should\nbe used for testing. Does it even matter? Well, maybe. If your dataset contains some sort of relevant `, mdx(\"em\", {\n    parentName: \"p\"\n  }, `order`), `,\nthen you should definitely use a specific portion of it. An example of this would be `, mdx(\"em\", {\n    parentName: \"p\"\n  }, `time series data`), `,\nwhich is ordered according to time. `), mdx(\"p\", null, `So if you are trying to jump on the Bitcoin-Hype and build a machine learning model\nto try and predict the future price of Bitcoin based on a dataset containing the price of Bitcoin for every week\nof the past 5 years, then it would not make a lot of sense to shuffle this dataset.\nThis is because by shuffling your dataset, you are distorting the ordering of your data,\nwhich in this case, is something we want to keep.\nIf your dataset does not follow any such intrinsic order, then you should definitely shuffle your dataset and take a random\nsubset for training and testing. Why? `), mdx(\"p\", null, `If you don’t shuffle your dataset and take say the first 20% of your dataset for testing,\nyou might just be unlucky and get disproportionately many samples from one class/from one specific interval,\nbecause the data was not properly shuffled before. Maybe the person creating the dataset “partially sorted” the dataset\nunintendedly. Imagine you have a dataset of mathematics exam questions, containing linear algebra, calculus, and probability questions.\nThe person who assembled the dataset might have written most of the linear algebra questions on one day, written the calculus questions\non another day, and written the probability questions on a third day. Then they just combined all of these questions and formed a dataset.\nAs a consequence, the first part of the dataset may contain disproportionately many linear algebra questions.\nWhy is this bad for our model?`), mdx(\"p\", null, `If we think back to our analogy with the student, imagine the student only gets linear algebra practice problems.\nBut on the exam, they see a problem from calculus! This would not be a great situation for our student.\nSimilarly, if our model is trained mostly on linear algebra questions, it will specialize in those types of questions,\njust like our student. Therefor it will not be as good at solving calculus questions.\nAt the same time, our test set contains a lot more calculus questions than linear algebra ones,\nbecause we used most of the linear algebra questions for training. So not only is our model best at solving linear algebra questions,\nbut our test set also contains fewer algebra questions than any other question category! This is a lose-lose situation.`), mdx(WarningTip, {\n    mdxType: \"WarningTip\"\n  }, mdx(\"p\", null, `What makes this worse is that this mistake can be extremely difficult to find.\nIf your model is underperforming, you will likely try out a more complex model,\nlook for outliers in the dataset, etc. But forgetting to shuffle your dataset before splitting it\nis a very subtle mistake and finding it can cost you a lot of time and energy.\nFortunately, if you are using scikit-learn’s `, mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, `train-test-split`), `-function (which we will take a look at in a second),\nit will shuffle your dataset by default, so even if you forget to set `, mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, `shuffle`), `=True, the function will still do\nit for you. Nevertheless, it is important to remember that shuffling your dataset before splitting it is\nsomething that can have a non-trivial effect on your model performance.`)), mdx(\"p\", null, `So in summary, if your data has an ordering, don’t destroy it, and if it does not have an ordering, make sure to shuffle the dataset before splitting.`), mdx(\"h3\", {\n    \"id\": \"what-is-stratification\"\n  }, `What is Stratification?`), mdx(\"div\", null, mdx(\"p\", null, `In the previous question we saw how `, mdx(\"em\", {\n    parentName: \"p\"\n  }, `having disproportionately many samples of one kind in only the train set or only the test set`), `\nis a very uncomfortable position to be in. Now, what if I told you that there is a way to make sure this `, mdx(\"strong\", {\n    parentName: \"p\"\n  }, `never`), ` happens?`, mdx(ToolTip, {\n    width: \"25em\",\n    trigger: \"*\",\n    mdxType: \"ToolTip\"\n  }, `If you\nread words such as `, mdx(\"em\", {\n    parentName: \"p\"\n  }, `never`), `, `, mdx(\"em\", {\n    parentName: \"p\"\n  }, `always`), `, or `, mdx(\"em\", {\n    parentName: \"p\"\n  }, `every single time`), `, it probably is too good to be true. Frankly,\nthere is no way to make sure this never ever happens, but there is a way to prevent this from happening in a large number of cases,\nso it is still worth taking a look at. We will also look at cases where it’s not so trivial to prevent this.`), `\nThe technique we can use to prevent this is called `, mdx(\"em\", {\n    parentName: \"p\"\n  }, `stratification`), `. Let’s look at a practical example to see how it works.`)), mdx(\"p\", null, `Let’s assume for a second that we are dealing with a classification-based task,\nmeaning we have some features and a finite amount of discrete labels which we are trying to predict using\nthe features. We’ll come back to regression in a moment.\nIn the case of a classification problem, the distribution of labels should be roughly the same for both the training and the testing set.`), mdx(\"p\", null, `So if we think of this in terms of our analogy, if the mathematics exam our student has to pass contains `, mdx(\"span\", {\n    parentName: \"p\",\n    \"className\": \"math math-inline\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"katex\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"katex-mathml\"\n  }, mdx(\"math\", {\n    parentName: \"span\",\n    \"xmlns\": \"http://www.w3.org/1998/Math/MathML\"\n  }, mdx(\"semantics\", {\n    parentName: \"math\"\n  }, mdx(\"mrow\", {\n    parentName: \"semantics\"\n  }, mdx(\"mfrac\", {\n    parentName: \"mrow\"\n  }, mdx(\"mn\", {\n    parentName: \"mfrac\"\n  }, `1`), mdx(\"mn\", {\n    parentName: \"mfrac\"\n  }, `3`))), mdx(\"annotation\", {\n    parentName: \"semantics\",\n    \"encoding\": \"application/x-tex\"\n  }, `\\\\frac{1}{3}`)))), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"katex-html\",\n    \"aria-hidden\": \"true\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"base\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"strut\",\n    \"style\": {\n      \"height\": \"1.190108em\",\n      \"verticalAlign\": \"-0.345em\"\n    }\n  }), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mord\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mopen nulldelimiter\"\n  }), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mfrac\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"vlist-t vlist-t2\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"vlist-r\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"vlist\",\n    \"style\": {\n      \"height\": \"0.845108em\"\n    }\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"style\": {\n      \"top\": \"-2.6550000000000002em\"\n    }\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"pstrut\",\n    \"style\": {\n      \"height\": \"3em\"\n    }\n  }), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"sizing reset-size6 size3 mtight\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mord mtight\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mord mtight\"\n  }, `3`)))), mdx(\"span\", {\n    parentName: \"span\",\n    \"style\": {\n      \"top\": \"-3.23em\"\n    }\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"pstrut\",\n    \"style\": {\n      \"height\": \"3em\"\n    }\n  }), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"frac-line\",\n    \"style\": {\n      \"borderBottomWidth\": \"0.04em\"\n    }\n  })), mdx(\"span\", {\n    parentName: \"span\",\n    \"style\": {\n      \"top\": \"-3.394em\"\n    }\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"pstrut\",\n    \"style\": {\n      \"height\": \"3em\"\n    }\n  }), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"sizing reset-size6 size3 mtight\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mord mtight\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mord mtight\"\n  }, `1`))))), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"vlist-s\"\n  }, `​`)), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"vlist-r\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"vlist\",\n    \"style\": {\n      \"height\": \"0.345em\"\n    }\n  }, mdx(\"span\", {\n    parentName: \"span\"\n  }))))), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mclose nulldelimiter\"\n  })))))), ` linear algebra questions,\n`, mdx(\"span\", {\n    parentName: \"p\",\n    \"className\": \"math math-inline\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"katex\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"katex-mathml\"\n  }, mdx(\"math\", {\n    parentName: \"span\",\n    \"xmlns\": \"http://www.w3.org/1998/Math/MathML\"\n  }, mdx(\"semantics\", {\n    parentName: \"math\"\n  }, mdx(\"mrow\", {\n    parentName: \"semantics\"\n  }, mdx(\"mfrac\", {\n    parentName: \"mrow\"\n  }, mdx(\"mn\", {\n    parentName: \"mfrac\"\n  }, `1`), mdx(\"mn\", {\n    parentName: \"mfrac\"\n  }, `3`))), mdx(\"annotation\", {\n    parentName: \"semantics\",\n    \"encoding\": \"application/x-tex\"\n  }, `\\\\frac{1}{3}`)))), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"katex-html\",\n    \"aria-hidden\": \"true\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"base\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"strut\",\n    \"style\": {\n      \"height\": \"1.190108em\",\n      \"verticalAlign\": \"-0.345em\"\n    }\n  }), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mord\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mopen nulldelimiter\"\n  }), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mfrac\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"vlist-t vlist-t2\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"vlist-r\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"vlist\",\n    \"style\": {\n      \"height\": \"0.845108em\"\n    }\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"style\": {\n      \"top\": \"-2.6550000000000002em\"\n    }\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"pstrut\",\n    \"style\": {\n      \"height\": \"3em\"\n    }\n  }), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"sizing reset-size6 size3 mtight\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mord mtight\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mord mtight\"\n  }, `3`)))), mdx(\"span\", {\n    parentName: \"span\",\n    \"style\": {\n      \"top\": \"-3.23em\"\n    }\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"pstrut\",\n    \"style\": {\n      \"height\": \"3em\"\n    }\n  }), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"frac-line\",\n    \"style\": {\n      \"borderBottomWidth\": \"0.04em\"\n    }\n  })), mdx(\"span\", {\n    parentName: \"span\",\n    \"style\": {\n      \"top\": \"-3.394em\"\n    }\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"pstrut\",\n    \"style\": {\n      \"height\": \"3em\"\n    }\n  }), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"sizing reset-size6 size3 mtight\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mord mtight\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mord mtight\"\n  }, `1`))))), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"vlist-s\"\n  }, `​`)), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"vlist-r\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"vlist\",\n    \"style\": {\n      \"height\": \"0.345em\"\n    }\n  }, mdx(\"span\", {\n    parentName: \"span\"\n  }))))), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mclose nulldelimiter\"\n  })))))), ` calculus questions, and `, mdx(\"span\", {\n    parentName: \"p\",\n    \"className\": \"math math-inline\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"katex\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"katex-mathml\"\n  }, mdx(\"math\", {\n    parentName: \"span\",\n    \"xmlns\": \"http://www.w3.org/1998/Math/MathML\"\n  }, mdx(\"semantics\", {\n    parentName: \"math\"\n  }, mdx(\"mrow\", {\n    parentName: \"semantics\"\n  }, mdx(\"mfrac\", {\n    parentName: \"mrow\"\n  }, mdx(\"mn\", {\n    parentName: \"mfrac\"\n  }, `1`), mdx(\"mn\", {\n    parentName: \"mfrac\"\n  }, `3`))), mdx(\"annotation\", {\n    parentName: \"semantics\",\n    \"encoding\": \"application/x-tex\"\n  }, `\\\\frac{1}{3}`)))), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"katex-html\",\n    \"aria-hidden\": \"true\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"base\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"strut\",\n    \"style\": {\n      \"height\": \"1.190108em\",\n      \"verticalAlign\": \"-0.345em\"\n    }\n  }), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mord\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mopen nulldelimiter\"\n  }), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mfrac\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"vlist-t vlist-t2\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"vlist-r\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"vlist\",\n    \"style\": {\n      \"height\": \"0.845108em\"\n    }\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"style\": {\n      \"top\": \"-2.6550000000000002em\"\n    }\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"pstrut\",\n    \"style\": {\n      \"height\": \"3em\"\n    }\n  }), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"sizing reset-size6 size3 mtight\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mord mtight\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mord mtight\"\n  }, `3`)))), mdx(\"span\", {\n    parentName: \"span\",\n    \"style\": {\n      \"top\": \"-3.23em\"\n    }\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"pstrut\",\n    \"style\": {\n      \"height\": \"3em\"\n    }\n  }), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"frac-line\",\n    \"style\": {\n      \"borderBottomWidth\": \"0.04em\"\n    }\n  })), mdx(\"span\", {\n    parentName: \"span\",\n    \"style\": {\n      \"top\": \"-3.394em\"\n    }\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"pstrut\",\n    \"style\": {\n      \"height\": \"3em\"\n    }\n  }), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"sizing reset-size6 size3 mtight\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mord mtight\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mord mtight\"\n  }, `1`))))), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"vlist-s\"\n  }, `​`)), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"vlist-r\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"vlist\",\n    \"style\": {\n      \"height\": \"0.345em\"\n    }\n  }, mdx(\"span\", {\n    parentName: \"span\"\n  }))))), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mclose nulldelimiter\"\n  })))))), ` probability questions, then the exercise sheets should roughly follow these relative amounts.`), mdx(\"p\", null, `When we split our dataset in this particular way,\nmeaning we make the training and testing set have as similar as possible label distributions,\nwe say that we create a `, mdx(\"em\", {\n    parentName: \"p\"\n  }, `stratified`), ` split.`), mdx(\"p\", null, `How does this work under the hood? I don’t want to dive into too much detail,\nbut let’s look at how we might implement stratification ourselves.\nLet’s say we have a dataset consisting of X and of y.\nX is just an array of length 10 (the contents of X are not important for our example) and y is `, mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, `[ 0, 0, 1, 1, 0, 1, 0, 0, 1, 1 ]`), `.\nIt’s not really important what this data represents, we’ll just use it as “dummy” data to better understand stratification.`), mdx(\"p\", null, `To start off, we can count the number of samples for each class,\nand then calculate each class percentage with regard to the size of the dataset.\nIn our example, we have 10 samples with 2 distinct labels 0 and 1, and each label appears 5 times.\nThis means that the relative frequency of label 0 is 0.5 = 50%, and the same is true for label 1.\nNow, we can calculate the number of samples needed for each class in our subsets.`), mdx(\"p\", null, `Let’s say we want a train set with a size of 80% of the original dataset and a test set with a size of the remaining 20%.\nSince we have 10 samples, our train set will contain 8 samples and our test set will contain 2 samples.\nWe now get the amounts of samples per label by multiplying the frequency of that label in our dataset by\nthe subset size. In other words, the number of samples with the label 0 in the train set\nis equal to the label frequency of label 0 (0.5) multiplied by the size of the train set (8).\nThis gives us 4. Similarly, we will need 0.5*2 = 1 samples with label 0 in our test set.\nSince label 0 and label 1 appear the same number of times in the dataset, the number of samples for label 1\nare the same as for label 0.\nNow we know how many samples per label each subset should have.`), mdx(\"div\", null, mdx(\"p\", null, `The last step is to just pick samples with the corresponding labels and pack them together into a train set and a test set.\nWe can do so by extracting all samples from the dataset that have a specific label, and then pick\nfrom the extracted values either randomly or in order.`, mdx(ToolTip, {\n    width: \"25em\",\n    trigger: \"*\",\n    mdxType: \"ToolTip\"\n  }, `Note that in practice,\nthere are more cases that need to be considered. Here we only looked at a small dataset with only two labels,\nbut you can also have a lot more labels and even multiple labels per sample. This should only serve as a small\nmental image as to how stratification might be implemented. `))), mdx(\"p\", null, `So if you now come across a phrase like ” to create a stratified split”, you will know exactly what it means.\nUsing stratification in your splits can be extremely easy, and we’ll take a look at some practical examples in the next section.\nBut what about regression?`), mdx(\"p\", null, `Since we are predicting a continuous range of values in regression,\nwe can’t “count” the labels in the same way as we did for classification.\nIn general, this means that stratification is not so trivial for regression,\nand as far as I know, there is no `, mdx(\"em\", {\n    parentName: \"p\"\n  }, `one-liner`), ` to create a “stratified” split for regression.\nHowever, that does not mean it’s not possible to come up with some alternatives.\nI don’t want to go into too much detail\nin this article because the topic is worthy of its own discussion, but if you are interested in reading more about this, I recommend\nyou take a look at `, mdx(\"a\", {\n    parentName: \"p\",\n    \"href\": \"https://scottclowe.com/2016-03-19-stratified-regression-partitions/\",\n    \"target\": \"_blank\",\n    \"rel\": \"nofollow noopener noreferrer\"\n  }, `this blog article by Scott C. Lowe`), `,\nwhich presents a couple of ideas in more detail.`), mdx(\"h3\", {\n    \"id\": \"how-can-i-efficiently-split-my-dataset-with-code\"\n  }, `How can I Efficiently Split my Dataset with Code?`), mdx(\"p\", null, `So how can you split your dataset without having to write unnecessarily large amounts of code?\nIt’s actually quite easy. All it takes is one line of scikit-learn-code and you’re done.\nLet’s take a look:`), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-python\"\n  }, `from sklearn.model_selection import train_test_split\n\nX_train, X_test, y_train, y_test = train_test_split(X,y, test_size=0.3, random_state=42, shuffle=True)\n`)), mdx(\"p\", null, `We first import the famous `, mdx(\"a\", {\n    parentName: \"p\",\n    \"href\": \"https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.train_test_split.html\",\n    \"target\": \"_blank\",\n    \"rel\": \"nofollow noopener noreferrer\"\n  }, mdx(\"inlineCode\", {\n    parentName: \"a\"\n  }, `train_test_split`)), `-function from scikit-learn, which takes the following parameters:`), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-python\"\n  }, `(*arrays, test_size=None, train_size=None, random_state=None, shuffle=True, stratify=None)\n`)), mdx(\"p\", null, `You only need to provide an argument for either the `, mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, `train_size`), ` or the `, mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, `test_size`), `, because the other one will be set to just 1-(the size you provided).\nIf you provide a value for `, mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, `random_state`), `, and execute this line of code multiple times, it will\nalways split the dataset in the same way. If you do not provide a value for `, mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, `random_state`), `, the split will be different every time.\nIf shuffle is true, then the dataset is shuffled before it is split.\nAnd lastly, we have `, mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, `stratify`), `. Here, you can provide the subset which should be used to command the stratification of the dataset.\nLet’s look at two quick examples.`), mdx(\"h4\", {\n    \"id\": \"practical-example-regression\"\n  }, `Practical Example: Regression`), mdx(\"p\", null, `Let’s consider the `, mdx(SmoothScrollLink, {\n    scrollid: \"a-practical-example\",\n    mdxType: \"SmoothScrollLink\"\n  }, `aforementioned dataset`), `\nof exam scores. We have two arrays, namely X and y. X contains the number of hours every student studied,\nand y contains the number of points each student achieved on the exam.`), mdx(\"p\", null, `Now we want to split this dataset into a train set containing 80% of the original\ndata and a test set containing 20% of the original data.\nWe also want to make the splitting reproducible.\nWe can do this with the following line of code:`), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-python\"\n  }, `X_train, X_test, y_train, y_test = train_test_split(X,y, test_size=0.2, random_state=42, shuffle=True)\n`)), mdx(\"p\", null, `And that’s it! It really is that easy.\nIf we want to, we can inspect our new train-and test-set with a simple matplotlib-plot:`), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-python\"\n  }, `plt.plot(X_train,y_train,\".b\",label=\"train\",markersize=12) # \".b\" means plot individual markers (\".\") in blue (\"b\")\nplt.plot(X_test,y_test,\".g\",label=\"test\",markersize=12) # \".g\" means plot individual markers (\".\") in green (\"g\")\nplt.legend()\nplt.show()\n`)), mdx(\"p\", null, `This yields the following plot:`), mdx(\"p\", null, mdx(\"span\", {\n    parentName: \"p\",\n    \"className\": \"gatsby-resp-image-wrapper\",\n    \"style\": {\n      \"position\": \"relative\",\n      \"display\": \"block\",\n      \"marginLeft\": \"auto\",\n      \"marginRight\": \"auto\",\n      \"maxWidth\": \"432px\"\n    }\n  }, `\n      `, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"gatsby-resp-image-background-image\",\n    \"style\": {\n      \"paddingBottom\": \"66.66666666666666%\",\n      \"position\": \"relative\",\n      \"bottom\": \"0\",\n      \"left\": \"0\",\n      \"backgroundImage\": \"url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAYAAACpUE5eAAAACXBIWXMAAAsTAAALEwEAmpwYAAABfklEQVQ4y4VTCXLDIBDL/5/YI5m4iVODCaacPkCdxWdSZ8oMBrOsWAlxSClhbXGdxQiK7fUYKSdN+8f5CJNwoEEphbquwasAxhiklHDOwVq7252z+K4sRB3AuMG5kBiGIReSAdu2RSMlzkzg83iE1hqv2kzoWjmceIn3W4WP62UhlwHpVKqwHTxCCJnumLxPmagVRYJtHaQaYJ1F3/crIJVrjH6qJL38FyKB80k+AMEHdF23rdCAsRqjDGlJfqwKkE7hyG7ou8dDvPcr4Hw7UhJogjF4uDUa58SvMoAJP7kg7QPSxxgDIeopuNULCy+lexg7ahs3DHYB6TTnNLROCOGxQtKapKgahcbaXSn+AFrrcL+LnKwU0U+LYclBbxeG95NH60fz/wtIgdl7UgJbGxo7QHcKoxvSrjfJx0+UIzjnaBoF78mTDM75rGtR/MBoD84ZQvD5JdHh8+ui6sqy3N5ywuxFWiQ9zXjV+fnFSGvDskYj7SUjU5zyaW3G+QWpJvrH0h6vIQAAAABJRU5ErkJggg==')\",\n      \"backgroundSize\": \"cover\",\n      \"display\": \"block\"\n    }\n  }), `\n  `, mdx(\"picture\", {\n    parentName: \"span\"\n  }, `\n          `, mdx(\"source\", {\n    parentName: \"picture\",\n    \"srcSet\": [\"/static/85935f7a41c5ae14fe68f995558dbcf8/e3a5e/trainTestSplit.webp 432w\"],\n    \"sizes\": \"(max-width: 432px) 100vw, 432px\",\n    \"type\": \"image/webp\"\n  }), `\n          `, mdx(\"source\", {\n    parentName: \"picture\",\n    \"srcSet\": [\"/static/85935f7a41c5ae14fe68f995558dbcf8/dc3b1/trainTestSplit.png 432w\"],\n    \"sizes\": \"(max-width: 432px) 100vw, 432px\",\n    \"type\": \"image/png\"\n  }), `\n          `, mdx(\"img\", {\n    parentName: \"picture\",\n    \"className\": \"gatsby-resp-image-image\",\n    \"src\": \"/static/85935f7a41c5ae14fe68f995558dbcf8/dc3b1/trainTestSplit.png\",\n    \"alt\": \"Train and Test-Subsets Plotted\",\n    \"title\": \"Train and Test-Subsets Plotted\",\n    \"loading\": \"lazy\",\n    \"decoding\": \"async\",\n    \"style\": {\n      \"width\": \"100%\",\n      \"height\": \"100%\",\n      \"margin\": \"0\",\n      \"verticalAlign\": \"middle\",\n      \"position\": \"absolute\",\n      \"top\": \"0\",\n      \"left\": \"0\"\n    }\n  }), `\n        `), `\n    `)), mdx(\"p\", null, `Now let’s take a look at a classification-based example.`), mdx(\"h4\", {\n    \"id\": \"practical-example-classification\"\n  }, `Practical Example: Classification`), mdx(\"p\", null, `Now let’s say we have another dataset,\ndescribing exam tasks. The dataset has two features as well as one label value.\nEach main-task is described by 1) the number of sub-tasks the main-task has and 2) the number of characters\nthe main-task has. So you can imagine that there is a main-task 1 with\nsubtasks 1a, 1b, and so on (or 1.1, 1.2, etc.). Each task (main + subtasks) is then\ncategorized into three levels of difficulty, namely `, mdx(\"em\", {\n    parentName: \"p\"\n  }, `easy`), `, `, mdx(\"em\", {\n    parentName: \"p\"\n  }, `medium`), `, and `, mdx(\"em\", {\n    parentName: \"p\"\n  }, `hard`), `.`), mdx(\"p\", null, `The dataset looks like this:`), mdx(ConditionalLazyPlot, _extends({}, assignmentsClassification, {\n    mdxType: \"ConditionalLazyPlot\"\n  }), mdx(\"p\", null, mdx(\"span\", {\n    parentName: \"p\",\n    \"className\": \"gatsby-resp-image-wrapper\",\n    \"style\": {\n      \"position\": \"relative\",\n      \"display\": \"block\",\n      \"marginLeft\": \"auto\",\n      \"marginRight\": \"auto\",\n      \"maxWidth\": \"1097px\"\n    }\n  }, `\n      `, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"gatsby-resp-image-background-image\",\n    \"style\": {\n      \"paddingBottom\": \"61.6%\",\n      \"position\": \"relative\",\n      \"bottom\": \"0\",\n      \"left\": \"0\",\n      \"backgroundImage\": \"url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAMCAYAAABiDJ37AAAACXBIWXMAABYlAAAWJQFJUiTwAAAB6UlEQVQoz11T2XKbQBCkhAQIAXtwyhI3SOi0ElyJk9j//1md2iEIk4euHYadru6ZWW0lcix5hiXPIdMzovJOCIsbgvyGsLwjrl7BXjq6R/dFhhXPMdUOeQVtDBQMqVBgNTsHPIv/kamTiNT/IMfC20NnKTSVVME6KKlI5yki2cMVJ0TyDbZo4YkzkQh5I6gaQxTg/AozqbD//o76+gtWUEGzgpLseNvDU4Exqub5ZE1MtqY4g8kbZNUHjv0n7LCG5iYHyOwCUxZEZIgShl9ipSyrb78aoPKyHE5ekMKVghLBssmyYuW7E5ykgaFshQ846QNWfMQ6OUIc/oA17+DBHSzrIcNvkOIGX77CFd1MOQ1lEzdEaPkF2TPDhmD4NXQvpTaYYQszamFFB1L/HI6YWjIOjQj97IJNVMPetdjs77C3d3jFA04+9NUrenjlG1j9E+u4w5KlM1WztVErYfol7LCC6M6wXyqYcYH9Z4/d7x4LNyWCJU+hs6FPI4k+7uVXQloBWcBNWthJDSdrEZxuyD5+YL2tYPkVlAt1bx1WcOKWYrUiTtLOyJ6LPV62/BILdw+27eCELU2O7TqI9EzK3O0Bfn6l/CZuKdb/s68tv8ientbwDIP8Sjsq08sQ7zoaoHKks2xmf6z9C2MOLCu+znDzAAAAAElFTkSuQmCC')\",\n      \"backgroundSize\": \"cover\",\n      \"display\": \"block\"\n    }\n  }), `\n  `, mdx(\"picture\", {\n    parentName: \"span\"\n  }, `\n          `, mdx(\"source\", {\n    parentName: \"picture\",\n    \"srcSet\": [\"/static/833217134e4ad7f22ee451b50fda0a33/601b1/taskClassificationDataset.webp 500w\", \"/static/833217134e4ad7f22ee451b50fda0a33/80c60/taskClassificationDataset.webp 1000w\", \"/static/833217134e4ad7f22ee451b50fda0a33/9186d/taskClassificationDataset.webp 1097w\"],\n    \"sizes\": \"(max-width: 1097px) 100vw, 1097px\",\n    \"type\": \"image/webp\"\n  }), `\n          `, mdx(\"source\", {\n    parentName: \"picture\",\n    \"srcSet\": [\"/static/833217134e4ad7f22ee451b50fda0a33/0eb09/taskClassificationDataset.png 500w\", \"/static/833217134e4ad7f22ee451b50fda0a33/1263b/taskClassificationDataset.png 1000w\", \"/static/833217134e4ad7f22ee451b50fda0a33/c37c4/taskClassificationDataset.png 1097w\"],\n    \"sizes\": \"(max-width: 1097px) 100vw, 1097px\",\n    \"type\": \"image/png\"\n  }), `\n          `, mdx(\"img\", {\n    parentName: \"picture\",\n    \"className\": \"gatsby-resp-image-image\",\n    \"src\": \"/static/833217134e4ad7f22ee451b50fda0a33/c37c4/taskClassificationDataset.png\",\n    \"alt\": \"Task Difficulty Classification Dataset\",\n    \"title\": \"Task Difficulty Classification Dataset\",\n    \"loading\": \"lazy\",\n    \"decoding\": \"async\",\n    \"style\": {\n      \"width\": \"100%\",\n      \"height\": \"100%\",\n      \"margin\": \"0\",\n      \"verticalAlign\": \"middle\",\n      \"position\": \"absolute\",\n      \"top\": \"0\",\n      \"left\": \"0\"\n    }\n  }), `\n        `), `\n    `))), mdx(\"p\", null, `Now we want to split this dataset into a train set containing 70% of the original data,\nand a test set containing the remaining 30% and make the splitting reproducible.\nWe can do this with the following line of code:`), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-python\"\n  }, `X_train, X_test, y_train, y_test = train_test_split(X,y, test_size=0.3, random_state=42, shuffle=True)\n`)), mdx(\"p\", null, `If we also want to stratify our split with regard to our labels, we can do so by passing in `, mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, `y`), ` as the value\nfor the argument `, mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, `stratify`), `, like so:`), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-python\"\n  }, `X_train_s, X_test_s, y_train_s, y_test_s = train_test_split(X,y, test_size=0.3, random_state=42, shuffle=True, stratify=y)\n`)), mdx(\"p\", null, `To make sure our stratification went as planned, we can use a few lines of matplotlib-code\nto display the label histograms of both our train set as well as our test set in the case where we did\nprovide an argument for stratification, and in the one we did not. Let’s create the plot:`), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-python\"\n  }, `fig,ax=plt.subplots(1,2,figsize=(10,5))\n\nax[0].hist([y_train_s,y_test_s],label=[\"train\",\"test\"],stacked=False)\nax[0].set_xlabel(\"label\")\nax[0].set_ylabel(\"num samples\")\nax[0].set_title(\"stratified\")\nax[0].legend()\n\nax[1].hist([y_train,y_test],label=[\"train\",\"test\"],stacked=False)\nax[1].set_xlabel(\"label\")\nax[1].set_ylabel(\"num samples\")\nax[1].set_title(\"not stratified\")\nax[1].legend()\n`)), mdx(\"p\", null, `This yields the following image:`), mdx(\"p\", null, mdx(\"span\", {\n    parentName: \"p\",\n    \"className\": \"gatsby-resp-image-wrapper\",\n    \"style\": {\n      \"position\": \"relative\",\n      \"display\": \"block\",\n      \"marginLeft\": \"auto\",\n      \"marginRight\": \"auto\",\n      \"maxWidth\": \"720px\"\n    }\n  }, `\n      `, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"gatsby-resp-image-background-image\",\n    \"style\": {\n      \"paddingBottom\": \"50%\",\n      \"position\": \"relative\",\n      \"bottom\": \"0\",\n      \"left\": \"0\",\n      \"backgroundImage\": \"url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAKCAYAAAC0VX7mAAAACXBIWXMAAAsTAAALEwEAmpwYAAABtElEQVQoz22SfW/aMBDG+f6fZtL216ShTeNFo1AxVHXreFkHlAJN4hjHJLHj/CY7hHbSTjrdOec8zz3n69R1jTcfnXPBT6cTmZK4NzVvvuZzrc+kSYzOMlpr73Tag9aaoiiorCUSksUuRivJcrkIBN59vTSGssiZb4+IU8bTds1ut8Na+wromZVSXEhItGGwTIAKLRPOeY4QSdPhpaPxH0GaW9xZojOFMeZfwCzLQgyAWclw8dJIAcqyRAjRjOTCOnlMELpsRlHXmLJ8BUzTFJmK6zw8YD90CNNNymr7HGbmXP1fQGoXSK+AmVLso4TcNHOIT5r+aBTywSphsdkjRXztppUsc4t1NU9piatsUNNpL0x/H1mLhiWWin73fQO4jDgKRSolz7K4qhivDqhzwSm3DKd3UJW4t4C3D4+sl/dXwN6nDyHvzR6If/bZq4pBrwtWhe83w89kLxuyCkbdd1CqBrDdn/tfczZ3/dD2MYqZfPviBXL7Y040HxNpx2zwESMP4cfvkyHpYRPmOLv5CkY3kltA/+y2cmGf/Iv7vfS5d2MtrqpCDG4MaSpDbq0J53ZD/gKilACDqd/Q8wAAAABJRU5ErkJggg==')\",\n      \"backgroundSize\": \"cover\",\n      \"display\": \"block\"\n    }\n  }), `\n  `, mdx(\"picture\", {\n    parentName: \"span\"\n  }, `\n          `, mdx(\"source\", {\n    parentName: \"picture\",\n    \"srcSet\": [\"/static/d519b4dcb6a909ad2b724ca8084ac673/601b1/stratifiedSplitAbsolute.webp 500w\", \"/static/d519b4dcb6a909ad2b724ca8084ac673/df77d/stratifiedSplitAbsolute.webp 720w\"],\n    \"sizes\": \"(max-width: 720px) 100vw, 720px\",\n    \"type\": \"image/webp\"\n  }), `\n          `, mdx(\"source\", {\n    parentName: \"picture\",\n    \"srcSet\": [\"/static/d519b4dcb6a909ad2b724ca8084ac673/0eb09/stratifiedSplitAbsolute.png 500w\", \"/static/d519b4dcb6a909ad2b724ca8084ac673/242a6/stratifiedSplitAbsolute.png 720w\"],\n    \"sizes\": \"(max-width: 720px) 100vw, 720px\",\n    \"type\": \"image/png\"\n  }), `\n          `, mdx(\"img\", {\n    parentName: \"picture\",\n    \"className\": \"gatsby-resp-image-image\",\n    \"src\": \"/static/d519b4dcb6a909ad2b724ca8084ac673/242a6/stratifiedSplitAbsolute.png\",\n    \"alt\": \"Stratified vs non-stratified label distributions with absolute values\",\n    \"title\": \"Stratified vs non-stratified label distributions with absolute values\",\n    \"loading\": \"lazy\",\n    \"decoding\": \"async\",\n    \"style\": {\n      \"width\": \"100%\",\n      \"height\": \"100%\",\n      \"margin\": \"0\",\n      \"verticalAlign\": \"middle\",\n      \"position\": \"absolute\",\n      \"top\": \"0\",\n      \"left\": \"0\"\n    }\n  }), `\n        `), `\n    `)), mdx(\"h4\", {\n    \"id\": \"a-mistake\"\n  }, `A Mistake?`), mdx(\"p\", null, `The above plot does look a bit strange.. the histograms of the train set and the test set\ndon’t seem to have similar column heights.\nDid our stratification fail? No, not at all! The issue here is that our histplot\nshows us the `, mdx(\"em\", {\n    parentName: \"p\"\n  }, `absolute`), ` amounts for each label. This means that it shows us the number\nof samples with label=1, label=2, label=3 there are in both sets. But in that case,\nif our test set is smaller than our train set, the amounts of samples for any label will probably\nbe larger in our train set. So how can we overcome this? We can add `, mdx(\"em\", {\n    parentName: \"p\"\n  }, `weights`), ` to the histplot\nto get the relative values. Normally, each sample in a set is weighted with 1, which seems logical.\nBut if we instead weigh every sample in the set with `, mdx(\"span\", {\n    parentName: \"p\",\n    \"className\": \"math math-inline\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"katex\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"katex-mathml\"\n  }, mdx(\"math\", {\n    parentName: \"span\",\n    \"xmlns\": \"http://www.w3.org/1998/Math/MathML\"\n  }, mdx(\"semantics\", {\n    parentName: \"math\"\n  }, mdx(\"mrow\", {\n    parentName: \"semantics\"\n  }, mdx(\"mfrac\", {\n    parentName: \"mrow\"\n  }, mdx(\"mn\", {\n    parentName: \"mfrac\"\n  }, `1`), mdx(\"mrow\", {\n    parentName: \"mfrac\"\n  }, mdx(\"mi\", {\n    parentName: \"mrow\"\n  }, `d`), mdx(\"mi\", {\n    parentName: \"mrow\"\n  }, `a`), mdx(\"mi\", {\n    parentName: \"mrow\"\n  }, `t`), mdx(\"mi\", {\n    parentName: \"mrow\"\n  }, `a`), mdx(\"mi\", {\n    parentName: \"mrow\"\n  }, `s`), mdx(\"mi\", {\n    parentName: \"mrow\"\n  }, `e`), mdx(\"mi\", {\n    parentName: \"mrow\"\n  }, `t`), mdx(\"mtext\", {\n    parentName: \"mrow\"\n  }, ` `), mdx(\"mi\", {\n    parentName: \"mrow\"\n  }, `s`), mdx(\"mi\", {\n    parentName: \"mrow\"\n  }, `i`), mdx(\"mi\", {\n    parentName: \"mrow\"\n  }, `z`), mdx(\"mi\", {\n    parentName: \"mrow\"\n  }, `e`)))), mdx(\"annotation\", {\n    parentName: \"semantics\",\n    \"encoding\": \"application/x-tex\"\n  }, `\\\\frac{1}{dataset\\\\ size}`)))), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"katex-html\",\n    \"aria-hidden\": \"true\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"base\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"strut\",\n    \"style\": {\n      \"height\": \"1.190108em\",\n      \"verticalAlign\": \"-0.345em\"\n    }\n  }), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mord\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mopen nulldelimiter\"\n  }), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mfrac\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"vlist-t vlist-t2\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"vlist-r\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"vlist\",\n    \"style\": {\n      \"height\": \"0.845108em\"\n    }\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"style\": {\n      \"top\": \"-2.6550000000000002em\"\n    }\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"pstrut\",\n    \"style\": {\n      \"height\": \"3em\"\n    }\n  }), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"sizing reset-size6 size3 mtight\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mord mtight\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mord mathnormal mtight\"\n  }, `d`), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mord mathnormal mtight\"\n  }, `a`), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mord mathnormal mtight\"\n  }, `t`), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mord mathnormal mtight\"\n  }, `a`), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mord mathnormal mtight\"\n  }, `s`), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mord mathnormal mtight\"\n  }, `e`), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mord mathnormal mtight\"\n  }, `t`), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mspace mtight\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mtight\"\n  }, ` `)), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mord mathnormal mtight\"\n  }, `s`), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mord mathnormal mtight\"\n  }, `i`), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mord mathnormal mtight\",\n    \"style\": {\n      \"marginRight\": \"0.04398em\"\n    }\n  }, `z`), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mord mathnormal mtight\"\n  }, `e`)))), mdx(\"span\", {\n    parentName: \"span\",\n    \"style\": {\n      \"top\": \"-3.23em\"\n    }\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"pstrut\",\n    \"style\": {\n      \"height\": \"3em\"\n    }\n  }), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"frac-line\",\n    \"style\": {\n      \"borderBottomWidth\": \"0.04em\"\n    }\n  })), mdx(\"span\", {\n    parentName: \"span\",\n    \"style\": {\n      \"top\": \"-3.394em\"\n    }\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"pstrut\",\n    \"style\": {\n      \"height\": \"3em\"\n    }\n  }), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"sizing reset-size6 size3 mtight\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mord mtight\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mord mtight\"\n  }, `1`))))), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"vlist-s\"\n  }, `​`)), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"vlist-r\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"vlist\",\n    \"style\": {\n      \"height\": \"0.345em\"\n    }\n  }, mdx(\"span\", {\n    parentName: \"span\"\n  }))))), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mclose nulldelimiter\"\n  })))))), `, then the sum\nof all weighted samples from one dataset will be exactly 1. This way we can display the relative\nclass percentages instead of the absolute class amounts in our histplot. The adjusted code looks like\nthis:`), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-python\",\n    \"metastring\": \"{0-2,6,12}\",\n    \"{0-2,6,12}\": true\n  }, `weights = [np.ones_like(y_train_s) / y_train_s.size, # np.ones_like generates an array of ones \n           np.ones_like(y_test_s) / y_test_s.size]   # with the shape of the passed array\n\nfig,ax=plt.subplots(1,2,figsize=(10,5))\n\nax[0].hist([y_train_s,y_test_s],label=[\"train\",\"test\"],stacked=False, weights=weights)\nax[0].set_xlabel(\"label\")\nax[0].set_ylabel(\"num samples\")\nax[0].set_title(\"stratified\")\nax[0].legend()\n\nax[1].hist([y_train,y_test],label=[\"train\",\"test\"],stacked=False, weights=weights)\nax[1].set_xlabel(\"label\")\nax[1].set_ylabel(\"num samples\")\nax[1].set_title(\"not stratified\")\nax[1].legend()\n`)), mdx(\"p\", null, `which yields the following plot:`), mdx(\"p\", null, mdx(\"span\", {\n    parentName: \"p\",\n    \"className\": \"gatsby-resp-image-wrapper\",\n    \"style\": {\n      \"position\": \"relative\",\n      \"display\": \"block\",\n      \"marginLeft\": \"auto\",\n      \"marginRight\": \"auto\",\n      \"maxWidth\": \"720px\"\n    }\n  }, `\n      `, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"gatsby-resp-image-background-image\",\n    \"style\": {\n      \"paddingBottom\": \"50%\",\n      \"position\": \"relative\",\n      \"bottom\": \"0\",\n      \"left\": \"0\",\n      \"backgroundImage\": \"url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAKCAYAAAC0VX7mAAAACXBIWXMAAAsTAAALEwEAmpwYAAABvUlEQVQoz1WSa2/aMBSG+f+/Zd8q9mEVhVYMaDuQoKOlA6oCpaKBXJzYuTgJz2SHgHoky8c58ntx3sbxeMSU2cuytEuIkCCUnEbfZqZXKibwXJSU1FXjNOpDHMekaYrWOVEoULslIvB5e1sShiFSSjvPtCZLE+YfDn4o2X6s+fzckuf5BdAwK6XOLJGKeb7/BWVKJDziJLFqzPwkmj/vHkGcUyYCFcdorb8DGoVmN+WKiG7759lOkedWZVkUlCfS4Vrgx5WqUnnoNLkABkFAFEVndk9E9DsV4MZLcFyfvfOFoTsDTl/xfa+6sOqTKXEBNOzipKAG/N1u2v5usmD11EWoDNx/1qK13Gshdu8YjdvxDWUSWkGN+t2eZnO2015lORBnwP5khvO3jchhN2qCdCrAwS3RfkOood9pgpbWQaO2MJrOWI2uT4Ah3daV7bvjGYdph30KvesfkLr2+2PvBrlfIwt46FyBjirAWuHzfMnm5cHKdg4uw/tb82KMXhYcFkNcVTIZtNChYy+ORwOCrw2+Spk83oFWleU6h1mWkReFzZPJnImR6c3SeU5RFDYaJqdmNz9S27m25zoh/wFis/vS0EDMpwAAAABJRU5ErkJggg==')\",\n      \"backgroundSize\": \"cover\",\n      \"display\": \"block\"\n    }\n  }), `\n  `, mdx(\"picture\", {\n    parentName: \"span\"\n  }, `\n          `, mdx(\"source\", {\n    parentName: \"picture\",\n    \"srcSet\": [\"/static/12e272ec8cf44b2e8d6b3db05d370f76/601b1/stratifiedSplitRelative.webp 500w\", \"/static/12e272ec8cf44b2e8d6b3db05d370f76/df77d/stratifiedSplitRelative.webp 720w\"],\n    \"sizes\": \"(max-width: 720px) 100vw, 720px\",\n    \"type\": \"image/webp\"\n  }), `\n          `, mdx(\"source\", {\n    parentName: \"picture\",\n    \"srcSet\": [\"/static/12e272ec8cf44b2e8d6b3db05d370f76/0eb09/stratifiedSplitRelative.png 500w\", \"/static/12e272ec8cf44b2e8d6b3db05d370f76/242a6/stratifiedSplitRelative.png 720w\"],\n    \"sizes\": \"(max-width: 720px) 100vw, 720px\",\n    \"type\": \"image/png\"\n  }), `\n          `, mdx(\"img\", {\n    parentName: \"picture\",\n    \"className\": \"gatsby-resp-image-image\",\n    \"src\": \"/static/12e272ec8cf44b2e8d6b3db05d370f76/242a6/stratifiedSplitRelative.png\",\n    \"alt\": \"Stratified vs non-stratified label distributions with relative values\",\n    \"title\": \"Stratified vs non-stratified label distributions with relative values\",\n    \"loading\": \"lazy\",\n    \"decoding\": \"async\",\n    \"style\": {\n      \"width\": \"100%\",\n      \"height\": \"100%\",\n      \"margin\": \"0\",\n      \"verticalAlign\": \"middle\",\n      \"position\": \"absolute\",\n      \"top\": \"0\",\n      \"left\": \"0\"\n    }\n  }), `\n        `), `\n    `)), mdx(\"p\", null, `Here, we can clearly see the difference between the stratified split and\nthe non-stratified (random) split. Nice!`), mdx(WarningTip, {\n    mdxType: \"WarningTip\"\n  }, mdx(\"p\", null, mdx(\"strong\", {\n    parentName: \"p\"\n  }, `“Breaking” Stratification`)), mdx(\"p\", null, `You may have noticed in the above image that even though our stratification worked correctly,\nthe label amounts weren’t `, mdx(\"em\", {\n    parentName: \"p\"\n  }, `exactly`), ` equal in the train and test sets. They were off by just a little bit.\nLet’s look at a smaller example to understand why this is the case.\nIf you have a dataset like this one:`), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-python\"\n  }, `X = np.arange(10) # = array containing the values 1..9, but X is not so important for this example\ny = [1, 0, 0, 0, 0, 0, 0, 0, 0, 1] # 9 zeros and 1 one\n`)), mdx(\"p\", null, `and you want to split this dataset with an 80-20-ratio using stratification,\nlike so:`), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-python\"\n  }, `X_train, X_test, y_train, y_test = train_test_split(X,y, test_size=0.2, random_state=42, shuffle=True, stratify=y)\n`)), mdx(\"p\", null, `then you might end up with the following sets:`), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-python\"\n  }, `>>> y_train\n[0, 1, 0, 0, 0, 1, 0, 0]\n>>> y_test\n[0, 0]\n`)), mdx(\"p\", null, `Notice how the test set does not contain a single label of 1! This is because the relative amount of ones\nin our entire dataset is `, mdx(\"span\", {\n    parentName: \"p\",\n    \"className\": \"math math-inline\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"katex\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"katex-mathml\"\n  }, mdx(\"math\", {\n    parentName: \"span\",\n    \"xmlns\": \"http://www.w3.org/1998/Math/MathML\"\n  }, mdx(\"semantics\", {\n    parentName: \"math\"\n  }, mdx(\"mrow\", {\n    parentName: \"semantics\"\n  }, mdx(\"mfrac\", {\n    parentName: \"mrow\"\n  }, mdx(\"mn\", {\n    parentName: \"mfrac\"\n  }, `2`), mdx(\"mn\", {\n    parentName: \"mfrac\"\n  }, `10`))), mdx(\"annotation\", {\n    parentName: \"semantics\",\n    \"encoding\": \"application/x-tex\"\n  }, `\\\\frac{2}{10}`)))), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"katex-html\",\n    \"aria-hidden\": \"true\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"base\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"strut\",\n    \"style\": {\n      \"height\": \"1.190108em\",\n      \"verticalAlign\": \"-0.345em\"\n    }\n  }), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mord\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mopen nulldelimiter\"\n  }), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mfrac\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"vlist-t vlist-t2\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"vlist-r\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"vlist\",\n    \"style\": {\n      \"height\": \"0.845108em\"\n    }\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"style\": {\n      \"top\": \"-2.6550000000000002em\"\n    }\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"pstrut\",\n    \"style\": {\n      \"height\": \"3em\"\n    }\n  }), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"sizing reset-size6 size3 mtight\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mord mtight\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mord mtight\"\n  }, `1`), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mord mtight\"\n  }, `0`)))), mdx(\"span\", {\n    parentName: \"span\",\n    \"style\": {\n      \"top\": \"-3.23em\"\n    }\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"pstrut\",\n    \"style\": {\n      \"height\": \"3em\"\n    }\n  }), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"frac-line\",\n    \"style\": {\n      \"borderBottomWidth\": \"0.04em\"\n    }\n  })), mdx(\"span\", {\n    parentName: \"span\",\n    \"style\": {\n      \"top\": \"-3.394em\"\n    }\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"pstrut\",\n    \"style\": {\n      \"height\": \"3em\"\n    }\n  }), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"sizing reset-size6 size3 mtight\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mord mtight\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mord mtight\"\n  }, `2`))))), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"vlist-s\"\n  }, `​`)), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"vlist-r\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"vlist\",\n    \"style\": {\n      \"height\": \"0.345em\"\n    }\n  }, mdx(\"span\", {\n    parentName: \"span\"\n  }))))), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mclose nulldelimiter\"\n  })))))), ` = 20%. Our test set contains two entries. 20% of 2 is\nequal to `, mdx(\"span\", {\n    parentName: \"p\",\n    \"className\": \"math math-inline\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"katex\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"katex-mathml\"\n  }, mdx(\"math\", {\n    parentName: \"span\",\n    \"xmlns\": \"http://www.w3.org/1998/Math/MathML\"\n  }, mdx(\"semantics\", {\n    parentName: \"math\"\n  }, mdx(\"mrow\", {\n    parentName: \"semantics\"\n  }, mdx(\"mn\", {\n    parentName: \"mrow\"\n  }, `2`), mdx(\"mo\", {\n    parentName: \"mrow\"\n  }, `⋅`), mdx(\"mn\", {\n    parentName: \"mrow\"\n  }, `0.2`), mdx(\"mo\", {\n    parentName: \"mrow\"\n  }, `=`), mdx(\"mn\", {\n    parentName: \"mrow\"\n  }, `0.4`), mdx(\"mo\", {\n    parentName: \"mrow\"\n  }, `=`), mdx(\"mfrac\", {\n    parentName: \"mrow\"\n  }, mdx(\"mn\", {\n    parentName: \"mfrac\"\n  }, `2`), mdx(\"mn\", {\n    parentName: \"mfrac\"\n  }, `5`))), mdx(\"annotation\", {\n    parentName: \"semantics\",\n    \"encoding\": \"application/x-tex\"\n  }, `2 \\\\cdot 0.2 = 0.4 = \\\\frac{2}{5}`)))), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"katex-html\",\n    \"aria-hidden\": \"true\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"base\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"strut\",\n    \"style\": {\n      \"height\": \"0.64444em\",\n      \"verticalAlign\": \"0em\"\n    }\n  }), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mord\"\n  }, `2`), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mspace\",\n    \"style\": {\n      \"marginRight\": \"0.2222222222222222em\"\n    }\n  }), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mbin\"\n  }, `⋅`), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mspace\",\n    \"style\": {\n      \"marginRight\": \"0.2222222222222222em\"\n    }\n  })), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"base\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"strut\",\n    \"style\": {\n      \"height\": \"0.64444em\",\n      \"verticalAlign\": \"0em\"\n    }\n  }), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mord\"\n  }, `0`), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mord\"\n  }, `.`), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mord\"\n  }, `2`), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mspace\",\n    \"style\": {\n      \"marginRight\": \"0.2777777777777778em\"\n    }\n  }), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mrel\"\n  }, `=`), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mspace\",\n    \"style\": {\n      \"marginRight\": \"0.2777777777777778em\"\n    }\n  })), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"base\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"strut\",\n    \"style\": {\n      \"height\": \"0.64444em\",\n      \"verticalAlign\": \"0em\"\n    }\n  }), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mord\"\n  }, `0`), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mord\"\n  }, `.`), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mord\"\n  }, `4`), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mspace\",\n    \"style\": {\n      \"marginRight\": \"0.2777777777777778em\"\n    }\n  }), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mrel\"\n  }, `=`), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mspace\",\n    \"style\": {\n      \"marginRight\": \"0.2777777777777778em\"\n    }\n  })), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"base\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"strut\",\n    \"style\": {\n      \"height\": \"1.190108em\",\n      \"verticalAlign\": \"-0.345em\"\n    }\n  }), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mord\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mopen nulldelimiter\"\n  }), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mfrac\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"vlist-t vlist-t2\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"vlist-r\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"vlist\",\n    \"style\": {\n      \"height\": \"0.845108em\"\n    }\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"style\": {\n      \"top\": \"-2.6550000000000002em\"\n    }\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"pstrut\",\n    \"style\": {\n      \"height\": \"3em\"\n    }\n  }), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"sizing reset-size6 size3 mtight\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mord mtight\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mord mtight\"\n  }, `5`)))), mdx(\"span\", {\n    parentName: \"span\",\n    \"style\": {\n      \"top\": \"-3.23em\"\n    }\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"pstrut\",\n    \"style\": {\n      \"height\": \"3em\"\n    }\n  }), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"frac-line\",\n    \"style\": {\n      \"borderBottomWidth\": \"0.04em\"\n    }\n  })), mdx(\"span\", {\n    parentName: \"span\",\n    \"style\": {\n      \"top\": \"-3.394em\"\n    }\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"pstrut\",\n    \"style\": {\n      \"height\": \"3em\"\n    }\n  }), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"sizing reset-size6 size3 mtight\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mord mtight\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mord mtight\"\n  }, `2`))))), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"vlist-s\"\n  }, `​`)), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"vlist-r\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"vlist\",\n    \"style\": {\n      \"height\": \"0.345em\"\n    }\n  }, mdx(\"span\", {\n    parentName: \"span\"\n  }))))), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mclose nulldelimiter\"\n  })))))), `. Because we can’t have `, mdx(\"span\", {\n    parentName: \"p\",\n    \"className\": \"math math-inline\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"katex\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"katex-mathml\"\n  }, mdx(\"math\", {\n    parentName: \"span\",\n    \"xmlns\": \"http://www.w3.org/1998/Math/MathML\"\n  }, mdx(\"semantics\", {\n    parentName: \"math\"\n  }, mdx(\"mrow\", {\n    parentName: \"semantics\"\n  }, mdx(\"mfrac\", {\n    parentName: \"mrow\"\n  }, mdx(\"mn\", {\n    parentName: \"mfrac\"\n  }, `2`), mdx(\"mn\", {\n    parentName: \"mfrac\"\n  }, `5`))), mdx(\"annotation\", {\n    parentName: \"semantics\",\n    \"encoding\": \"application/x-tex\"\n  }, `\\\\frac{2}{5}`)))), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"katex-html\",\n    \"aria-hidden\": \"true\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"base\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"strut\",\n    \"style\": {\n      \"height\": \"1.190108em\",\n      \"verticalAlign\": \"-0.345em\"\n    }\n  }), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mord\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mopen nulldelimiter\"\n  }), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mfrac\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"vlist-t vlist-t2\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"vlist-r\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"vlist\",\n    \"style\": {\n      \"height\": \"0.845108em\"\n    }\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"style\": {\n      \"top\": \"-2.6550000000000002em\"\n    }\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"pstrut\",\n    \"style\": {\n      \"height\": \"3em\"\n    }\n  }), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"sizing reset-size6 size3 mtight\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mord mtight\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mord mtight\"\n  }, `5`)))), mdx(\"span\", {\n    parentName: \"span\",\n    \"style\": {\n      \"top\": \"-3.23em\"\n    }\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"pstrut\",\n    \"style\": {\n      \"height\": \"3em\"\n    }\n  }), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"frac-line\",\n    \"style\": {\n      \"borderBottomWidth\": \"0.04em\"\n    }\n  })), mdx(\"span\", {\n    parentName: \"span\",\n    \"style\": {\n      \"top\": \"-3.394em\"\n    }\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"pstrut\",\n    \"style\": {\n      \"height\": \"3em\"\n    }\n  }), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"sizing reset-size6 size3 mtight\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mord mtight\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mord mtight\"\n  }, `2`))))), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"vlist-s\"\n  }, `​`)), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"vlist-r\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"vlist\",\n    \"style\": {\n      \"height\": \"0.345em\"\n    }\n  }, mdx(\"span\", {\n    parentName: \"span\"\n  }))))), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mclose nulldelimiter\"\n  })))))), ` of\na sample with label 1 in our test set, our splitting function just decided to not take\nin any sample with that label at all. `), mdx(\"p\", null, `So in this example, stratification did not improve the quality of our split that much.\nSo what can you do when you are in a situation like this?\nYou have two options.\nRecall that, in stratified sampling, the number of samples with a particular label in a subset can be calculated as the product of\nthe label frequency and the subset size. Right now our number of samples is `, mdx(\"span\", {\n    parentName: \"p\",\n    \"className\": \"math math-inline\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"katex\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"katex-mathml\"\n  }, mdx(\"math\", {\n    parentName: \"span\",\n    \"xmlns\": \"http://www.w3.org/1998/Math/MathML\"\n  }, mdx(\"semantics\", {\n    parentName: \"math\"\n  }, mdx(\"mrow\", {\n    parentName: \"semantics\"\n  }, mdx(\"mfrac\", {\n    parentName: \"mrow\"\n  }, mdx(\"mn\", {\n    parentName: \"mfrac\"\n  }, `2`), mdx(\"mn\", {\n    parentName: \"mfrac\"\n  }, `5`))), mdx(\"annotation\", {\n    parentName: \"semantics\",\n    \"encoding\": \"application/x-tex\"\n  }, `\\\\frac{2}{5}`)))), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"katex-html\",\n    \"aria-hidden\": \"true\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"base\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"strut\",\n    \"style\": {\n      \"height\": \"1.190108em\",\n      \"verticalAlign\": \"-0.345em\"\n    }\n  }), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mord\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mopen nulldelimiter\"\n  }), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mfrac\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"vlist-t vlist-t2\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"vlist-r\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"vlist\",\n    \"style\": {\n      \"height\": \"0.845108em\"\n    }\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"style\": {\n      \"top\": \"-2.6550000000000002em\"\n    }\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"pstrut\",\n    \"style\": {\n      \"height\": \"3em\"\n    }\n  }), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"sizing reset-size6 size3 mtight\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mord mtight\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mord mtight\"\n  }, `5`)))), mdx(\"span\", {\n    parentName: \"span\",\n    \"style\": {\n      \"top\": \"-3.23em\"\n    }\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"pstrut\",\n    \"style\": {\n      \"height\": \"3em\"\n    }\n  }), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"frac-line\",\n    \"style\": {\n      \"borderBottomWidth\": \"0.04em\"\n    }\n  })), mdx(\"span\", {\n    parentName: \"span\",\n    \"style\": {\n      \"top\": \"-3.394em\"\n    }\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"pstrut\",\n    \"style\": {\n      \"height\": \"3em\"\n    }\n  }), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"sizing reset-size6 size3 mtight\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mord mtight\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mord mtight\"\n  }, `2`))))), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"vlist-s\"\n  }, `​`)), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"vlist-r\"\n  }, mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"vlist\",\n    \"style\": {\n      \"height\": \"0.345em\"\n    }\n  }, mdx(\"span\", {\n    parentName: \"span\"\n  }))))), mdx(\"span\", {\n    parentName: \"span\",\n    \"className\": \"mclose nulldelimiter\"\n  })))))), `, which is too low.\nWe can increase that number by either 1. increasing the label frequency or 2. increasing the subset size.\nThe first option is a lot more difficult than the second one because it likely involves getting more samples\nwith that particular label, which is not always possible/affordable.\nLet’s see how both variants perform in practice.`), mdx(\"p\", null, `1)\nIf we manage to get one more label of 1 into the dataset, like this:`), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-python\"\n  }, `X = np.arange(11) # now we have eleven values in our dataset \ny = [1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1]\n`)), mdx(\"p\", null, `and again perform our 80-20-split, we will get something like this:`), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-python\"\n  }, `>>> X_train, X_test, y_train, y_test = train_test_split(X,y, test_size=0.2, random_state=42, shuffle=True, stratify=y)\n>>> y_train\n[1, 0, 0, 0, 0, 0, 0, 1]\n>>> y_test\n[0, 0, 1]\n`)), mdx(\"p\", null, `As we see, our y_test now contains a sample with label 1.`), mdx(\"p\", null, `2)`), mdx(\"p\", null, `We can also increase the size of our test set from 0.2 to f.e. 0.3:`), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-python\"\n  }, `>>> X_train, X_test, y_train, y_test = train_test_split(X,y, test_size=0.3, random_state=42, shuffle=True, stratify=y)\n>>> y_train\n[0, 1, 0, 0, 0, 0, 0]\n>>> y_test\n[0, 0, 1]\n`)), mdx(\"p\", null, `which also adds a sample with label 1 to our y_test.`), mdx(\"p\", null, `This problem should only be troublesome in cases where your data is particularly sparse (like the one shown here).\nBut if you ever come across an issue like this, you’ll now know what to do in order to combat it.`), mdx(\"p\", null, `Credit where credit is due, I have found this particular example on `, mdx(\"a\", {\n    parentName: \"p\",\n    \"href\": \"https://stackoverflow.com/questions/39856729/stratify-parameter-from-sklearns-train-test-split-not-working-correctly\",\n    \"target\": \"_blank\",\n    \"rel\": \"nofollow noopener noreferrer\"\n  }, `stackoverflow`), `\nand found it so useful that I decided to include it in this article and extend it with\na couple of code snippets and additional examples.`)), mdx(\"p\", null, `If you want to play around with this classification dataset on your own,\nyou can copy the code from this article and run it on your local machine.\nBecause we provided an argument for `, mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, `random_state`), `, you should get the same results when you run the code\non your own machine!\nBelow you can find a drop-down containing the dataset as a code snippet.`), mdx(Collapsible, {\n    title: \"Task Difficulty Classification Dataset\",\n    mdxType: \"Collapsible\"\n  }, mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-python\"\n  }, `X = np.array([[400.,   6.],\n       [424.,   4.],\n       [136.,   4.],\n       [ 99.,   6.],\n       [478.,   4.],\n       [ 90.,   4.],\n       [411.,   9.],\n       [256.,   3.],\n       [189.,   9.],\n       [280.,   2.],\n       [596.,   4.],\n       [250.,   4.],\n       [136.,   6.],\n       [174.,   9.],\n       [548.,   6.],\n       [428.,   6.],\n       [795.,   6.],\n       [295.,   3.],\n       [282.,   9.],\n       [206.,   2.],\n       [342.,   6.],\n       [374.,   6.],\n       [350.,   6.],\n       [546.,   9.],\n       [444.,   4.],\n       [204.,   4.],\n       [507.,   6.],\n       [ 90.,   2.],\n       [681.,   6.],\n       [ 40.,   3.],\n       [510.,   4.],\n       [456.,   6.],\n       [578.,   4.],\n       [303.,   9.],\n       [184.,   6.],\n       [544.,   4.],\n       [408.,   9.],\n       [247.,   2.],\n       [810.,   9.],\n       [288.,   3.],\n       [582.,   4.],\n       [580.,   6.],\n       [152.,   4.],\n       [618.,   6.],\n       [256.,   6.],\n       [540.,   4.],\n       [621.,   9.],\n       [ 91.,   2.],\n       [735.,   6.],\n       [189.,   2.]])\n\ny = np.array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 1., 1., 1., 1., 1., 1.,\n       1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,\n       1., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2.])\n`))), mdx(\"h2\", {\n    \"id\": \"further-reading\"\n  }, `Further Reading`), mdx(\"p\", null, `If you have read a few tutorials on machine learning you might have come across a so-called `, mdx(\"em\", {\n    parentName: \"p\"\n  }, `validation set`), `, or `, mdx(\"em\", {\n    parentName: \"p\"\n  }, `val set`), ` for short (sometimes it’s also called a development set, or dev set).\nIf you have trained lots of machine learning models in the past, using a validation set might seem like second nature to you.\nBut maybe, like I did when I first got into machine learning, you are scratching your head and asking yourself why a validation set is needed if we already have a test set.\n`, mdx(\"em\", {\n    parentName: \"p\"\n  }, `Which problem does the validation set solve?`), ` This question is so common and understanding why we need a val set is so important, that I want to dedicate an entire post to explaining what a validation\nset is, what problems it solves, and how you can start using validation sets in your next machine learning projects.\nOften times, truly understanding why validation is necessary can be something that differentiates someone who has a good understanding of how machine learning models are trained from\nsomeone who just knows how to write the code needed to train them.\nIf you’re interested in reading more about this important aspect of training machine learning models, then I highly recommend that you read the article `, mdx(\"a\", {\n    parentName: \"p\",\n    \"href\": \"/dataset_optimization/why_do_we_need_a_val_set\"\n  }, `Why do We Need a Validation Set?`), `. `), mdx(\"p\", null, `Now, the validation set is one thing. But maybe you’ve also heard about `, mdx(\"em\", {\n    parentName: \"p\"\n  }, `cross validation`), `.\nMaybe you’ve heard that it is a good practice in machine learning and that it also has `, mdx(\"em\", {\n    parentName: \"p\"\n  }, `something`), ` to do with a validation set.\nBut maybe you are unsure as to how it really works or how you can integrate it in your machine learning projects.\nIf this is you, then fear no more! After reading the article `, mdx(\"a\", {\n    parentName: \"p\",\n    \"href\": \"/model_optimization/cross_validation\"\n  }, `Cross Validation Explained, Step by Step`), `\nyou will understand exactly what cross validation is and how you can use it in your next machine learning project by\nadding just a few lines of code.`));\n}\n;\nMDXContent.isMDXComponent = true;","excerpt":"If you are just starting out in machine learning and building your first real models, you will have to split your dataset into a train set as well as a test set. But what benefits does this splitting yield? How can you split your dataset optimally? In this article, we will go through these questions and explore why splitting your dataset makes sense and how you can split your dataset properly.","tableOfContents":{"items":[{"url":"#outline","title":"Outline"},{"url":"#prerequisites","title":"Prerequisites"},{"url":"#training-and-testing","title":"Training and Testing","items":[{"url":"#a-practical-example","title":"A Practical Example"},{"url":"#an-analogy","title":"An Analogy"}]},{"url":"#common-questions","title":"Common Questions","items":[{"url":"#how-large-should-the-testing-set-be","title":"How Large Should the Testing Set be?"},{"url":"#which-part-of-the-dataset-should-be-used-for-testing","title":"Which Part of the Dataset Should be Used For Testing?"},{"url":"#what-is-stratification","title":"What is Stratification?"},{"url":"#how-can-i-efficiently-split-my-dataset-with-code","title":"How can I Efficiently Split my Dataset with Code?","items":[{"url":"#practical-example-regression","title":"Practical Example: Regression"},{"url":"#practical-example-classification","title":"Practical Example: Classification"},{"url":"#a-mistake","title":"A Mistake?"}]}]},{"url":"#further-reading","title":"Further Reading"}]},"frontmatter":{"title":"How to Split Your Dataset the Right Way","userDate":"1 May 2021","date":"2021-05-01T18:09:26.968Z","dateModified":"2021-05-01T18:09:26.968Z","changelog":[],"tags":["Dataset Optimization"],"timeToRead":15,"excerpt":"If you are just starting out in machine learning and building your first real models, you will have to split your dataset into a train set as well as a test set. But what benefits does this splitting yield? How can you split your dataset optimally? In this article, we will go through these questions and explore why splitting your dataset makes sense and how you can split your dataset properly.","image":{"childImageSharp":{"gatsbyImageData":{"layout":"fullWidth","placeholder":{"fallback":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAALCAIAAADwazoUAAAACXBIWXMAAAsTAAALEwEAmpwYAAACcUlEQVQozxXPaW/SYAAA4DfA1pVCOVooPaDH27ctZZwFBgwm24QKHedidAM3sssNk0EkXlGTJWYxUWf85Ff/mb/F+PyDB7gk3S0bHsUEAsSNfDRj6NUCY7VyzfLZ+cFqXqnbla29DJlpl/fSWtkEMRlDOirKSkEFLslwK0kgqFZ3qO2P0hX286pvT7/dv+v++rTz9w+4e3v0/aNTefpbq5a2HLva73tEKZZGAR0BTFABIxadUef0AvAQgzqupoCUI1QNhxqV3mRzWsBQ1yEKGEjIiJObWaU/AELCizSwEeSImHJ8s/DpGcBpW/X45dDfb4WWh66BHSHNPpMdRzOHlGEz2Qadko1q8mSxEHK6nIfA42cSWrZ3OAEkQyYLQZTYrxPTsWQ/lpVUBBZrXGZfstpa8dGGlHcJEuDE8cVZubUFOAWs+WOsnCo32jSn/Hj4+eLy2pmeP79e3n19uFis5svb1YfXw+nx3f2Xm+XtydVVoWHsjAdiycJkCPCwgIeF2m6XjCLJKChJy8hWAnLKbDyazeetXhPVdphcsdHrJJsdy3bYfO7Js4OAoRL6/zPv9nMJVTvqpXud7Z5tOe3SZFzjN3cBzXsRXBelkAGlLDceQGfUqDs9rrjthTKQdJCQxE2dD9HUpG/OjjtvXo3mZwdX02bS6nrDfESSYQ7qJYgp8OUs/341PZ0NKTMJEqpb0kDNUnYrJsWKAGc9BBOJm0REARtRjGQISiBp3h/hfXEJk5EnoQdMizR0P1LdIiIQAsEIG4ryWFDwUQJBxbEgi4d5Hx33hgQ8xHspgWZ4X5in+bhXUYGgrEEdiHpAV8vbiX/nc3rHlqIDkgAAAABJRU5ErkJggg=="},"images":{"fallback":{"src":"/static/e9b8f309bba40fd754df6982c7c7159d/87706/SplitDataTheRightWay_Thumbnail.png","srcSet":"/static/e9b8f309bba40fd754df6982c7c7159d/0dee1/SplitDataTheRightWay_Thumbnail.png 750w,\n/static/e9b8f309bba40fd754df6982c7c7159d/8beaa/SplitDataTheRightWay_Thumbnail.png 1080w,\n/static/e9b8f309bba40fd754df6982c7c7159d/87706/SplitDataTheRightWay_Thumbnail.png 1280w","sizes":"100vw"},"sources":[{"srcSet":"/static/e9b8f309bba40fd754df6982c7c7159d/a66aa/SplitDataTheRightWay_Thumbnail.webp 750w,\n/static/e9b8f309bba40fd754df6982c7c7159d/65dd5/SplitDataTheRightWay_Thumbnail.webp 1080w,\n/static/e9b8f309bba40fd754df6982c7c7159d/71d4d/SplitDataTheRightWay_Thumbnail.webp 1280w","type":"image/webp","sizes":"100vw"}]},"width":1,"height":0.5625}}},"author":[{"id":"Boris Giba","bio":"Hi! I'm Boris and I run this website. You can find more information in the \"About\"-tab.","avatar":{"children":[{"fluid":{"base64":"data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAUABQDASIAAhEBAxEB/8QAFwABAQEBAAAAAAAAAAAAAAAAAAQFBv/EABYBAQEBAAAAAAAAAAAAAAAAAAABAv/aAAwDAQACEAMQAAAB25bcya1Bcz84HRg//8QAGxAAAwACAwAAAAAAAAAAAAAAAQIDABESMTP/2gAIAQEAAQUCo3FFo4OEbG1R8u5SXZh4f//EABURAQEAAAAAAAAAAAAAAAAAAAEg/9oACAEDAQE/ASP/xAAUEQEAAAAAAAAAAAAAAAAAAAAg/9oACAECAQE/AR//xAAeEAABBAEFAAAAAAAAAAAAAAABAAIQETESIUFRcf/aAAgBAQAGPwIkZTQ/Sb6ilvxBIzDPF//EABoQAQACAwEAAAAAAAAAAAAAAAERIQAQMUH/2gAIAQEAAT8hQ1PjHRkxa9E644V8JdD3BEk1JiRlbnUf/9oADAMBAAIAAwAAABADAID/xAAWEQEBAQAAAAAAAAAAAAAAAAABESD/2gAIAQMBAT8QAjcf/8QAFhEBAQEAAAAAAAAAAAAAAAAAARAR/9oACAECAQE/EFdIT//EAB4QAQACAQQDAAAAAAAAAAAAAAERIQAQMUFRYYGx/9oACAEBAAE/EClkAFglYJ8Xl1kNwXZjrQ1xCEcPBl8E6A89+sAII7JzjXgEBSYlyYrZm+d/uVsWluf/2Q==","aspectRatio":1,"src":"/static/ba9e631272af1b925d6491b30df3b36f/2f1b1/boris.jpg","srcSet":"/static/ba9e631272af1b925d6491b30df3b36f/b3d27/boris.jpg 40w,\n/static/ba9e631272af1b925d6491b30df3b36f/5a735/boris.jpg 80w,\n/static/ba9e631272af1b925d6491b30df3b36f/6e81a/boris.jpg 120w,\n/static/ba9e631272af1b925d6491b30df3b36f/2f1b1/boris.jpg 800w,\n/static/ba9e631272af1b925d6491b30df3b36f/97bce/boris.jpg 1878w","sizes":"(max-width: 800px) 100vw, 800px"}}]}}],"imgCaption":"Background image by Mario Gogh","imgSource":"https://unsplash.com/photos/lTXhxtaqF3c","uniquePostId":8,"tutorialId":null,"tutorialEntryIdx":null,"dependencies":[],"proficiencyLevel":"Beginner"}},"relatedPosts":{"totalCount":2,"edges":[{"node":{"id":"d9274609-29df-5679-92c0-1f0d2c752677","excerpt":"Standardization is one of the most useful transformations you can apply to your dataset. What is even more important is that many models, especially regularized ones, require the data to be standardized in order to function properly. In this article, you will learn everything you need to know about standardization. You will learn why it works, when you should use it, and how you can do so with just a few lines of code.","frontmatter":{"title":"When, Why, And How You Should Standardize Your Data","timeToRead":10,"date":"2021-07-06T11:47:10.172Z"},"fields":{"slug":"/dataset_optimization/standardization/"}}},{"node":{"id":"025f12b7-e505-5eff-8617-17e9e76707b8","excerpt":"If you are just starting out in machine learning and building your first real models, you will have to split your dataset into a train set as well as a test set. But what benefits does this splitting yield? How can you split your dataset optimally? In this article, we will go through these questions and explore why splitting your dataset makes sense and how you can split your dataset properly.","frontmatter":{"title":"How to Split Your Dataset the Right Way","timeToRead":15,"date":"2021-05-01T18:09:26.968Z"},"fields":{"slug":"/dataset_optimization/split_data_the_right_way/"}}}]}},"pageContext":{"slug":"/dataset_optimization/split_data_the_right_way/","prev":{"excerpt":"You have likely heard about bias and variance before. They are two fundamental terms in machine learning and often used to explain overfitting and underfitting. If you're working with machine learning methods, it's crucial to understand these concepts well so that you can make optimal decisions in your own projects. In this article, you'll learn everything you need to know about bias, variance, overfitting, and the bias-variance tradeoff.","frontmatter":{"title":"Bias, Variance, and Overfitting Explained, Step by Step","tags":["Model Optimization"],"date":"2021-04-17T19:54:40.006Z","dateModified":"2021-04-17T19:54:40.006Z","changelog":[["2022-03-19T19:39:23.993Z","Fixed typo, thanks to Ali Jan!"]],"draft":false,"excerpt":"You have likely heard about bias and variance before. They are two fundamental terms in machine learning and often used to explain overfitting and underfitting. If you're working with machine learning methods, it's crucial to understand these concepts well so that you can make optimal decisions in your own projects. In this article, you'll learn everything you need to know about bias, variance, overfitting, and the bias-variance tradeoff.","uniquePostId":7,"timeToRead":20,"tutorialId":null,"tutorialEntryIdx":null,"dependencies":[],"proficiencyLevel":"Beginner","image":{"childImageSharp":{"gatsbyImageData":{"layout":"fullWidth","placeholder":{"fallback":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAALCAIAAADwazoUAAAACXBIWXMAAAsTAAALEwEAmpwYAAACdElEQVQozxXOWVLaAAAA0HxVHGXJngAhLGELCYSyJYSQoAhFBK1FtCiLSFiDMyjYTqmg/rXTS9iTdvpO8AAuxHAhpl6tfK6fyJlknAtzQSbOs8l4LOSlaTse5zk+HGTcLi9NIaCFpuyimM4rOU3NA1HWHwn6ttvN8/PzcDicz41ep1MtHz0sFt+eVveGMZtO5/PZavk4m0wvLhq6Puj1ulImXdBUwOt2R0LMeKQP9cFkPDJmU31wd3py3O+29X5vNNQb52f3hmFMp6e16lm91r657nTakpguaHkAhBwESaeSyePSYU7KpOKx0oFaPixoOemqcX5cKhY1pajlU0LU56YwyMownmxWzCvy/zYEO6wgaQXtDBOWJelAzX0qFqrloiZLk+FgrN9l0wk1lxX4iN/nwSArFwnl84qiyKqaB6w2knYHr6+7HJ+w2Aguwmu5rCpLlVJx0L+VMunaSbVULMqSyAb9BAwG/D5NU5WcLIoZYMcENputxcNys3np3+mLh+V6/WO1Ws0N48/vXzet1uvry8/1ejqZLBaLt7e31Wq53W50feD3M8DePhIV0qun71qh1O70a/Xzx+XTl8ZlhItXK5VK+ei2171pXXW7HWM2vWxeTMaj9/e/xnyOIDDgdQeCbp/ZjJp2oQ8m244J2tkF98woiFD7FowgHJmEkBFYIexjGYoPuj7yoa/NhpwVKRID0lEhl5JI0mMFCQh2ICiFkx6McOOkB0EpixVHUCeJYk7URhNwKsYeKJIQCWSSQoz1AwjiwHEXCDtgxAkjFIK5UJyGYAeKu6w2wmzBEJSCYCcM4S4c9tgR1k8n43wqIUQjgX/XKpxML/eDfwAAAABJRU5ErkJggg=="},"images":{"fallback":{"src":"/static/7e8675c2404d1fdaa12b78c05104d577/87706/BiasAndVariance_Thumbnail.png","srcSet":"/static/7e8675c2404d1fdaa12b78c05104d577/0dee1/BiasAndVariance_Thumbnail.png 750w,\n/static/7e8675c2404d1fdaa12b78c05104d577/8beaa/BiasAndVariance_Thumbnail.png 1080w,\n/static/7e8675c2404d1fdaa12b78c05104d577/87706/BiasAndVariance_Thumbnail.png 1280w","sizes":"100vw"},"sources":[{"srcSet":"/static/7e8675c2404d1fdaa12b78c05104d577/a66aa/BiasAndVariance_Thumbnail.webp 750w,\n/static/7e8675c2404d1fdaa12b78c05104d577/65dd5/BiasAndVariance_Thumbnail.webp 1080w,\n/static/7e8675c2404d1fdaa12b78c05104d577/71d4d/BiasAndVariance_Thumbnail.webp 1280w","type":"image/webp","sizes":"100vw"}]},"width":1,"height":0.5625}}},"author":[{"id":"Boris Giba","bio":"Hi! I'm Boris and I run this website. You can find more information in the \"About\"-tab.","avatar":{"children":[{"fluid":{"aspectRatio":1,"base64":"data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAUABQDASIAAhEBAxEB/8QAFwABAQEBAAAAAAAAAAAAAAAAAAQFBv/EABYBAQEBAAAAAAAAAAAAAAAAAAABAv/aAAwDAQACEAMQAAAB25bcya1Bcz84HRg//8QAGxAAAwACAwAAAAAAAAAAAAAAAQIDABESMTP/2gAIAQEAAQUCo3FFo4OEbG1R8u5SXZh4f//EABURAQEAAAAAAAAAAAAAAAAAAAEg/9oACAEDAQE/ASP/xAAUEQEAAAAAAAAAAAAAAAAAAAAg/9oACAECAQE/AR//xAAeEAABBAEFAAAAAAAAAAAAAAABAAIQETESIUFRcf/aAAgBAQAGPwIkZTQ/Sb6ilvxBIzDPF//EABoQAQACAwEAAAAAAAAAAAAAAAERIQAQMUH/2gAIAQEAAT8hQ1PjHRkxa9E644V8JdD3BEk1JiRlbnUf/9oADAMBAAIAAwAAABADAID/xAAWEQEBAQAAAAAAAAAAAAAAAAABESD/2gAIAQMBAT8QAjcf/8QAFhEBAQEAAAAAAAAAAAAAAAAAARAR/9oACAECAQE/EFdIT//EAB4QAQACAQQDAAAAAAAAAAAAAAERIQAQMUFRYYGx/9oACAEBAAE/EClkAFglYJ8Xl1kNwXZjrQ1xCEcPBl8E6A89+sAII7JzjXgEBSYlyYrZm+d/uVsWluf/2Q==","sizes":"(max-width: 800px) 100vw, 800px","src":"/static/ba9e631272af1b925d6491b30df3b36f/2f1b1/boris.jpg","srcSet":"/static/ba9e631272af1b925d6491b30df3b36f/fd013/boris.jpg 200w,\n/static/ba9e631272af1b925d6491b30df3b36f/25252/boris.jpg 400w,\n/static/ba9e631272af1b925d6491b30df3b36f/2f1b1/boris.jpg 800w,\n/static/ba9e631272af1b925d6491b30df3b36f/0ff54/boris.jpg 1200w,\n/static/ba9e631272af1b925d6491b30df3b36f/06655/boris.jpg 1600w,\n/static/ba9e631272af1b925d6491b30df3b36f/97bce/boris.jpg 1878w"}}]}}]},"fields":{"layout":"post","slug":"/model_optimization/bias_and_variance/"}},"next":{"excerpt":"Ridge Regression is an adaptation of the popular and widely used linear regression algorithm. It enhances regular linear regression by slightly changing its cost function, which results in less overfit models. In this article, you will learn everything you need to know about Ridge Regression, and how you can start using it in your own machine learning projects.","frontmatter":{"title":"Ridge Regression Explained, Step by Step","tags":["Machine Learning Models"],"date":"2021-05-23T19:13:45.907Z","dateModified":"2021-11-02T08:08:00.157Z","changelog":[["2021-11-02T08:08:00.157Z","Added 1/n to the definition of the MSE which I previously forgot in one section. Thanks to @ML_Schlagy for pointing this out!"]],"draft":false,"excerpt":"Ridge Regression is an adaptation of the popular and widely used linear regression algorithm. It enhances regular linear regression by slightly changing its cost function, which results in less overfit models. In this article, you will learn everything you need to know about Ridge Regression, and how you can start using it in your own machine learning projects.","uniquePostId":9,"timeToRead":26,"tutorialId":null,"tutorialEntryIdx":null,"dependencies":[],"proficiencyLevel":"Beginner","image":{"childImageSharp":{"gatsbyImageData":{"layout":"fullWidth","placeholder":{"fallback":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAALCAIAAADwazoUAAAACXBIWXMAAAsTAAALEwEAmpwYAAACVUlEQVQozz3OWU8aUQCG4QMeBmEYhmFmmOWcWZgVDqACKkOCCzZao8SaaohCG5qmS2wv3JDlptg7TZo2aYzav+CvbDBNk+fuTb58gEopkZT6H0ypEU6lOJVKKRSnTCsr/5PMQEaEjAATAkzwkOZAVDApwYw8g1NGmNNCLAIsAowcSkghZiqclCGnQlZ+Jj2v8CAuWrMoH8UkigiFCRCsfK3Z+3hSXXlRCtYbWy3VLyN/QbAKIU6DvA7TGkxjOP0rA4bDjGgljXnaWqLdAGjlne6nu4fHi8H41++7u/v72x8//zw+tHsfQBLHNEIpHpRsKJpxxQJ0Un6m6M6cahcjvMFiz1sI3n8+ebm1ubHeHAxHJGialdUZXAzLPpScWcWlkSuYeRCNC1FaiMb5fn9w1b/qdrqt1t75ZX88HnWOj990upubW68O2tu7e+3O24Oj7v5Bm4oxaVHJOj6QkSMjR8Xul6+nw9H42/X3m5vbp6ens7PT4WAwHI0uLy+uJ5OL8/PJZDIcjXq9dzutvf3Xh43VJpAUS1KyCnL4jIF0L19YrC43avVVrDmW5WCMi8VSsTRHSCEI6mvrG4fto8Va3fGJpGLASC5tVChaTPHIcudJaTnrFCXF1owc1n0j69m2l8uTSqVaLleCel1BGgCh0Aw1Q8VAdnnHqe3auXJ1aaWxtj1XDkybYMMXJENSbd3MmxZxPOJ6Oc/PIayzXDrBpqgYE5lNAE62OcnK2iRXWPRJFelekpOFjDFfrvOioSLHsIiCXd30sZ7NKIjjRU4QYwk2TMX/AgxCcEm95chYAAAAAElFTkSuQmCC"},"images":{"fallback":{"src":"/static/bffad91bca399e12f65048348b2b2df8/87706/RidgeRegression_Thumbnail.png","srcSet":"/static/bffad91bca399e12f65048348b2b2df8/0dee1/RidgeRegression_Thumbnail.png 750w,\n/static/bffad91bca399e12f65048348b2b2df8/8beaa/RidgeRegression_Thumbnail.png 1080w,\n/static/bffad91bca399e12f65048348b2b2df8/87706/RidgeRegression_Thumbnail.png 1280w","sizes":"100vw"},"sources":[{"srcSet":"/static/bffad91bca399e12f65048348b2b2df8/a66aa/RidgeRegression_Thumbnail.webp 750w,\n/static/bffad91bca399e12f65048348b2b2df8/65dd5/RidgeRegression_Thumbnail.webp 1080w,\n/static/bffad91bca399e12f65048348b2b2df8/71d4d/RidgeRegression_Thumbnail.webp 1280w","type":"image/webp","sizes":"100vw"}]},"width":1,"height":0.5625}}},"author":[{"id":"Boris Giba","bio":"Hi! I'm Boris and I run this website. You can find more information in the \"About\"-tab.","avatar":{"children":[{"fluid":{"aspectRatio":1,"base64":"data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAUABQDASIAAhEBAxEB/8QAFwABAQEBAAAAAAAAAAAAAAAAAAQFBv/EABYBAQEBAAAAAAAAAAAAAAAAAAABAv/aAAwDAQACEAMQAAAB25bcya1Bcz84HRg//8QAGxAAAwACAwAAAAAAAAAAAAAAAQIDABESMTP/2gAIAQEAAQUCo3FFo4OEbG1R8u5SXZh4f//EABURAQEAAAAAAAAAAAAAAAAAAAEg/9oACAEDAQE/ASP/xAAUEQEAAAAAAAAAAAAAAAAAAAAg/9oACAECAQE/AR//xAAeEAABBAEFAAAAAAAAAAAAAAABAAIQETESIUFRcf/aAAgBAQAGPwIkZTQ/Sb6ilvxBIzDPF//EABoQAQACAwEAAAAAAAAAAAAAAAERIQAQMUH/2gAIAQEAAT8hQ1PjHRkxa9E644V8JdD3BEk1JiRlbnUf/9oADAMBAAIAAwAAABADAID/xAAWEQEBAQAAAAAAAAAAAAAAAAABESD/2gAIAQMBAT8QAjcf/8QAFhEBAQEAAAAAAAAAAAAAAAAAARAR/9oACAECAQE/EFdIT//EAB4QAQACAQQDAAAAAAAAAAAAAAERIQAQMUFRYYGx/9oACAEBAAE/EClkAFglYJ8Xl1kNwXZjrQ1xCEcPBl8E6A89+sAII7JzjXgEBSYlyYrZm+d/uVsWluf/2Q==","sizes":"(max-width: 800px) 100vw, 800px","src":"/static/ba9e631272af1b925d6491b30df3b36f/2f1b1/boris.jpg","srcSet":"/static/ba9e631272af1b925d6491b30df3b36f/fd013/boris.jpg 200w,\n/static/ba9e631272af1b925d6491b30df3b36f/25252/boris.jpg 400w,\n/static/ba9e631272af1b925d6491b30df3b36f/2f1b1/boris.jpg 800w,\n/static/ba9e631272af1b925d6491b30df3b36f/0ff54/boris.jpg 1200w,\n/static/ba9e631272af1b925d6491b30df3b36f/06655/boris.jpg 1600w,\n/static/ba9e631272af1b925d6491b30df3b36f/97bce/boris.jpg 1878w"}}]}}]},"fields":{"layout":"post","slug":"/machine_learning_models/ridge_regression/"}},"primaryTag":"Dataset Optimization"}},"staticQueryHashes":["13077718","1316742457","1394499604","2907728042","3762220167"]}