Need help in understanding a small example Pardon me, I agree the title of the question is not clear. I would like to know the understanding of below steps which are picked from the textbook "Hands on machine learning".

 &gt;&gt;&gt; housing['income_cat'].value_counts() 
 &gt;&gt;&gt; 3.0    7236
     2.0    6581
     4.0    3639
     5.0    2362
     1.0     822



  If I am not wrong, the above step is to get the counts for each class. For example, for class '3' there are 7236 instances. Likewise, for class '2' there are 6581 instances. 


&gt;&gt;&gt;  housing['income_cat'].value_counts / len(housing) 
&gt;&gt;&gt;  3.0    0.350581
     2.0    0.318847
     4.0    0.176308
     5.0    0.114438
     1.0    0.039826



  Next, I was not clear, what was the intention behind the above step. By doing the above step, what am I suppose to learn ?. and


 &gt;&gt;&gt; from sklearn.model_selection import StratifiedShuffleSplit

 &gt;&gt;&gt; split = StratifiedShuffleSplit(n_splits=1, test_size=0.2, random_state=42)
 &gt;&gt;&gt; for train_index, test_index in split.split(housing, housing["income_cat"]):
         strat_train_set = housing.loc[train_index]
         strat_test_set = housing.loc[test_index]

 &gt;&gt;&gt;  strat_test_set['income_cat'].value_counts() / len(strat_test_set)
 &gt;&gt;&gt;  3.0    0.350533
      2.0    0.318798
      4.0    0.176357
      5.0    0.114583
      1.0    0.039729
      Name: income_cat, dtype: float64



  How come strat_test_set['income_cat'].value_counts() / len(strat_test_set) results are almost same to the results of housing['income_cat'].value_counts / len(housing) ?
Regarding your first question, by dividing by the length, you get the percentage of each category, and you can see that one category has a very low percentage. 

If you use the regular train_test_split, the proportion of the categories will be different in each set, as the split is made randomly, and this can introduce a bias.

Imagine categories with a very low number of observations, you could even have categories missing entirely in either of the sets, which will cause trouble for your model.

The use of stratified sampling allows you to have the same proportion for the categories. That is why you see the same results: it is the goal of this sampling.