<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Dev Haral on Medium]]></title>
        <description><![CDATA[Stories by Dev Haral on Medium]]></description>
        <link>https://medium.com/@devharal777?source=rss-e072bd5bd38------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*0nzdkr6fzOa7T_4lqGTCPg.jpeg</url>
            <title>Stories by Dev Haral on Medium</title>
            <link>https://medium.com/@devharal777?source=rss-e072bd5bd38------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 20:34:46 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@devharal777/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Stratified Shuffle Split]]></title>
            <link>https://medium.com/@devharal777/stratified-shuffle-split-e3e0b58366a1?source=rss-e072bd5bd38------2</link>
            <guid isPermaLink="false">https://medium.com/p/e3e0b58366a1</guid>
            <category><![CDATA[machine-learning-tools]]></category>
            <dc:creator><![CDATA[Dev Haral]]></dc:creator>
            <pubDate>Tue, 15 Nov 2022 19:26:48 GMT</pubDate>
            <atom:updated>2022-11-15T19:31:02.931Z</atom:updated>
            <content:encoded><![CDATA[<p>Suppose a survey company decides to call 1,000 people to ask them a few questions, they don’t just pick 1,000 people randomly in a phone book. They try to ensure that these 1,000 people are representative of the whole population. For example, the US population is com‐ posed of 51.3% female and 48.7% male, so a well-conducted survey in the US would try to maintain this ratio in the sample: 513 female and 487 male. This is called stratified sampling: the population is divided into homogeneous subgroups called strata, and the right number of instances is sampled from each stratum to guarantee that the test set is representative of the overall population. If they used purely random sam‐ pling, there would be about 12% chance of sampling a skewed test set with either less than 49% female or more than 54% female. Either way, the survey results would be significantly biased.</p><p>Suppose you chatted with experts who told you that the median income is a very important attribute to predict median housing prices. You may want to ensure that the test set is representative of the various categories of incomes in the whole dataset. Since the median income is a continuous numerical attribute, you first need to create an income category attribute. Let’s look at the median income histogram more closely (back in Figure 2–8): most median income values are clustered around 1.5 to 6 (i.e., $15,000–$60,000), but some median incomes go far beyond 6. It is important to have a sufficient number of instances in your dataset for each stratum, or else the estimate of the stratum’s importance may be biased. This means that you should not have too many strata, and each stratum should be large enough. The following code uses the pd.cut() function to create an income category attribute with 5 categories (labeled from 1 to 5): category 1 ranges from 0 to 1.5 (i.e., less than $15,000), category 2 from 1.5 to 3, and so on:</p><pre>housing[&quot;income_cat&quot;] = pd.cut(housing[&quot;median_income&quot;],<br> bins=[0., 1.5, 3.0, 4.5, 6., np.inf],<br> labels=[1, 2, 3, 4, 5])</pre><pre>from sklearn.model_selection import StratifiedShuffleSplit<br>split = StratifiedShuffleSplit(n_splits=1, test_size=0.2, random_state=42)<br>for train_index, test_index in split.split(housing, housing[&quot;income_cat&quot;]):<br> strat_train_set = housing.loc[train_index]<br> strat_test_set = housing.loc[test_index]</pre><p>Let’s see if this worked as expected. You can start by looking at the income category proportions in the test set:</p><pre>&gt;&gt;&gt; strat_test_set[&quot;income_cat&quot;].value_counts() / len(strat_test_set)<br>3 0.350533<br>2 0.318798<br>4 0.176357<br>5 0.114583<br>1 0.039729<br>Name: income_cat, dtype: float64</pre><p>With similar code you can measure the income category proportions in the full data‐ set. Figure 2–10 compares the income category proportions in the overall dataset, in the test set generated with stratified sampling, and in a test set generated using purely random sampling. As you can see, the test set generated using stratified sampling has income category proportions almost identical to those in the full dataset, whereas the test set generated using purely random sampling is quite skewed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/601/1*lLtRnYbAaZRPOBJqsYMQ4w.png" /></figure><p>Now you should remove the income_cat attribute so the data is back to its original state:</p><pre>for set_ in (strat_train_set, strat_test_set):<br> set_.drop(&quot;income_cat&quot;, axis=1, inplace=True)</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e3e0b58366a1" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>