[英]Numpy assign an array value based on the values of another array with column selected based on a vector
I have a 2 dimensional array 我有一个二维数组
X
array([[2, 3, 3, 3],
[3, 2, 1, 3],
[2, 3, 1, 2],
[2, 2, 3, 1]])
and a 1 dimensional array 和一维数组
y
array([1, 0, 0, 1])
For each row of X, i want to find the column index where X has the lowest value and y has a value of 1, and set the corresponding row column pair in a third matrix to 1 对于X的每一行,我想找到X值为最低且y值为1的列索引,并将第三矩阵中的对应行列对设置为1
For example, in case of the first row of X, the column index corresponding to the minimum X value (for the first row only) and y = 1 is 0, then I want Z[0,0] = 1 and all other Z[0,i] = 0. Similarly, for the second row, column index 0 or 3 gives the lowest X value with y = 1. Then i want either Z[1,0] or Z[1,3] = 1 (preferably Z[1,0] = 1 and all other Z[1,i] = 0, since 0 column is the first occurance) 例如,在X的第一行的情况下,对应于最小X值(仅对于第一行)且y = 1的列索引为0,那么我希望Z [0,0] = 1且所有其他Z [0,i] =0。类似地,对于第二行,列索引0或3给出y = 1的最低X值。然后我希望Z [1,0]或Z [1,3] = 1(最好Z [1,0] = 1,所有其他Z [1,i] = 0,因为首先出现0列)
My final Z array will look like 我最后的Z数组看起来像
Z
array([[1, 0, 0, 0],
[1, 0, 0, 0],
[1, 0, 0, 0],
[0, 0, 0, 1]])
One way to do this is using masked arrays. 一种方法是使用掩码数组。
import numpy as np
X = np.array([[2, 3, 3, 3],
[3, 2, 1, 3],
[2, 3, 1, 2],
[2, 2, 3, 1]])
y = np.array([1, 0, 0, 1])
#get a mask in the shape of X. (True for places to ignore.)
y_mask = np.vstack([y == 0] * len(X))
X_masked = np.ma.masked_array(X, y_mask)
out = np.zeros_like(X)
mins = np.argmin(X_masked, axis=0)
#Output: array([0, 0, 0, 3], dtype=int64)
#Now just set the indexes to 1 on the minimum for each axis.
out[np.arange(len(out)), mins] = 1
print(out)
[[1 0 0 0]
[1 0 0 0]
[1 0 0 0]
[0 0 0 1]]
you can use numpy.argmin()
, to get the indexes of the min value at each row of X
. 您可以使用numpy.argmin()
获得X
每一行的最小值的索引。 For example: 例如:
import numpy as np
a = np.arange(6).reshape(2,3) + 10
ids = np.argmin(a, axis=1)
Similarly, you can the indexes where y is 1 by either numpy.nonzero
or numpy.where
. 同样,您可以通过numpy.nonzero
或numpy.where
来索引y为1的索引。 Once you have the two index arrays setting the values in third array should be quite easy. 一旦有了两个索引数组,就可以轻松设置第三个数组中的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.