Python 如何创建一个元素都等于指定值的数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14112235/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 10:30:14  来源:igfitidea点击:

How do I create an array whose elements are all equal to a specified value?

pythonnumpyscipy

提问by maxm

How do I create an array where every entry is the same value? I know numpy.ones()and numpy.zeros()do this for 1's and 0's, but what about -1?

如何创建一个数组,其中每个条目的值都相同?我知道numpy.ones()numpy.zeros()为 1 和 0 执行此操作,但是呢-1

For example:

例如:

>>import numpy as np
>>np.zeros((3,3))
array([[ 1.,  1.,  1.],
       [ 1.,  1.,  1.],
       [ 1.,  1.,  1.]])

>>np.ones((2,5))
array([[ 1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.]])

>>np.negative_ones((2,5))
???

采纳答案by DSM

I don't know if there's a nice one-liner without an arithmetic operation, but probably the fastest approach is to create an uninitialized array using emptyand then use .fill()to set the values. For comparison:

我不知道是否有没有算术运算的不错的单行,但最快的方法可能是创建一个未初始化的数组empty,然后使用它.fill()来设置值。比较:

>>> timeit m = np.zeros((3,3)); m += -1
100000 loops, best of 3: 6.9 us per loop
>>> timeit m = np.ones((3,3)); m *= -1
100000 loops, best of 3: 9.49 us per loop
>>> timeit m = np.zeros((3,3)); m.fill(-1)
100000 loops, best of 3: 2.31 us per loop
>>> timeit m = np.empty((3,3)); m[:] = -1
100000 loops, best of 3: 3.18 us per loop

>>> timeit m = np.empty((3,3)); m.fill(-1)
100000 loops, best of 3: 2.09 us per loop

but to be honest, I tend to either add to the zero matrix or multiply the ones matrix instead, as initialization is seldom a bottleneck.

但老实说,我倾向于添加到零矩阵或乘以一个矩阵,因为初始化很少是瓶颈。

回答by Manish Mulani

-1 * np.ones((2,5))

-1 * np.ones((2,5))

Multplying by the number you need in the matrix will do the trick.

乘以矩阵中所需的数字即可。

In [5]: -1 * np.ones((2,5))
Out[5]: 
array([[-1., -1., -1., -1., -1.],
       [-1., -1., -1., -1., -1.]])

In [6]: 5 * np.ones((2,5))                                                                                                                                                                                                                  
Out[6]:                                                                                                                                                                                                                                      
array([[ 5.,  5.,  5.,  5.,  5.],                                                                                                                                                                                                            
       [ 5.,  5.,  5.,  5.,  5.]]) 

回答by asheeshr

For an array of -1s

对于 -1 的数组

-1 * np.ones((2,5))

Simply multiply with the constant.

简单地乘以常数。

回答by Ramón J Romero y Vigil

foo = np.repeat(10, 50).reshape((5,10))

Will create a 5x10 matrix of 10s.

将创建一个 5x10 的 10 矩阵。

回答by learning

How about:

怎么样:

[[-1]*n]*m

where n is the number of columns and m is the number of rows?

其中 n 是列数,m 是行数?

回答by DontDivideByZero

Use np.full()as follows:

使用np.full()方法如下:

np.full((2, 5), -1.)

Returns:

返回:

array([[-1., -1., -1., -1., -1.],
       [-1., -1., -1., -1., -1.]])