简体   繁体   English

enumerate(zip(*k_fold(dataset, folds))) 如何工作?

[英]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 行。

Python's enumerate function Python的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

Python's zip function Python 的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's unpacking operator Python 的解包操作符

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.

相关问题 如何计算 K 折交叉验证中所有折的分类报告中的平均值? - How to compute the average values in classification report for all folds in a K-fold Cross-validation? 如何枚举/压缩为lambda - how to enumerate / zip as lambda 如何在不一次加载整个数据集的情况下将数据集拆分为 K 折叠? - How to split dataset into K-fold without loading the whole dataset at once? 对整个数据集进行 K 折交叉验证 - K-Fold Cross Validation on entire Dataset scikit中的GridSearchCV如何学习选择k折的最佳参数 - How does GridSearchCV in scikit learn pick the best parameters for k fold Pytorch的“折叠”和“展开”是如何工作的? - How does Pytorch's "Fold" and "Unfold" work? 如何使用 zip 文件中的 kaggle 数据集? - How to work with a kaggle dataset in a zip file? 枚举如何在多项式函数(Python)中工作? - How does enumerate work in polynomial function (Python)? 如何实现n次重复k折交叉验证,从而在sklearn中产生n * k折? - How to implement n times repeated k-folds cross validation that yields n*k folds in sklearn? "如何计算分层 K 折交叉验证的不平衡数据集的误报率?" - How to compute false positive rate of an imbalanced dataset for Stratified K fold cross validation?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM