这个 numpy 赋值中尾随逗号的作用是啥?
Posted
技术标签:
【中文标题】这个 numpy 赋值中尾随逗号的作用是啥?【英文标题】:What is the effect of the trailing comma in this numpy assignment?这个 numpy 赋值中尾随逗号的作用是什么? 【发布时间】:2021-09-26 21:53:38 【问题描述】:我正在使用下面的代码作为在 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 中搜索了高低并使用类似代码运行了一堆测试,但我找不到使用或不使用逗号之间的任何区别。
【问题讨论】:
【参考方案1】:除了添加逗号只是使部分代码变得多余之外,没有太大区别。但请注意逗号会减慢程序速度,见下文:
>>> 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,)
【讨论】:
以什么方式冗余? 您的示例中的大括号是不必要的。a = 1,
也达到了同样的效果。
@flakes 是的,但不是当我们想将元组传递给函数的参数时:)
@DarrellHougen 在计算机编程中,冗余代码是程序中不必要的代码,例如我们现在讨论的逗号 :)
哇哦。我对那个计时测试完全持怀疑态度,所以我重新安排了一点。这比我想象的要贵得多。多出5-10%。 gist.github.com/cal-pratt/83361d6dce0368b9560a0853ebfa7d63【参考方案2】:
重要任务的时间安排:
In [146]: x=np.zeros((100,100,100))
In [148]: y=np.arange(10000.).reshape(100,100)
In [149]: x[1]=y
In [150]: timeit x[1]=y
6.89 µs ± 64.6 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
In [151]: timeit x[1,]=y
6.89 µs ± 70.5 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
In [153]: timeit x[1,:,:]=y
7.12 µs ± 3.44 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
琐碎任务的次数:
In [154]: timeit x[1]
194 ns ± 3.79 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
In [155]: timeit x[1,]
205 ns ± 10.6 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
有时在我的回答中我会包含尾随冒号x[1,:,:]
。代码并不关心,但它可以帮助人们注意它正在访问多个维度之一。
【讨论】:
以上是关于这个 numpy 赋值中尾随逗号的作用是啥?的主要内容,如果未能解决你的问题,请参考以下文章