gpt4 book ai didi

python - 在此 numpy 分配中尾随逗号的效果是什么?

转载 作者:行者123 更新时间:2023-12-05 03:37:25 27 4
gpt4 key购买 nike

我使用下面的代码作为在 TensorFlow 中编写数据生成器函数的模板,我想知道下面的尾随逗号是否必要或有用:

def __data_generation(self, list_IDs_temp):
'Generates data containing batch_size samples' # X : (n_samples, *dim, n_channels)
# Initialization
X = np.empty((self.batch_size, *self.dim, self.n_channels))
y = np.empty((self.batch_size), dtype=int)

# Generate data
for i, ID in enumerate(list_IDs_temp):
# Store sample
X[i,] = np.load('data/' + ID + '.npy')

# Store class
y[i] = self.labels[ID]

return X, keras.utils.to_categorical(y, num_classes=self.n_classes)

X[i,] 中的逗号有什么作用吗?我搜索了高低,并在 Jupyter 中使用类似代码运行了一堆测试,但我找不到使用或不使用逗号之间的任何区别。

最佳答案

除了添加逗号只是使部分代码变得多余之外,没有太大区别。但请注意,逗号会减慢程序,见下文:

>>> from timeit import timeit
>>> timeit('a[:3,]', 'import numpy as np; a = np.array([1, 2, 3, 4, 5])')
0.26200279999999765
>>> timeit('a[:3,]', 'import numpy as np; a = np.array([1, 2, 3, 4, 5])')
0.27410390000000007
>>> timeit('a[:3,]', 'import numpy as np; a = np.array([1, 2, 3, 4, 5])')
0.3642131000000006
>>> timeit('a[:3,]', 'import numpy as np; a = np.array([1, 2, 3, 4, 5])')
0.3105785999999995
>>> timeit('a[:3,]', 'import numpy as np; a = np.array([1, 2, 3, 4, 5])')
0.2766163000000006
>>> timeit('a[:3,]', 'import numpy as np; a = np.array([1, 2, 3, 4, 5])')
0.2650689999999969
>>> timeit('a[:3,]', 'import numpy as np; a = np.array([1, 2, 3, 4, 5])')
0.2776439999999951
>>> timeit('a[:3,]', 'import numpy as np; a = np.array([1, 2, 3, 4, 5])')
0.3056855999999968
>>> timeit('a[:3,]', 'import numpy as np; a = np.array([1, 2, 3, 4, 5])')
0.2718677000000014
>>> timeit('a[:3,]', 'import numpy as np; a = np.array([1, 2, 3, 4, 5])')
0.2666911999999968
>>> from timeit import timeit
>>> timeit('a[:3]', 'import numpy as np; a = np.array([1, 2, 3, 4, 5])')
0.25228500000000054
>>> timeit('a[:3]', 'import numpy as np; a = np.array([1, 2, 3, 4, 5])')
0.23471499999999423
>>> timeit('a[:3]', 'import numpy as np; a = np.array([1, 2, 3, 4, 5])')
0.3306362000000007
>>> timeit('a[:3]', 'import numpy as np; a = np.array([1, 2, 3, 4, 5])')
0.2560698000000059
>>> timeit('a[:3]', 'import numpy as np; a = np.array([1, 2, 3, 4, 5])')
0.2566029000000043
>>> timeit('a[:3]', 'import numpy as np; a = np.array([1, 2, 3, 4, 5])')
0.24175780000000202
>>> timeit('a[:3]', 'import numpy as np; a = np.array([1, 2, 3, 4, 5])')
0.23682909999999424
>>> timeit('a[:3]', 'import numpy as np; a = np.array([1, 2, 3, 4, 5])')
0.2400262999999967
>>> timeit('a[:3]', 'import numpy as np; a = np.array([1, 2, 3, 4, 5])')
0.2468849999999918
>>> timeit('a[:3]', 'import numpy as np; a = np.array([1, 2, 3, 4, 5])')
0.22863809999999773

有关更多信息(尽管我确定您已经知道这一点),在两个括号之间的对象后添加一个逗号确实有所不同,因为它创建了一个元组并且没有它,括号就会消失:

a = (1)
print(a)
a = (1,)
print(a)

输出:

1
(1,)

关于python - 在此 numpy 分配中尾随逗号的效果是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69339254/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com