我正在读一本关于机器学习的书,作者谈到了训练和测试拆分过程中的随机种子,作者说,在这段时间里,机器将看到你的整个数据集。
作者正在使用以下函数来划分Tran和Test split,
def split_train_test(data, test_ratio):
shuffled_indices = np.random.permutation(len(data))
test_set_size = int(len(data) * test_ratio)
test_indices = shuffled_indices[:test_set_size]
train_indices = shuffled_indices[test_set_size:]
return data.iloc[train_indices], data.iloc[test_indices]
Usage of the function like this:
>>>train_set, test_set = split_train_test(housing, 0.2)
>>> len(train_set)
16512
>>> len(test_set)
4128嗯,这是可行的,但它并不完美:如果您再次运行该程序,它将生成一个不同的测试集!随着时间的推移,你(或你的机器学习算法)将看到整个数据集,这是你想要避免的。
Sachin Rastogi:为什么以及如何影响我的模型性能?我知道我的模型精度在每一次运行中都会有所不同,因为训练集总是不同的。我的模型将如何在一段时间内看到整个数据集?
作者还提供了一些解决方案,
一种解决方案是在第一次运行时保存测试集,然后在后续运行中加载它。另一种选择是在调用np.random.permutation()之前设置随机数生成器的种子(例如,np.random.seed(42)),以便它始终生成相同的混洗索引。
但这两个解决方案都将在您下次获取更新的数据集时中断。一种常见的解决方案是使用每个实例的标识符来决定它是否应该进入测试集中(假设实例具有唯一且不可变的标识符)。
Sachin Rastogi:这会是一个很好的训练/测试部门吗?我认为不,Train和Test应该包含跨数据集的元素,以避免来自Train集的任何偏差。
作者举了一个例子,
您可以计算每个实例的标识符的散列,如果散列小于或等于最大散列值的20%,则将该实例放入测试集中。这可确保测试集在多次运行时保持一致,即使您刷新数据集也是如此。
新的测试集将包含20%的新实例,但它将不包含以前在训练集中的任何实例。
Sachin Rastogi:我无法理解这个解决方案。你能帮帮忙吗?
发布于 2020-10-21 06:24:13
对我来说,答案是:
- Instances that were put in the _training set_ before the update of the dataset will remain there (as their hash value won't change - and so their left-most bit - and it will remain higher than 0.2\*max\_hash\_value);
- Instances that were put in the _test set_ before the update of the dataset will remain there (as their hash value won't change and it will remain lower than 0.2\*max\_hash\_value).更新后的测试集将包含20%的新实例以及与旧测试集关联的所有实例,使其保持稳定。
我还建议在这里查看作者的解释:https://github.com/ageron/handson-ml/issues/71。
https://stackoverflow.com/questions/56365869
复制相似问题