

{"id":23722,"date":"2018-08-06T03:58:09","date_gmt":"2018-08-06T03:58:09","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=23722"},"modified":"2026-04-27T17:59:05","modified_gmt":"2026-04-27T12:29:05","slug":"train-test-set-in-python-ml","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/train-test-set-in-python-ml\/","title":{"rendered":"Train and Test Set in Python Machine Learning &#8211; How to Split"},"content":{"rendered":"<p>In our last session, we discussed\u00a0<a href=\"https:\/\/data-flair.training\/blogs\/python-ml-data-preprocessing\/\"><strong>Data Preprocessing, Analysis &amp; Visualization in Python ML<\/strong><\/a>.<strong>\u00a0<\/strong>Now, in this tutorial, we will learn how to split a CSV file into Train and Test Data in <a href=\"https:\/\/data-flair.training\/blogs\/python-machine-learning-tutorial\/\"><strong>Python Machine Learning<\/strong><\/a>. Moreover, we will learn the prerequisites and process for splitting a dataset into training data and a test set in Python ML.<\/p>\n<p>We\u2019ve cleaned our data and seen the charts; now it\u2019s time to focus on the ultimate stress test. Splitting your dataset is like an insurance policy for your code; it\u2019s the only way you can prove that a machine learning model actually starts working before you make it work.<\/p>\n<p>So, let\u2019s begin with how to Train &amp; Test Set in Python Machine Learning.<\/p>\n<div id=\"attachment_23743\" style=\"width: 1210px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/Train-and-Test-Set-in-Python-Machine-Learning-01.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-23743\" class=\"wp-image-23743 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/Train-and-Test-Set-in-Python-Machine-Learning-01.jpg\" alt=\"Train and Test Set in Python Machine Learning - How to Split\" width=\"1200\" height=\"628\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/Train-and-Test-Set-in-Python-Machine-Learning-01.jpg 1200w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/Train-and-Test-Set-in-Python-Machine-Learning-01-150x79.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/Train-and-Test-Set-in-Python-Machine-Learning-01-300x157.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/Train-and-Test-Set-in-Python-Machine-Learning-01-768x402.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/Train-and-Test-Set-in-Python-Machine-Learning-01-1024x536.jpg 1024w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/a><p id=\"caption-attachment-23743\" class=\"wp-caption-text\">Train and Test Set in Python Machine Learning &#8211; How to Split<\/p><\/div>\n<h3 class=\"western\">Training and Test Data in Python Machine Learning<\/h3>\n<p>As we work with datasets, a <a href=\"https:\/\/data-flair.training\/blogs\/machine-learning-algorithm\/\"><strong>machine learning algorithm<\/strong><\/a> works in two stages. We usually split the data around 20%-80% between testing and training stages. Under supervised learning, we split a dataset into training data and test data in Python ML.<\/p>\n<div id=\"attachment_23731\" style=\"width: 478px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/1-16.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-23731\" class=\"wp-image-23731 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/1-16.png\" alt=\"Train and Test Set in Python Machine Learning\" width=\"468\" height=\"183\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/1-16.png 468w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/1-16-150x59.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/1-16-300x117.png 300w\" sizes=\"auto, (max-width: 468px) 100vw, 468px\" \/><\/a><p id=\"caption-attachment-23731\" class=\"wp-caption-text\">Train and Test Set in Python Machine Learning<\/p><\/div>\n<h4><strong>a. Prerequisites for\u00a0Train and Test Data<\/strong><\/h4>\n<p>We will need the following<a href=\"https:\/\/data-flair.training\/blogs\/python-library\/\"><strong> Python libraries<\/strong><\/a> for this tutorial: <a href=\"https:\/\/data-flair.training\/blogs\/pandas-tutorial\/\"><strong>pandas<\/strong><\/a> and sklearn.<br \/>\nWe can install these with pip-<\/p>\n<pre class=\"EnlighterJSRAW\">pip install pandas<\/pre>\n<pre class=\"EnlighterJSRAW\">pip install sklearn<\/pre>\n<p>Also, We use pandas to import the dataset and sklearn to perform the splitting. You can import these packages as-<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; import pandas as pd\r\n&gt;&gt;&gt; from sklearn.model_selection import train_test_split\r\n&gt;&gt;&gt; from sklearn.datasets import load_iris<\/pre>\n<p><strong><a href=\"https:\/\/data-flair.training\/blogs\/python-data-file-formats\/\">Do you Know about Python Data File Formats \u2013 How to Read CSV, JSON, XLS\u00a0<\/a><\/strong><\/p>\n<h3 class=\"western\">How to Split Train and Test Set in Python Machine Learning?<\/h3>\n<p>The following is the process of Train and Test set in Python ML. So, let\u2019s take a dataset first.<\/p>\n<div id=\"attachment_23738\" style=\"width: 1210px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/Split-Train-and-Test-Set-in-Python-Machine-Learning-01.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-23738\" class=\"wp-image-23738 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/Split-Train-and-Test-Set-in-Python-Machine-Learning-01.jpg\" alt=\"How to Split Train and Test Set in Python Machine Learning\" width=\"1200\" height=\"628\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/Split-Train-and-Test-Set-in-Python-Machine-Learning-01.jpg 1200w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/Split-Train-and-Test-Set-in-Python-Machine-Learning-01-150x79.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/Split-Train-and-Test-Set-in-Python-Machine-Learning-01-300x157.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/Split-Train-and-Test-Set-in-Python-Machine-Learning-01-768x402.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/Split-Train-and-Test-Set-in-Python-Machine-Learning-01-1024x536.jpg 1024w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/a><p id=\"caption-attachment-23738\" class=\"wp-caption-text\">How to Split Train and Test Set in Python Machine Learning<\/p><\/div>\n<h4 class=\"western\">a. Loading the Dataset<\/h4>\n<p>Let\u2019s load the forestfires dataset using pandas.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; data=pd.read_csv('forestfires.csv')\r\n&gt;&gt;&gt; data.head()<\/pre>\n<div id=\"attachment_23732\" style=\"width: 585px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/forestfires.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-23732\" class=\"wp-image-23732 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/forestfires.png\" alt=\"Train and Test Set in Python Machine Learning\" width=\"575\" height=\"112\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/forestfires.png 575w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/forestfires-150x29.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/forestfires-300x58.png 300w\" sizes=\"auto, (max-width: 575px) 100vw, 575px\" \/><\/a><p id=\"caption-attachment-23732\" class=\"wp-caption-text\">Train and Test Set in Python Machine Learning<\/p><\/div>\n<h4 class=\"western\">b. Splitting<\/h4>\n<p>Let\u2019s split this data into labels and features. Now, what\u2019s that? Using features, we predict labels. I mean using features (the data we use to predict labels), we predict labels (the data we want to predict).<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; y=data.temp\r\n&gt;&gt;&gt; x=data.drop('temp',axis=1)<\/pre>\n<p>Temp is a label to predict temperatures in y; we use the drop() function to take all other data in x. Then, we split the data.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.2)\r\n&gt;&gt;&gt; x_train.head()<\/pre>\n<div id=\"attachment_23734\" style=\"width: 569px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/train.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-23734\" class=\"wp-image-23734 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/train.png\" alt=\"Train and Test Set in Python Machine Learning\" width=\"559\" height=\"115\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/train.png 559w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/train-150x31.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/train-300x62.png 300w\" sizes=\"auto, (max-width: 559px) 100vw, 559px\" \/><\/a><p id=\"caption-attachment-23734\" class=\"wp-caption-text\">Train and Test Set in Python Machine Learning<\/p><\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; x_train.shape<\/pre>\n<p>(413, 12)<br \/>\n<strong><a href=\"https:\/\/data-flair.training\/blogs\/relational-database-with-python\/\">Do you Know How to work with relational databases with Python<\/a><\/strong><\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; x_test.head()<\/pre>\n<div id=\"attachment_23733\" style=\"width: 567px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/test.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-23733\" class=\"wp-image-23733 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/test.png\" alt=\"Train and Test Set in Python Machine Learning\" width=\"557\" height=\"113\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/test.png 557w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/test-150x30.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/test-300x61.png 300w\" sizes=\"auto, (max-width: 557px) 100vw, 557px\" \/><\/a><p id=\"caption-attachment-23733\" class=\"wp-caption-text\">Train and Test Set in Python Machine Learning<\/p><\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; x_test.shape<\/pre>\n<p>(104, 12)<br \/>\nThe line test_size=0.2 suggests that the test data should be 20% of the dataset and the rest should be train data. With the outputs of the shape() functions, you can see that we have 104 rows in the test data and 413 in the training data.<\/p>\n<h4 class=\"western\">c. Another Example<\/h4>\n<p>Let\u2019s take another example. We\u2019ll use the IRIS dataset this time.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; iris=load_iris()\r\n&gt;&gt;&gt; x,y=iris.data,iris.target\r\n&gt;&gt;&gt; x_train,x_test,y_train,y_test=train_test_split(x,y,\r\ntrain_size=0.5,\r\ntest_size=0.5,\r\nrandom_state=123)\r\n&gt;&gt;&gt; y_test<\/pre>\n<p><strong>array([1, 2, 2, 1, 0, 2, 1, 0, 0, 1, 2, 0, 1, 2, 2, 2, 0, 0, 1, 0, 0, 2,<\/strong><br \/>\n<strong>0, 2, 0, 0, 0, 2, 2, 0, 2, 2, 0, 0, 1, 1, 2, 0, 0, 1, 1, 0, 2, 2,<\/strong><br \/>\n<strong>2, 2, 2, 1, 0, 0, 2, 0, 0, 1, 1, 1, 1, 2, 1, 2, 0, 2, 1, 0, 0, 2,<\/strong><br \/>\n<strong>1, 2, 2, 0, 1, 1, 2, 0, 2])<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; y_train<\/pre>\n<p><strong>array([1, 1, 0, 2, 2, 0, 0, 1, 1, 2, 0, 0, 1, 0, 1, 2, 0, 2, 0, 0, 1, 0,<\/strong><br \/>\n<strong>0, 1, 2, 1, 1, 1, 0, 0, 1, 2, 0, 0, 1, 1, 1, 2, 1, 1, 1, 2, 0, 0,<\/strong><br \/>\n<strong>1, 2, 2, 2, 2, 0, 1, 0, 1, 1, 0, 1, 2, 1, 2, 2, 0, 1, 0, 2, 2, 1,<\/strong><br \/>\n<strong>1, 2, 2, 1, 0, 1, 1, 2, 2])<\/strong><\/p>\n<p>One of the essential approaches when dividing data into training and testing sets is to capture the model\u2019s performance on unseen data. This practice aids in evaluating the model\u2019s performance and potentially recognizing problems such as overfitting or underfitting. We continuously split the dataset in order to check our model\u2019s capabilities of not memorizing the data, yet capable of classifying new instances.<\/p>\n<p>Furthermore, it should be noted that setting the random_state parameter in train_test_split makes the dataset creation repeatable. This parameter is set to a fixed integer to make sure that the same splitting of data is achieved every time a specific model is tested; thus is essential during the testing of the model.<\/p>\n<p><strong><a href=\"https:\/\/data-flair.training\/blogs\/python-machine-learning-environment-setup\/\">Let&#8217;s explore Python Machine Learning Environment Setup\u00a0<\/a><\/strong><\/p>\n<h3 class=\"western\">Plotting of Train and Test Set in Python<\/h3>\n<p>We fit our model on the training data to make predictions on it. Let\u2019s import the linear_model from sklearn, apply linear regression to the dataset, and plot the results.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; from sklearn.linear_model import LinearRegression as lm\r\n&gt;&gt;&gt; model=lm().fit(x_train,y_train)\r\n&gt;&gt;&gt; predictions=model.predict(x_test)\r\n&gt;&gt;&gt; import matplotlib.pyplot as plt\r\n&gt;&gt;&gt; plt.scatter(y_test,predictions)<\/pre>\n<p><strong>&lt;matplotlib.collections.PathCollection object at 0x0651CA30&gt;<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; plt.xlabel('True values')<\/pre>\n<p><strong>Text(0.5,0,&#8217;True values&#8217;)<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; plt.ylabel('Predictions')<\/pre>\n<p><strong>Text(0,0.5,&#8217;Predictions&#8217;)<\/strong><br \/>\n<strong><a href=\"https:\/\/data-flair.training\/blogs\/python-numpy-tutorial\/\">Read about Python NumPy \u2013 NumPy ndarray &amp; NumPy Array<\/a><\/strong><\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; plt.show()<\/pre>\n<div id=\"attachment_23735\" style=\"width: 605px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/plot-2.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-23735\" class=\"wp-image-23735 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/plot-2.png\" alt=\"Train and Test Set in Python Machine Learning\" width=\"595\" height=\"448\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/plot-2.png 595w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/plot-2-150x113.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/plot-2-300x226.png 300w\" sizes=\"auto, (max-width: 595px) 100vw, 595px\" \/><\/a><p id=\"caption-attachment-23735\" class=\"wp-caption-text\">Train and Test Set in Python Machine Learning<\/p><\/div>\n<p><strong>0.9396299518034936<\/strong><br \/>\nSo, this was all about Train and Test Set in Python Machine Learning. Hope you like our explanation.<\/p>\n<h3 class=\"western\">Conclusion<\/h3>\n<p>Today, we learned how to split a CSV or a dataset into two subsets- the training set and the test set in Python Machine Learning. We usually let the test set be 20% of the entire data set, and the rest 80% will be the training set. Furthermore, if you have a query, feel free to ask in the comment box.<br \/>\nRelated Topic-\u00a0<strong><a href=\"https:\/\/data-flair.training\/blogs\/python-geographic-maps-graph-data\/\">Python Geographic Maps &amp; Graph Data<\/a><\/strong><br \/>\n<strong><a href=\"https:\/\/www.python.org\/\">For reference<\/a><\/strong><span hidden class=\"__iawmlf-post-loop-links\" data-iawmlf-links=\"[{&quot;id&quot;:149,&quot;href&quot;:&quot;https:\\\/\\\/www.python.org&quot;,&quot;archived_href&quot;:&quot;http:\\\/\\\/web-wp.archive.org\\\/web\\\/20251206090101\\\/https:\\\/\\\/www.python.org\\\/&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2025-12-06 12:20:59&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2025-12-09 12:44:48&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2025-12-12 13:49:48&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2025-12-15 14:13:48&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2025-12-18 15:26:07&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2025-12-21 17:05:18&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2025-12-24 19:33:20&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2025-12-28 02:44:18&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2025-12-31 04:43:13&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-01-03 07:01:16&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-01-06 07:15:14&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-01-09 07:16:21&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-01-12 10:01:16&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-01-15 10:07:06&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-01-18 10:11:43&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-01-21 10:20:21&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-01-24 10:47:21&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-01-27 10:58:10&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-01-30 10:59:29&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-02 12:28:37&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-05 13:05:41&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-08 15:11:10&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-11 15:46:29&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-14 17:21:34&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-17 18:37:27&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-20 18:52:05&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-23 19:52:29&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-27 01:02:50&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-02 03:50:52&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-05 05:18:10&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-08 06:18:52&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-11 07:24:15&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-14 08:33:37&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-17 08:58:17&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-20 12:26:41&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-23 14:32:34&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-26 16:21:46&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-29 17:22:50&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-01 18:18:54&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-04 18:27:04&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-08 02:33:55&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-11 04:53:57&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-14 06:48:30&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-17 07:17:55&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-20 07:32:43&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-23 09:34:41&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-26 10:13:17&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-29 10:35:31&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-05-02 11:50:34&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-05-05 12:07:03&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-05-08 13:08:24&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-05-11 14:46:17&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-05-14 21:24:09&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-05-18 03:08:37&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-05-21 06:27:39&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-05-24 07:06:36&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-05-27 07:30:50&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-05-30 08:47:47&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-06-02 09:37:18&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-06-05 09:43:29&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-06-08 10:40:15&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-06-11 10:49:02&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-06-14 16:29:16&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-06-17 18:19:15&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-06-20 22:43:57&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-06-24 02:56:27&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-06-27 03:13:02&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-06-30 03:52:58&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-07-03 04:15:28&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-07-06 05:06:06&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-07-09 05:35:45&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-07-12 06:10:30&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-07-15 07:03:59&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-07-18 07:25:58&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-07-21 07:48:03&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-07-24 08:26:41&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-07-27 08:34:39&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-07-30 09:24:48&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-08-02 10:06:50&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-08-05 10:49:31&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-08-08 10:53:52&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-08-11 11:13:51&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-08-14 13:23:10&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-08-17 14:08:02&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-08-20 15:23:43&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-08-23 16:52:16&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-08-26 20:31:34&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-08-30 01:11:43&quot;,&quot;http_code&quot;:206}],&quot;broken&quot;:false,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-08-30 01:11:43&quot;,&quot;http_code&quot;:206},&quot;process&quot;:&quot;done&quot;}]\"><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In our last session, we discussed\u00a0Data Preprocessing, Analysis &amp; Visualization in Python ML.\u00a0Now, in this tutorial, we will learn how to split a CSV file into Train and Test Data in Python Machine Learning.&#46;&#46;&#46;<\/p>\n","protected":false},"author":5,"featured_media":23743,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[6336,6337,9542,9948,12945,14871,14872,14873,14877],"class_list":["post-23722","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-how-to-train-data-in-python","tag-how-to-train-data-set-in-python","tag-plotting-of-train-and-test-set-in-python","tag-prerequisites-for-train-and-test-data","tag-sklearn-train-test-split-stratified","tag-train-test-split-numpy","tag-train-test-split-python","tag-train_test_split-random_state","tag-training-and-test-data-in-python-machine-learning"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Train and Test Set in Python Machine Learning - How to Split - DataFlair<\/title>\n<meta name=\"description\" content=\"Train and Test Set in Python Machine Learning - How to Split Train Data and Test Data in Python ML, How to Plot Train set and Test Set in Python\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/train-test-set-in-python-ml\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Train and Test Set in Python Machine Learning - How to Split - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Train and Test Set in Python Machine Learning - How to Split Train Data and Test Data in Python ML, How to Plot Train set and Test Set in Python\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/train-test-set-in-python-ml\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-08-06T03:58:09+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-04-27T12:29:05+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/Train-and-Test-Set-in-Python-Machine-Learning-01.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Train and Test Set in Python Machine Learning - How to Split - DataFlair","description":"Train and Test Set in Python Machine Learning - How to Split Train Data and Test Data in Python ML, How to Plot Train set and Test Set in Python","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:\/\/data-flair.training\/blogs\/train-test-set-in-python-ml\/","og_locale":"en_US","og_type":"article","og_title":"Train and Test Set in Python Machine Learning - How to Split - DataFlair","og_description":"Train and Test Set in Python Machine Learning - How to Split Train Data and Test Data in Python ML, How to Plot Train set and Test Set in Python","og_url":"https:\/\/data-flair.training\/blogs\/train-test-set-in-python-ml\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2018-08-06T03:58:09+00:00","article_modified_time":"2026-04-27T12:29:05+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/Train-and-Test-Set-in-Python-Machine-Learning-01.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/train-test-set-in-python-ml\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/train-test-set-in-python-ml\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823"},"headline":"Train and Test Set in Python Machine Learning &#8211; How to Split","datePublished":"2018-08-06T03:58:09+00:00","dateModified":"2026-04-27T12:29:05+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/train-test-set-in-python-ml\/"},"wordCount":818,"commentCount":37,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/train-test-set-in-python-ml\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/Train-and-Test-Set-in-Python-Machine-Learning-01.jpg","keywords":["how to train data in python","how to train data set in python","Plotting of Train and Test Set in Python","Prerequisites for Train and Test Data","sklearn train test split stratified","train test split numpy","train test split python","train_test_split random_state","Training and Test Data in Python Machine Learning"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/train-test-set-in-python-ml\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/train-test-set-in-python-ml\/","url":"https:\/\/data-flair.training\/blogs\/train-test-set-in-python-ml\/","name":"Train and Test Set in Python Machine Learning - How to Split - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/train-test-set-in-python-ml\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/train-test-set-in-python-ml\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/Train-and-Test-Set-in-Python-Machine-Learning-01.jpg","datePublished":"2018-08-06T03:58:09+00:00","dateModified":"2026-04-27T12:29:05+00:00","description":"Train and Test Set in Python Machine Learning - How to Split Train Data and Test Data in Python ML, How to Plot Train set and Test Set in Python","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/train-test-set-in-python-ml\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/train-test-set-in-python-ml\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/train-test-set-in-python-ml\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/Train-and-Test-Set-in-Python-Machine-Learning-01.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/08\/Train-and-Test-Set-in-Python-Machine-Learning-01.jpg","width":1200,"height":628,"caption":"Train and Test Set in Python Machine Learning - How to Split"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/train-test-set-in-python-ml\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Python Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/python\/"},{"@type":"ListItem","position":3,"name":"Train and Test Set in Python Machine Learning &#8211; How to Split"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team creates expert-level guides on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our goal is to empower learners with easy-to-understand content. Explore our resources for career growth and practical learning.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam1\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/23722","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=23722"}],"version-history":[{"count":11,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/23722\/revisions"}],"predecessor-version":[{"id":147965,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/23722\/revisions\/147965"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/23743"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=23722"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=23722"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=23722"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}