在python中,四维数组的零。

时间:2022-01-13 01:54:15

I want to make an 4 dimensional array of zeros in python. I know how to do this for a square array but I want the lists to have different lengths.

我想在python中创建一个四维的零的数组。我知道如何对方阵做这个,但我想让列表有不同的长度。

Right now I use this:

现在我用这个:

numpy.zeros((200,)*4)

Which gives them all length 200 but I would like to have lengths 200,20,100,20 because now I have a lot of zeros in my array that I don't use

它们的长度都是200但我希望它们的长度是200 20100 20因为现在数组中有很多0我不使用

3 个解决方案

#1


7  

You can use np.full:

您可以使用np.full:

>>> np.full((200,20,10,20), 0)

numpy.full

numpy.full

Return a new array of given shape and type, filled with fill_value.

返回一个给定形状和类型的新数组,填充fill_value。

Example :

例子:

>>> np.full((1,3,2,4), 0)
array([[[[ 0.,  0.,  0.,  0.],
         [ 0.,  0.,  0.,  0.]],

        [[ 0.,  0.,  0.,  0.],
         [ 0.,  0.,  0.,  0.]],

        [[ 0.,  0.,  0.,  0.],
         [ 0.,  0.,  0.,  0.]]]])

#2


4  

You can pass more than one arg to shape:

你可以通过一个以上的arg形状:

shape : int or sequence of ints Shape of the new array, e.g., (2, 3) or 2.

形状:新阵列的int或ints序列形状,例如(2,3)或2。

In [26]: arr = np.zeros((200, 20, 10, 20))

In [27]: arr.shape
Out[27]: (200, 20, 10, 20)

It also seems a lot more efficient when you have large dimensions:

当你有大尺寸时,它看起来也更有效率:

In [43]: timeit arr = np.full((200, 100, 100, 100),0)
1 loops, best of 3: 232 ms per loop

In [44]: timeit arr = np.zeros((200, 100, 100, 100))
100000 loops, best of 3: 12.6 µs per loop
In [45]: timeit arr = np.zeros((500, 100, 100, 100))
100000 loops, best of 3: 13.5 µs per loop    
In [46]: timeit arr = np.full((500, 100, 100, 100),0)
1 loops, best of 3: 1.19 s per loop

#3


0  

As of Python v. 3.50, using the np.full command returns

在Python v3.50中,使用np。完整的命令返回

FutureWarning: in the future, full((1, 3, 2, 4), 0) will return an array of dtype('int32').

FutureWarning:在将来,full(1、3、2、4、0)将返回一个dtype数组('int32')。

Based on this, I'd plug @Padriac's answer. Both work for now, though!

基于此,我插入@Padriac的答案。不过现在这两种方法都有效!

#1


7  

You can use np.full:

您可以使用np.full:

>>> np.full((200,20,10,20), 0)

numpy.full

numpy.full

Return a new array of given shape and type, filled with fill_value.

返回一个给定形状和类型的新数组,填充fill_value。

Example :

例子:

>>> np.full((1,3,2,4), 0)
array([[[[ 0.,  0.,  0.,  0.],
         [ 0.,  0.,  0.,  0.]],

        [[ 0.,  0.,  0.,  0.],
         [ 0.,  0.,  0.,  0.]],

        [[ 0.,  0.,  0.,  0.],
         [ 0.,  0.,  0.,  0.]]]])

#2


4  

You can pass more than one arg to shape:

你可以通过一个以上的arg形状:

shape : int or sequence of ints Shape of the new array, e.g., (2, 3) or 2.

形状:新阵列的int或ints序列形状,例如(2,3)或2。

In [26]: arr = np.zeros((200, 20, 10, 20))

In [27]: arr.shape
Out[27]: (200, 20, 10, 20)

It also seems a lot more efficient when you have large dimensions:

当你有大尺寸时,它看起来也更有效率:

In [43]: timeit arr = np.full((200, 100, 100, 100),0)
1 loops, best of 3: 232 ms per loop

In [44]: timeit arr = np.zeros((200, 100, 100, 100))
100000 loops, best of 3: 12.6 µs per loop
In [45]: timeit arr = np.zeros((500, 100, 100, 100))
100000 loops, best of 3: 13.5 µs per loop    
In [46]: timeit arr = np.full((500, 100, 100, 100),0)
1 loops, best of 3: 1.19 s per loop

#3


0  

As of Python v. 3.50, using the np.full command returns

在Python v3.50中,使用np。完整的命令返回

FutureWarning: in the future, full((1, 3, 2, 4), 0) will return an array of dtype('int32').

FutureWarning:在将来,full(1、3、2、4、0)将返回一个dtype数组('int32')。

Based on this, I'd plug @Padriac's answer. Both work for now, though!

基于此,我插入@Padriac的答案。不过现在这两种方法都有效!