实例用X零,其余的人一个矩阵 [英] Instantiate a matrix with x zeros and the rest ones
问题描述
我想能够快速实例化一个矩阵,其中第一少数细胞中的行(变量的数目)是0,其余的是那些
I would like to be able to quickly instantiate a matrix where the first few (variable number of) cells in a row are 0, and the rest are ones.
想象一下,我们希望有一个3×4矩阵。
Imagine we want a 3x4 matrix.
我第一次实例矩阵所有的:
I have instantiated the matrix first as all ones:
ones = np.ones([4,3])
然后想象我们有公布多少前导零有一个数组:
Then imagine we have an array that announces how many leading zeros there are:
arr = np.array([2,1,3,0]) # first row has 2 zeroes, second row 1 zero, etc
所需的结果:
array([[0, 0, 1],
[0, 1, 1],
[0, 0, 0],
[1, 1, 1]])
显然,这可以在相反的方式进行为好,但我会考虑的办法,其中1为默认值,零会被替换。
Obviously this can be done in the opposite way as well, but I'd consider the approach where 1 is a default value, and zeros would be replaced.
什么是避免一些愚蠢的循环的最佳方式?
What would be the best way to avoid some silly loop?
推荐答案
下面是一个办法。 N
是结果列数。行数由确定LEN(ARR)
。
Here's one way. n
is the number of columns in the result. The number of rows is determined by len(arr)
.
In [29]: n = 5
In [30]: arr = np.array([1, 2, 3, 0, 3])
In [31]: (np.arange(n) >= arr[:, np.newaxis]).astype(int)
Out[31]:
array([[0, 1, 1, 1, 1],
[0, 0, 1, 1, 1],
[0, 0, 0, 1, 1],
[1, 1, 1, 1, 1],
[0, 0, 0, 1, 1]])
有两个部分是如何工作的解释。首先,如何创建 M
的零和 N-M
的人一排?为此,我们使用 np.arange
以创建具有值的行[0,1,...,N-1]`:
There are two parts to the explanation of how this works. First, how to create a row with m
zeros and n-m
ones? For that, we use np.arange
to create a row with values [0, 1, ..., n-1]`:
In [35]: n
Out[35]: 5
In [36]: np.arange(n)
Out[36]: array([0, 1, 2, 3, 4])
其次,比较一下阵列 M
:
In [37]: m = 2
In [38]: np.arange(n) >= m
Out[38]: array([False, False, True, True, True], dtype=bool)
这给了布尔值的数组;第一个 M
值是假,其余都是如此。通过转换这些价值观为整数,我们得到0和1组成的数组:
That gives an array of boolean values; the first m
values are False and the rest are True. By casting those values to integers, we get an array of 0s and 1s:
In [39]: (np.arange(n) >= m).astype(int)
Out[39]: array([0, 0, 1, 1, 1])
要在一个的阵列执行此的 M
值(你的改编
),我们使用广播;这是解释的第二个关键的想法。
To perform this over an array of m
values (your arr
), we use broadcasting; this is the second key idea of the explanation.
请注意什么改编[:, np.newaxis]
给出:
In [40]: arr
Out[40]: array([1, 2, 3, 0, 3])
In [41]: arr[:, np.newaxis]
Out[41]:
array([[1],
[2],
[3],
[0],
[3]])
这就是改编[:, np.newaxis]
重塑改编
与形状的二维数组(5,1)。 ( arr.reshape(-1,1)
可能被用来代替。)现在,当我们比较这对 np.arange(N)
(一维数组长度 N
),在广播踢:
That is, arr[:, np.newaxis]
reshapes arr
into a 2-d array with shape (5, 1). (arr.reshape(-1, 1)
could have been used instead.) Now when we compare this to np.arange(n)
(a 1-d array with length n
), broadcasting kicks in:
In [42]: np.arange(n) >= arr[:, np.newaxis]
Out[42]:
array([[False, True, True, True, True],
[False, False, True, True, True],
[False, False, False, True, True],
[ True, True, True, True, True],
[False, False, False, True, True]], dtype=bool)
由于@RogerFan在他的评论指出,这是基本的参数外的产品,使用方式> =
操作
最后投中键入 INT
给出期望的结果:
A final cast to type int
gives the desired result:
In [43]: (np.arange(n) >= arr[:, np.newaxis]).astype(int)
Out[43]:
array([[0, 1, 1, 1, 1],
[0, 0, 1, 1, 1],
[0, 0, 0, 1, 1],
[1, 1, 1, 1, 1],
[0, 0, 0, 1, 1]])
这篇关于实例用X零,其余的人一个矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!