[英]How does enumerate(zip(*k_fold(dataset, folds))) work?
If we have:如果我们有:
a = ['a', 'aa', 'aaa']
b = ['b', 'bb', 'bbb']
for i, (x, y) in enumerate(zip(a, b)):
print (i, x, y)
then the code prints:然后代码打印:
0 a b
1 aa bb
2 aaa bbb
To iterate over all elements of the two lists, they must have the same size.要遍历两个列表的所有元素,它们必须具有相同的大小。
Now, if we have the following snippet:现在,如果我们有以下代码段:
for fold, (train_idx, test_idx, val_idx) in enumerate(zip(*k_fold(dataset, folds))):
pass
where len(dataset)
= 1000 and folds
= 3, then how does the code works in terms of *k_fold(dataset, folds)
?其中
len(dataset)
= 1000 和folds
= 3,那么代码在*k_fold(dataset, folds)
方面是如何工作的?
EDIT:编辑:
I add the reference of the snippet about which my question is, it is line 31 of this code .我添加了我的问题所在的片段的引用,它是此代码的第 31 行。
enumerate
function enumerate
函数Enumeration is used to iterate through an iterable whilst keeping an integer count of the number of iterations, so:枚举用于遍历一个可迭代对象,同时保持迭代次数的整数计数,因此:
>>> for number, value in enumerate(["a", "b", "c"]):
... print(number, value)
1 a
2 b
3 c
zip
function zip
函数The built-in function zip
is used to combine two iterables like so:内置函数
zip
用于组合两个可迭代对象,如下所示:
>>> a = [1, 2]
>>> b = [3, 4]
>>> list(zip(a, b))
[(1, 3), (2, 4)]
When zip
is provided with iterables of different length, then it returns a zip
object with the length of the shortest iterable.当
zip
提供不同长度的可迭代对象时,它会返回一个具有最短可迭代对象长度的zip
对象。 So:所以:
>>> a = [1, 2, 5, 6]
>>> b = [3, 4]
>>> list(zip(a, b))
[(1, 3), (2, 4)]
Python uses the *
to unpack iterables. Python 使用
*
来解压可迭代对象。 Looking through the GitHub repository, it seems that k_fold
returns a tuple with 3 elements.查看 GitHub 存储库,似乎
k_fold
返回了一个包含 3 个元素的元组。 This is so that they can pass the values that the k_fold
function returns into the iterable.这样他们就可以将
k_fold
函数返回的值传递给可迭代对象。
bonus example:奖金示例:
a = [1, 2, 5, 6, 8, 9, 10 , 11]
b = [3, 4, 12, 13 ]
c = [ 14, 15 ]
for i in enumerate(zip(a, b, c)):
print(i)
output:输出:
(0, (1, 3, 14))
(1, (2, 4, 15)) -----> like they are fold, (train_idx, test_idx, val_idx)
not sure about what train_idx, test_idx, val_idx are in the code on github:不确定 github 上的代码中的 train_idx、test_idx、val_idx 是什么:
train_idx, test_idx val_idx are lists don't know with what they are filled though ! train_idx, test_idx val_idx 是列表,但不知道它们填充了什么!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.