简体   繁体   English

如果最小值满足条件,则获取二维np.array中每一行的最小值索引。

[英]Get the indices of min values for each row in a 2D np.array, if the min value satisfies a condition

I've a 2D np.array with dimension 1000 (rows) x 12 (columns) . 我有一个二维np.array ,尺寸为1000 (rows) x 12 (columns)

I need to get the indices of those values that are below 1.5 . 我需要获取低于1.5的那些值的索引。
If a row contains more than one value that satisfies this condition, then I need to keep only the indices of the lowest. 如果一行包含多个满足此条件的值,那么我只需要保留最低的索引。

I'd be quite happy with using idx1,idx2=np.where(x < 1.5) , but this sometimes returns several indices that are in the same rows. 我对使用idx1,idx2=np.where(x < 1.5)很满意,但这有时会返回同一行中的多个索引。 I could of course loop over all repeated rows in idx1 and keep only the indices whose values in x where are the lowest, but I was wondering if there's a more pythonic way. 我当然可以循环遍历idx1所有重复的行,并只保留x中的值最低的索引,但是我想知道是否还有更多的Python方式。

Thanks 谢谢

One way would be to use a numpy masked array . 一种方法是使用numpy掩码数组 Lets define the following random ndarray : 让我们定义以下随机ndarray

a = np.random.normal(1,2,(4,2))

print(a.round(2))
array([[ 1.41, -0.68],
       [-1.53,  2.74],
       [ 1.19,  2.66],
       [ 2.  ,  1.26]])

We can define a masked array with: 我们可以使用以下方法定义蒙版数组:

ma = np.ma.array(a, mask = a >= 1.5)

print(ma.round(2))
[[1.41 -0.68]
 [-1.53 --]
 [1.19 --]
 [-- 1.26]]

In order to deal with rows with no values bellow the threshold, you could do: 为了处理阈值以下没有值的行,您可以执行以下操作:

m = ma.mask.any(axis=1)
# array([ True,  True,  True,  True])

Which will contain a False if there are no valid values along a given row. 如果给定行上没有有效值,则它将包含False And then take the np.argmin over the masked array to get the columns with the minimum values bellow 1.5: 然后将np.argmin放在被屏蔽的数组上,以获取最小值在1.5以下的列:

np.argmin(ma, axis=1)[m]
# array([1, 0, 0, 1])

And for the rows you could do: 对于行,您可以执行以下操作:

np.flatnonzero(m)
# array([0, 1, 2, 3])

You can just do this: 您可以这样做:

# First index is all rows
idx1 = np.arange(len(x))
# Second index is minimum values
idx2 = np.argmin(m, axis=1)
# Filter rows where minimum is not below threshold
valid = x[idx1, idx2] < 1.5
idx1 = idx1[valid]
idx2 = idx2[valid]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 np.array 将 append 转化为二维 np.array - Using np.array to append into a 2D np.array 用查找表替换 2D/3D-np.array 中的值(= np.array with 2 columns: key + value) - Replace values in 2D/3D-np.array with lookup-table (= np.array with 2 columns: key + value) 如何将浮点数列表的 2d np.array 转换为浮点数的 2d np.array,将列表值堆叠到行 - How to convert 2d np.array of lists of floats into a 2d np.array of floats, stacking the list values to rows 如何在二维数组的每一行中获取 8 个最大值/最大值及其索引 - How to get 8 maximum / highest values in each row of 2D array with their indices 将 2D np.array 中的 int 值替换为 3 个值的列表,使其成为 3D - Replace int values in 2D np.array with list of 3 values to make it 3D 使用 np.array 作为索引更新 3D Numpy 数组 - update 3D Numpy array with np.array as indices 从pandas数据框创建np.array,该数据框有一列保存数组索引的值,另一列保存每个索引的值? - Create np.array from pandas dataframe which has a column holding values of the array's indices and another column holding the value at each index? Numpy:从 nD 数组中提取最小值的索引 - Numpy: extract indices of the min values from the n-D array 如何将2d np.array中的每十分之一(非零)值替换为零 - How to replace every tenth (nonzero) value in a 2d np.array by zero 获取符合我的标准的 np.array 索引的最快方法 - Fastest way to get indices of a np.array that meet my criteria
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM