NumPy数组初始化(填充相同的值)

时间:2022-06-20 01:54:05

I need to create a NumPy array of length n, each element of which is v.

我需要创建一个长度为n的NumPy数组,每个元素都是v。

Is there anything better than:

还有比这更好的吗?

a = empty(n)
for i in range(n):
    a[i] = v

I know zeros and ones would work for v = 0, 1. I could use v * ones(n), but it won't work when v is None, and also would be much slower.

我知道0和1对v = 0, 1。我可以用v * 1 (n),但是当v是0的时候它就不成立了,而且也会慢得多。

6 个解决方案

#1


164  

NumPy 1.8 introduced np.full(), which is a more direct method than empty() followed by fill() for creating an array filled with a certain value:

NumPy 1.8引入了np.full(),这是一个比empty()更直接的方法,然后填充()来创建一个包含一定值的数组:

>>> np.full((3, 5), 7)
array([[ 7.,  7.,  7.,  7.,  7.],
       [ 7.,  7.,  7.,  7.,  7.],
       [ 7.,  7.,  7.,  7.,  7.]])

>>> np.full((3, 5), 7, dtype=int)
array([[7, 7, 7, 7, 7],
       [7, 7, 7, 7, 7],
       [7, 7, 7, 7, 7]])

This is arguably the way of creating an array filled with certain values, because it explicitly describes what is being achieved (and it can in principle be very efficient since it performs a very specific task).

这可以说是创建一个充满了特定值的数组的方法,因为它明确地描述了正在实现的内容(而且在原则上,它可以非常高效,因为它执行一个非常特定的任务)。

#2


76  

Updated for Numpy 1.7.0:(Hat-tip to @Rolf Bartstra.)

更新为Numpy 1.7.0:(Hat-tip to @Rolf Bartstra)。

a=np.empty(n); a.fill(5) is fastest.

一个= np.empty(n);a.fill(5)是最快的。

In descending speed order:

下行速度顺序:

%timeit a=np.empty(1e4); a.fill(5)
100000 loops, best of 3: 5.85 us per loop

%timeit a=np.empty(1e4); a[:]=5 
100000 loops, best of 3: 7.15 us per loop

%timeit a=np.ones(1e4)*5
10000 loops, best of 3: 22.9 us per loop

%timeit a=np.repeat(5,(1e4))
10000 loops, best of 3: 81.7 us per loop

%timeit a=np.tile(5,[1e4])
10000 loops, best of 3: 82.9 us per loop

#3


54  

I believe fill is the fastest way to do this.

我相信填充是最快的方法。

a = np.empty(10)
a.fill(7)

You should also always avoid iterating like you are doing in your example. A simple a[:] = v will accomplish what your iteration does using numpy broadcasting.

您还应该始终避免迭代,就像您在示例中所做的那样。一个简单的A [:] = v将完成您的迭代使用的numpy广播。

#4


13  

Apparently, not only the absolute speeds but also the speed order (as reported by user1579844) are machine dependent; here's what I found:

显然,不仅是绝对速度,而且速度顺序(如user1579844所报告的)是依赖于机器的;以下是我的发现:

a=np.empty(1e4); a.fill(5) is fastest;

一个= np.empty(1 e4);a.fill(5)是最快的;

In descending speed order:

下行速度顺序:

timeit a=np.empty(1e4); a.fill(5) 
# 100000 loops, best of 3: 10.2 us per loop
timeit a=np.empty(1e4); a[:]=5
# 100000 loops, best of 3: 16.9 us per loop
timeit a=np.ones(1e4)*5
# 100000 loops, best of 3: 32.2 us per loop
timeit a=np.tile(5,[1e4])
# 10000 loops, best of 3: 90.9 us per loop
timeit a=np.repeat(5,(1e4))
# 10000 loops, best of 3: 98.3 us per loop
timeit a=np.array([5]*int(1e4))
# 1000 loops, best of 3: 1.69 ms per loop (slowest BY FAR!)

So, try and find out, and use what's fastest on your platform.

所以,试着找出,并在你的平台上使用最快的。

#5


6  

You can use numpy.tile, e.g. :

您可以使用numpy。瓷砖,例如:

v = 7
rows = 3
cols = 5
a = numpy.tile(v, (rows,cols))
a
Out[1]: 
array([[7, 7, 7, 7, 7],
       [7, 7, 7, 7, 7],
       [7, 7, 7, 7, 7]])

Although tile is meant to 'tile' an array (instead of a scalar, as in this case), it will do the job, creating pre-filled arrays of any size and dimension.

尽管tile的意思是“tile”(而不是标量),它将完成任务,创建任何大小和维度的预填充数组。

#6


6  

I had

我有

numpy.array(n * [value])

in mind, but apparently that is slower than all other suggestions for large enough n.

但显然,这比其他所有建议都要慢。

Here is full comparison with perfplot (a pet project of mine).

这是与perfplot(我的一个宠物项目)的完全比较。

NumPy数组初始化(填充相同的值)

The two empty alternatives are still the fastest (with NumPy 1.12.1). full catches up for large arrays.

两个空的替代品仍然是最快的(与NumPy 1.12.1)。完全捕获大型数组。


Code to generate the plot:

产生情节的代码:

import numpy as np
import perfplot


def empty_fill(n):
    a = np.empty(n)
    a.fill(3.14)
    return a


def empty_colon(n):
    a = np.empty(n)
    a[:] = 3.14
    return a


def ones_times(n):
    return 3.14 * np.ones(n)


def repeat(n):
    return np.repeat(3.14, (n))


def tile(n):
    return np.repeat(3.14, [n])


def full(n):
    return np.full((n), 3.14)


def list_to_array(n):
    return np.array(n * [3.14])


perfplot.show(
    setup=lambda n: n,
    kernels=[
        empty_fill, empty_colon, ones_times, repeat, tile, full, list_to_array
        ],
    n_range=[2**k for k in range(23)],
    xlabel='len(a)',
    logx=True,
    logy=True,
    )

#1


164  

NumPy 1.8 introduced np.full(), which is a more direct method than empty() followed by fill() for creating an array filled with a certain value:

NumPy 1.8引入了np.full(),这是一个比empty()更直接的方法,然后填充()来创建一个包含一定值的数组:

>>> np.full((3, 5), 7)
array([[ 7.,  7.,  7.,  7.,  7.],
       [ 7.,  7.,  7.,  7.,  7.],
       [ 7.,  7.,  7.,  7.,  7.]])

>>> np.full((3, 5), 7, dtype=int)
array([[7, 7, 7, 7, 7],
       [7, 7, 7, 7, 7],
       [7, 7, 7, 7, 7]])

This is arguably the way of creating an array filled with certain values, because it explicitly describes what is being achieved (and it can in principle be very efficient since it performs a very specific task).

这可以说是创建一个充满了特定值的数组的方法,因为它明确地描述了正在实现的内容(而且在原则上,它可以非常高效,因为它执行一个非常特定的任务)。

#2


76  

Updated for Numpy 1.7.0:(Hat-tip to @Rolf Bartstra.)

更新为Numpy 1.7.0:(Hat-tip to @Rolf Bartstra)。

a=np.empty(n); a.fill(5) is fastest.

一个= np.empty(n);a.fill(5)是最快的。

In descending speed order:

下行速度顺序:

%timeit a=np.empty(1e4); a.fill(5)
100000 loops, best of 3: 5.85 us per loop

%timeit a=np.empty(1e4); a[:]=5 
100000 loops, best of 3: 7.15 us per loop

%timeit a=np.ones(1e4)*5
10000 loops, best of 3: 22.9 us per loop

%timeit a=np.repeat(5,(1e4))
10000 loops, best of 3: 81.7 us per loop

%timeit a=np.tile(5,[1e4])
10000 loops, best of 3: 82.9 us per loop

#3


54  

I believe fill is the fastest way to do this.

我相信填充是最快的方法。

a = np.empty(10)
a.fill(7)

You should also always avoid iterating like you are doing in your example. A simple a[:] = v will accomplish what your iteration does using numpy broadcasting.

您还应该始终避免迭代,就像您在示例中所做的那样。一个简单的A [:] = v将完成您的迭代使用的numpy广播。

#4


13  

Apparently, not only the absolute speeds but also the speed order (as reported by user1579844) are machine dependent; here's what I found:

显然,不仅是绝对速度,而且速度顺序(如user1579844所报告的)是依赖于机器的;以下是我的发现:

a=np.empty(1e4); a.fill(5) is fastest;

一个= np.empty(1 e4);a.fill(5)是最快的;

In descending speed order:

下行速度顺序:

timeit a=np.empty(1e4); a.fill(5) 
# 100000 loops, best of 3: 10.2 us per loop
timeit a=np.empty(1e4); a[:]=5
# 100000 loops, best of 3: 16.9 us per loop
timeit a=np.ones(1e4)*5
# 100000 loops, best of 3: 32.2 us per loop
timeit a=np.tile(5,[1e4])
# 10000 loops, best of 3: 90.9 us per loop
timeit a=np.repeat(5,(1e4))
# 10000 loops, best of 3: 98.3 us per loop
timeit a=np.array([5]*int(1e4))
# 1000 loops, best of 3: 1.69 ms per loop (slowest BY FAR!)

So, try and find out, and use what's fastest on your platform.

所以,试着找出,并在你的平台上使用最快的。

#5


6  

You can use numpy.tile, e.g. :

您可以使用numpy。瓷砖,例如:

v = 7
rows = 3
cols = 5
a = numpy.tile(v, (rows,cols))
a
Out[1]: 
array([[7, 7, 7, 7, 7],
       [7, 7, 7, 7, 7],
       [7, 7, 7, 7, 7]])

Although tile is meant to 'tile' an array (instead of a scalar, as in this case), it will do the job, creating pre-filled arrays of any size and dimension.

尽管tile的意思是“tile”(而不是标量),它将完成任务,创建任何大小和维度的预填充数组。

#6


6  

I had

我有

numpy.array(n * [value])

in mind, but apparently that is slower than all other suggestions for large enough n.

但显然,这比其他所有建议都要慢。

Here is full comparison with perfplot (a pet project of mine).

这是与perfplot(我的一个宠物项目)的完全比较。

NumPy数组初始化(填充相同的值)

The two empty alternatives are still the fastest (with NumPy 1.12.1). full catches up for large arrays.

两个空的替代品仍然是最快的(与NumPy 1.12.1)。完全捕获大型数组。


Code to generate the plot:

产生情节的代码:

import numpy as np
import perfplot


def empty_fill(n):
    a = np.empty(n)
    a.fill(3.14)
    return a


def empty_colon(n):
    a = np.empty(n)
    a[:] = 3.14
    return a


def ones_times(n):
    return 3.14 * np.ones(n)


def repeat(n):
    return np.repeat(3.14, (n))


def tile(n):
    return np.repeat(3.14, [n])


def full(n):
    return np.full((n), 3.14)


def list_to_array(n):
    return np.array(n * [3.14])


perfplot.show(
    setup=lambda n: n,
    kernels=[
        empty_fill, empty_colon, ones_times, repeat, tile, full, list_to_array
        ],
    n_range=[2**k for k in range(23)],
    xlabel='len(a)',
    logx=True,
    logy=True,
    )