机器学习建立训练集合测试集的函数

我现在在阅读 Hands-On Machine Learning with Scikit-Learn & TensorFlow 书中在建立测试集中给出了如下代码:

def test_set_check(identifier, test_ratio, hash):
    return hash(np.int64(identifier)).digest()[-1] < 256 * test_ratio


def split_train_test_by_id(data, test_ratio, id_column, hash=hashlib.md5):
    ids = data[id_column]
    in_test_set = ids.apply(lambda id_: test_set_check(id_, test_ratio, hash))
    return data.loc[~in_test_set], data.loc[in_test_set]


housing_with_id = housing.reset_index()  # adds an 'index' column
train_set, test_set = split_train_test_by_id(housing_with_id, 0.2, "index")
print(len(train_set), "train+", len(test_set), "test")

我个人的感觉这段代码的目的是为了让读者理解是如何得到训练集合测试集的,实际工作中应该不用自行输入这一段代码。
如果我的感觉是正确的实际的工作中是如何做的呢?

from sklearn.model_selection import train_test_split
train_set, test_set = train_test_split(housing, test_size=0.2, random_state=42)