我想在Python中创建一些特定尺寸、特定类型的数组,并初始化为相同的值。我可以使用特定大小的NumPy数组,但不确定如何用特定值初始化它们。当然,我不想使用zeros()或ones()。
非常感谢。
非常感谢。
有很多方法可以做到这一点。我想到的第一个单行代码是tile
:
>>> numpy.tile(2, 25)
array([2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2])
您可以在任何形状中平铺一个值:
>>> numpy.tile(2, (5, 5))
array([[2, 2, 2, 2, 2],
[2, 2, 2, 2, 2],
[2, 2, 2, 2, 2],
[2, 2, 2, 2, 2],
[2, 2, 2, 2, 2]])
然而,正如下面的一些答案所示,这并不是最快的方法。它适用于任何大小的平铺数组,而不仅仅是单个值,因此如果您只想用单个值填充一个数组,那么最好先分配数组,然后使用切片赋值:
>>> a = numpy.empty((5, 5), dtype=int)
>>> a[:] = 2
>>> a
array([[2, 2, 2, 2, 2],
[2, 2, 2, 2, 2],
[2, 2, 2, 2, 2],
[2, 2, 2, 2, 2],
[2, 2, 2, 2, 2]])
根据我的几项测试,没有更快的方法。然而,在下面的答案中提到的两种方法同样快速:ndarray.fill
和 numpy.full
。
这些测试均在新的Mac电脑上运行OS 10.12.6,使用Python 3.6.1和ipython
进行。
def fill_tile(value, shape):
return numpy.tile(value, shape)
def fill_assign(value, shape, dtype):
new = numpy.empty(shape, dtype=dtype)
new[:] = value
return new
def fill_fill(value, shape, dtype):
new = numpy.empty(shape, dtype=dtype)
new.fill(value)
return new
def fill_full(value, shape, dtype):
return numpy.full(shape, value, dtype=dtype)
def fill_plus(value, shape, dtype):
new = numpy.zeros(shape, dtype=dtype)
new += value
return new
def fill_plus_oneline(value, shape, dtype):
return numpy.zeros(shape, dtype=dtype) + value
for f in [fill_assign, fill_fill, fill_full, fill_plus, fill_plus_oneline]:
assert (fill_tile(2, (500, 500)) == f(2, (500, 500), int)).all()
tile
的确非常慢:
In [3]: %timeit fill_tile(2, (500, 500))
947 µs ± 10.3 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
切片赋值与ndarray.fill
和numpy.full
并列第一:
In [4]: %timeit fill_assign(2, (500, 500), int)
102 µs ± 1.37 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
In [5]: %timeit fill_fill(2, (500, 500), int)
102 µs ± 1.99 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
In [6]: %timeit fill_full(2, (500, 500), int)
102 µs ± 1.47 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
就地广播加法仅稍慢:
In [7]: %timeit fill_plus(2, (500, 500), int)
179 µs ± 3.7 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
并且非就地广播加法只比它慢一点:
In [8]: %timeit fill_plus_oneline(2, (500, 500), int)
213 µs ± 4.74 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
shape = (100,100)
val = 3.14
dt = np.float
a = np.empty(shape,dtype=dt)
a.fill(val)
通过这种方式,您可以设置和传递参数。此外,在时间方面
In [35]: %timeit a=np.empty(shape,dtype=dt); a.fill(val)
100000 loops, best of 3: 13 us per loop
In [36]: %timeit a=np.tile(val,shape)
10000 loops, best of 3: 102 us per loop
因此,使用fill
与empty
似乎比tile
更快。
numpy.int64(x)
或其他方式来明确它。但速度提升可能是显著的,具体取决于使用情况;在这里,似乎存在可读性和速度之间的小折衷。+1——我很高兴知道这一点。@Shan,你应该接受满足你需求的任何答案。如果你选择接受这个答案,我也不会生气 :) - senderlenumpy.full()
来实现此功能。>>> import numpy as np
>>> np.full((3,4), 100, dtype = int)
array([[ 100, 100, 100, 100],
[ 100, 100, 100, 100],
[ 100, 100, 100, 100]])
np.zeros((N,M),dtype=int)+100
快了2倍。 - hpaulj