https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.train_test_split.html
实例:
train_set,test_set=train_test_split(housing,test_size=0.2,random_state=42)
编写函数实现数据集划分:参考《机器学习实战:基于Scikit-Learn和TensorFlow》
import pandas as pd
import numpy as np
import hashlib
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]
id=housing["longitude"]*1000+housing["latitude"]
housing_with_id=pd.DataFrame(id,columns=['id'])
train_set,test_set=split_train_test_by_id(housing_with_id,0.2,"id")