{"id":214588,"date":"2024-11-04T11:36:41","date_gmt":"2024-11-04T16:36:41","guid":{"rendered":"https:\/\/ibkrcampus.com\/campus\/?p=214588"},"modified":"2024-11-04T11:36:53","modified_gmt":"2024-11-04T16:36:53","slug":"train-test-split-and-cross-validation-a-python-tutorial","status":"publish","type":"post","link":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/train-test-split-and-cross-validation-a-python-tutorial\/","title":{"rendered":"Train\/Test Split and Cross Validation &#8211; A Python Tutorial"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><em>The post &#8220;Train\/Test Split and Cross Validation &#8211; A Python Tutorial&#8221; first appeared on <a href=\"https:\/\/algotrading101.com\/learn\/train-test-split\/\">AlgoTrading101<\/a> blog.<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><em>Excerpt<\/em><\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">What is a training and testing split?&nbsp;<strong>It is the splitting of a dataset into multiple parts. We train our model using one part and test its effectiveness on another.<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this article, our focus is on the proper methods for modelling a relationship between 2 assets.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We will check if bonds can be used as a leading indicator for the S&amp;P500.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-what-is-data-splitting-in-modelling\">What is data splitting in modelling?<\/h2>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"730\" height=\"393\" data-src=\"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/11\/training-validation-test-data-set-algotrading.jpg\" alt=\"\" class=\"wp-image-214590 lazyload\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/11\/training-validation-test-data-set-algotrading.jpg 730w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/11\/training-validation-test-data-set-algotrading-700x377.jpg 700w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/11\/training-validation-test-data-set-algotrading-300x162.jpg 300w\" data-sizes=\"(max-width: 730px) 100vw, 730px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 730px; aspect-ratio: 730\/393;\" \/><figcaption class=\"wp-element-caption\"><a href=\"https:\/\/datascience.stackexchange.com\/questions\/61467\/clarification-on-train-test-and-val-and-how-to-use-implement-it\" target=\"_blank\" rel=\"noreferrer noopener\">Source<\/a><\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Data splitting is the process of splitting data into 3 sets:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Data which we use to design our models (Training set)<\/li>\n\n\n\n<li>Data which we use to refine our models (Validation set)<\/li>\n\n\n\n<li>Data which we use to test our models (Testing set)<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">If we do not split our data, we might test our model with the same data that we use to train our model.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If the model is a trading strategy specifically designed for Apple stock in 2008, and we test its effectiveness on Apple stock in 2008, of course it is going to do well.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We need to test it on 2009\u2019s data. Thus, 2008 is our training set and 2009 is our testing set.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To recap what are training, validation and testing sets\u2026<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-testing-set\">What is a Training Set?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The training set is the set of data we analyse (train on) to design the rules in the model.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A training set is also known as the in-sample data or training data.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-validation-set\">What is a Validation Set?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The validation set is a set of data that we did not use when training our model that we use to assess how well these rules perform on new data.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It is also a set we use to tune parameters and input features for our model so that it gives us what we think is the best performance possible for new data.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-test-set\">What is a Test Set?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The test set is a set of data we did not use to train our model or use in the validation set to inform our choice of parameters\/input features.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We will use it as a final test once we have decided on our final model, to get the best possible estimate of how successful our model will be when used on entirely new data.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A test set is also known as the out-of-sample data or test data.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"Why-do-we-need-to-split-our-data\">Why do we need to split our data?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To prevent look-ahead bias, overfitting and underfitting.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Look-ahead bias: Building a model based on data that is not supposed to be known.<\/li>\n\n\n\n<li>Overfitting: This is the process of designing a model that adapts so closely to historical data that it becomes ineffective in the future.<\/li>\n\n\n\n<li>Underfitting: This is the process of designing a model that adapts so loosely to historical data that it becomes ineffective in the future.<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"look-ahead-bias\">Look-Ahead Bias<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Let\u2019s illustrate this with an example.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here is Amazon\u2019s stock performance from 2013 to 2020.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"730\" height=\"472\" data-src=\"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/11\/amazon-chart-algotrading.jpg\" alt=\"\" class=\"wp-image-214591 lazyload\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/11\/amazon-chart-algotrading.jpg 730w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/11\/amazon-chart-algotrading-700x453.jpg 700w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/11\/amazon-chart-algotrading-300x194.jpg 300w\" data-sizes=\"(max-width: 730px) 100vw, 730px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 730px; aspect-ratio: 730\/472;\" \/><figcaption class=\"wp-element-caption\"><strong><em>Source: Tradingview.com<\/em><\/strong><\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Wow, it is trending up rather smoothly. I\u2019ll design a trading model that invests in Amazon as it trends up.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I then test my trading model on this same dataset (2013 to 2020).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To my non-surprise, the model performs brilliantly and I make a lot of hypothetical monies. You don\u2019t say!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When the trading model is being tested from 2013, it knows what Amazon\u2019s 2014 stock behavior will be because we took into account 2014\u2019s data when designing the trading model.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The model is said to have \u201clooked ahead\u201d into the future.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>Thus, there is look-ahead bias in our model. We built a model based on data we were not supposed to know.<\/em><\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"Overfitting\">Overfitting<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">In the simplest sense, when training, a model attempts to learn how to map input features (the available data) to the target (what we want to predict).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Overfitting is the term used to describe when a model has learnt this relationship \u201ctoo well\u201d for the training data.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">By \u201ctoo well\u201d we mean rather that it has learnt the relationship too closely- that it sees more trends\/correlations\/connections than really exist.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We can think of this as a model picking up on too much of the \u201cnoise\u201d in the training data- learning to map exact and very specific characteristics of the training data to the target when in reality these were one-off occurrences\/connections and not representative of the broader patterns generally present in the data.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">As such, the model performs very well for the training data, but flounders comparatively with new data. The patterns developed from the training data do not generalise well to new unseen data.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This is almost always a consequence of making a model too complex- allowing it to have too many rules and\/or features relative to the \u201creal\u201d amount of patterns that exist in the data. Its possibly also a consequence of having too many features for the number of observations (training data) we have to train with.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example in the extreme, imagine we had 1000 pieces of training data and a model that had 1000 \u201crules\u201d. It could essentially learn to construct rules that stated:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>rule 1: map all data with features extremely close to x1,y1,z1 (which happen to be the exact features of training data 1) to the target value of training data 1.<\/li>\n\n\n\n<li>rule 2: map all data with features extremely close to x2,y2,z2 to the target value of training data 2.<\/li>\n\n\n\n<li>\u2026<\/li>\n\n\n\n<li>rule 1000: map all data with features extremely close to x1000,y1000,z1000 to the target value of training data 1000.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Such a model would perform excellently on the training data, but would probably be nearly useless on any new data that deviated even slightly from the examples that it trained on.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can read more about overfitting here:&nbsp;<a href=\"https:\/\/algotrading101.com\/learn\/what-is-overfitting-in-trading\/\" target=\"_blank\" rel=\"noreferrer noopener\">What is Overfitting in Trading?<\/a><\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"Underfitting\">Underfitting<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">By contrast, underfitting is when a model is too non-specific. I.e., it hasn\u2019t really learnt any meaningful relationships between the training data and the target variable.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Such a model would perform well neither on the training data nor any new data.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This is a rather rarer occurrence in practice than overfitting, and usually occurs because a model is too simple- for example imagine fitting a linear regression model to non-linear data, or perhaps a random forest model with a max depth of 2 to data with many features present.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In general you want to develop a model that captures as many patterns in the training data that exist as possible that still generalise well (are applicable) to new unseen data.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In other words, we want a model that is neither overfitted or underfitted, but just&nbsp;<em>right.<\/em><\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" width=\"378\" height=\"95\" data-src=\"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/11\/underfitting_and_overfitting_graph2-1-algotrading.jpg\" alt=\"\" class=\"wp-image-214593 lazyload\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/11\/underfitting_and_overfitting_graph2-1-algotrading.jpg 378w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/11\/underfitting_and_overfitting_graph2-1-algotrading-300x75.jpg 300w\" data-sizes=\"(max-width: 378px) 100vw, 378px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 378px; aspect-ratio: 378\/95;\" \/><figcaption class=\"wp-element-caption\"><a href=\"https:\/\/docs.aws.amazon.com\/machine-learning\/latest\/dg\/model-fit-underfitting-vs-overfitting.html\">https:\/\/docs.aws.amazon.com\/machine-learning\/latest\/dg\/model-fit-underfitting-vs-overfitting.html<\/a><\/figcaption><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\"><em>Visit <a href=\"https:\/\/algotrading101.com\/learn\/train-test-split\/\">AlgoTrading101<\/a> to read on how to train your model<\/em>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, our focus is on the proper methods for modelling a relationship between 2 assets.<\/p>\n","protected":false},"author":1120,"featured_media":181981,"comment_status":"open","ping_status":"closed","sticky":true,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[339,343,349,338,341],"tags":[851,17968,806,4922,595,11633,17967,17966],"contributors-categories":[13746],"class_list":["post-214588","post","type-post","status-publish","format-standard","has-post-thumbnail","category-data-science","category-programing-languages","category-python-development","category-ibkr-quant-news","category-quant-development","tag-algo-trading","tag-cross-validation","tag-data-science","tag-econometrics","tag-python","tag-split","tag-test","tag-train","contributors-categories-algotrading101"],"pp_statuses_selecting_workflow":false,"pp_workflow_action":"current","pp_status_selection":"publish","acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v26.9 (Yoast SEO v28.0) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Train\/Test Split and Cross Validation &#8211; A Python Tutorial<\/title>\n<meta name=\"description\" content=\"In this article, our focus is on the proper methods for modelling a relationship between 2 assets.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.interactivebrokers.com\/campus\/wp-json\/wp\/v2\/posts\/214588\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Train\/Test Split and Cross Validation - A Python Tutorial\" \/>\n<meta property=\"og:description\" content=\"In this article, our focus is on the proper methods for modelling a relationship between 2 assets.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/train-test-split-and-cross-validation-a-python-tutorial\/\" \/>\n<meta property=\"og:site_name\" content=\"IBKR Campus US\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-04T16:36:41+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-04T16:36:53+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/02\/world-nodes-connect.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"900\" \/>\n\t<meta property=\"og:image:height\" content=\"550\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Greg Bland\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Greg Bland\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\n\t    \"@context\": \"https:\\\/\\\/schema.org\",\n\t    \"@graph\": [\n\t        {\n\t            \"@type\": \"NewsArticle\",\n\t            \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/train-test-split-and-cross-validation-a-python-tutorial\\\/#article\",\n\t            \"isPartOf\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/train-test-split-and-cross-validation-a-python-tutorial\\\/\"\n\t            },\n\t            \"author\": {\n\t                \"name\": \"Greg Bland\",\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/person\\\/61200ed458846d146b20de782ae1c103\"\n\t            },\n\t            \"headline\": \"Train\\\/Test Split and Cross Validation &#8211; A Python Tutorial\",\n\t            \"datePublished\": \"2024-11-04T16:36:41+00:00\",\n\t            \"dateModified\": \"2024-11-04T16:36:53+00:00\",\n\t            \"mainEntityOfPage\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/train-test-split-and-cross-validation-a-python-tutorial\\\/\"\n\t            },\n\t            \"wordCount\": 1127,\n\t            \"commentCount\": 0,\n\t            \"publisher\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#organization\"\n\t            },\n\t            \"image\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/train-test-split-and-cross-validation-a-python-tutorial\\\/#primaryimage\"\n\t            },\n\t            \"thumbnailUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2023\\\/02\\\/world-nodes-connect.jpg\",\n\t            \"keywords\": [\n\t                \"Algo Trading\",\n\t                \"Cross Validation\",\n\t                \"Data Science\",\n\t                \"Econometrics\",\n\t                \"Python\",\n\t                \"split\",\n\t                \"Test\",\n\t                \"Train\"\n\t            ],\n\t            \"articleSection\": [\n\t                \"Data Science\",\n\t                \"Programming Languages\",\n\t                \"Python Development\",\n\t                \"Quant\",\n\t                \"Quant Development\"\n\t            ],\n\t            \"inLanguage\": \"en-US\",\n\t            \"potentialAction\": [\n\t                {\n\t                    \"@type\": \"CommentAction\",\n\t                    \"name\": \"Comment\",\n\t                    \"target\": [\n\t                        \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/train-test-split-and-cross-validation-a-python-tutorial\\\/#respond\"\n\t                    ]\n\t                }\n\t            ]\n\t        },\n\t        {\n\t            \"@type\": \"WebPage\",\n\t            \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/train-test-split-and-cross-validation-a-python-tutorial\\\/\",\n\t            \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/train-test-split-and-cross-validation-a-python-tutorial\\\/\",\n\t            \"name\": \"Train\\\/Test Split and Cross Validation - A Python Tutorial | IBKR Campus US\",\n\t            \"isPartOf\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#website\"\n\t            },\n\t            \"primaryImageOfPage\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/train-test-split-and-cross-validation-a-python-tutorial\\\/#primaryimage\"\n\t            },\n\t            \"image\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/train-test-split-and-cross-validation-a-python-tutorial\\\/#primaryimage\"\n\t            },\n\t            \"thumbnailUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2023\\\/02\\\/world-nodes-connect.jpg\",\n\t            \"datePublished\": \"2024-11-04T16:36:41+00:00\",\n\t            \"dateModified\": \"2024-11-04T16:36:53+00:00\",\n\t            \"description\": \"In this article, our focus is on the proper methods for modelling a relationship between 2 assets.\",\n\t            \"inLanguage\": \"en-US\",\n\t            \"potentialAction\": [\n\t                {\n\t                    \"@type\": \"ReadAction\",\n\t                    \"target\": [\n\t                        \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/train-test-split-and-cross-validation-a-python-tutorial\\\/\"\n\t                    ]\n\t                }\n\t            ]\n\t        },\n\t        {\n\t            \"@type\": \"ImageObject\",\n\t            \"inLanguage\": \"en-US\",\n\t            \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/train-test-split-and-cross-validation-a-python-tutorial\\\/#primaryimage\",\n\t            \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2023\\\/02\\\/world-nodes-connect.jpg\",\n\t            \"contentUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2023\\\/02\\\/world-nodes-connect.jpg\",\n\t            \"width\": 900,\n\t            \"height\": 550,\n\t            \"caption\": \"Autocorrelation and Autocovariance: Calculation, Examples, and More - Part I\"\n\t        },\n\t        {\n\t            \"@type\": \"WebSite\",\n\t            \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#website\",\n\t            \"url\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/\",\n\t            \"name\": \"IBKR Campus US\",\n\t            \"description\": \"Financial Education from Interactive Brokers\",\n\t            \"publisher\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#organization\"\n\t            },\n\t            \"potentialAction\": [\n\t                {\n\t                    \"@type\": \"SearchAction\",\n\t                    \"target\": {\n\t                        \"@type\": \"EntryPoint\",\n\t                        \"urlTemplate\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/?s={search_term_string}\"\n\t                    },\n\t                    \"query-input\": {\n\t                        \"@type\": \"PropertyValueSpecification\",\n\t                        \"valueRequired\": true,\n\t                        \"valueName\": \"search_term_string\"\n\t                    }\n\t                }\n\t            ],\n\t            \"inLanguage\": \"en-US\"\n\t        },\n\t        {\n\t            \"@type\": \"Organization\",\n\t            \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#organization\",\n\t            \"name\": \"Interactive Brokers\",\n\t            \"alternateName\": \"IBKR\",\n\t            \"url\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/\",\n\t            \"logo\": {\n\t                \"@type\": \"ImageObject\",\n\t                \"inLanguage\": \"en-US\",\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/logo\\\/image\\\/\",\n\t                \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2024\\\/05\\\/ibkr-campus-logo.jpg\",\n\t                \"contentUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2024\\\/05\\\/ibkr-campus-logo.jpg\",\n\t                \"width\": 669,\n\t                \"height\": 669,\n\t                \"caption\": \"Interactive Brokers\"\n\t            },\n\t            \"image\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/logo\\\/image\\\/\"\n\t            },\n\t            \"publishingPrinciples\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/about-ibkr-campus\\\/\",\n\t            \"ethicsPolicy\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/cyber-security-notice\\\/\"\n\t        },\n\t        {\n\t            \"@type\": \"Person\",\n\t            \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/person\\\/61200ed458846d146b20de782ae1c103\",\n\t            \"name\": \"Greg Bland\",\n\t            \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/author\\\/greg-bland\\\/\"\n\t        }\n\t    ]\n\t}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Train\/Test Split and Cross Validation &#8211; A Python Tutorial","description":"In this article, our focus is on the proper methods for modelling a relationship between 2 assets.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.interactivebrokers.com\/campus\/wp-json\/wp\/v2\/posts\/214588\/","og_locale":"en_US","og_type":"article","og_title":"Train\/Test Split and Cross Validation - A Python Tutorial","og_description":"In this article, our focus is on the proper methods for modelling a relationship between 2 assets.","og_url":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/train-test-split-and-cross-validation-a-python-tutorial\/","og_site_name":"IBKR Campus US","article_published_time":"2024-11-04T16:36:41+00:00","article_modified_time":"2024-11-04T16:36:53+00:00","og_image":[{"width":900,"height":550,"url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/02\/world-nodes-connect.jpg","type":"image\/jpeg"}],"author":"Greg Bland","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Greg Bland","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"NewsArticle","@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/train-test-split-and-cross-validation-a-python-tutorial\/#article","isPartOf":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/train-test-split-and-cross-validation-a-python-tutorial\/"},"author":{"name":"Greg Bland","@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/person\/61200ed458846d146b20de782ae1c103"},"headline":"Train\/Test Split and Cross Validation &#8211; A Python Tutorial","datePublished":"2024-11-04T16:36:41+00:00","dateModified":"2024-11-04T16:36:53+00:00","mainEntityOfPage":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/train-test-split-and-cross-validation-a-python-tutorial\/"},"wordCount":1127,"commentCount":0,"publisher":{"@id":"https:\/\/ibkrcampus.com\/campus\/#organization"},"image":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/train-test-split-and-cross-validation-a-python-tutorial\/#primaryimage"},"thumbnailUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/02\/world-nodes-connect.jpg","keywords":["Algo Trading","Cross Validation","Data Science","Econometrics","Python","split","Test","Train"],"articleSection":["Data Science","Programming Languages","Python Development","Quant","Quant Development"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/train-test-split-and-cross-validation-a-python-tutorial\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/train-test-split-and-cross-validation-a-python-tutorial\/","url":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/train-test-split-and-cross-validation-a-python-tutorial\/","name":"Train\/Test Split and Cross Validation - A Python Tutorial | IBKR Campus US","isPartOf":{"@id":"https:\/\/ibkrcampus.com\/campus\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/train-test-split-and-cross-validation-a-python-tutorial\/#primaryimage"},"image":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/train-test-split-and-cross-validation-a-python-tutorial\/#primaryimage"},"thumbnailUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/02\/world-nodes-connect.jpg","datePublished":"2024-11-04T16:36:41+00:00","dateModified":"2024-11-04T16:36:53+00:00","description":"In this article, our focus is on the proper methods for modelling a relationship between 2 assets.","inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/train-test-split-and-cross-validation-a-python-tutorial\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/train-test-split-and-cross-validation-a-python-tutorial\/#primaryimage","url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/02\/world-nodes-connect.jpg","contentUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/02\/world-nodes-connect.jpg","width":900,"height":550,"caption":"Autocorrelation and Autocovariance: Calculation, Examples, and More - Part I"},{"@type":"WebSite","@id":"https:\/\/ibkrcampus.com\/campus\/#website","url":"https:\/\/ibkrcampus.com\/campus\/","name":"IBKR Campus US","description":"Financial Education from Interactive Brokers","publisher":{"@id":"https:\/\/ibkrcampus.com\/campus\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/ibkrcampus.com\/campus\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/ibkrcampus.com\/campus\/#organization","name":"Interactive Brokers","alternateName":"IBKR","url":"https:\/\/ibkrcampus.com\/campus\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/logo\/image\/","url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/05\/ibkr-campus-logo.jpg","contentUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/05\/ibkr-campus-logo.jpg","width":669,"height":669,"caption":"Interactive Brokers"},"image":{"@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/logo\/image\/"},"publishingPrinciples":"https:\/\/www.interactivebrokers.com\/campus\/about-ibkr-campus\/","ethicsPolicy":"https:\/\/www.interactivebrokers.com\/campus\/cyber-security-notice\/"},{"@type":"Person","@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/person\/61200ed458846d146b20de782ae1c103","name":"Greg Bland","url":"https:\/\/www.interactivebrokers.com\/campus\/author\/greg-bland\/"}]}},"jetpack_featured_media_url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/02\/world-nodes-connect.jpg","_links":{"self":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/posts\/214588","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/users\/1120"}],"replies":[{"embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/comments?post=214588"}],"version-history":[{"count":0,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/posts\/214588\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/media\/181981"}],"wp:attachment":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/media?parent=214588"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/categories?post=214588"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/tags?post=214588"},{"taxonomy":"contributors-categories","embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/contributors-categories?post=214588"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}