[英]How to find the smallest number except one particular index
edit) Sorry my question wasn't clear. 编辑)对不起,我的问题不清楚。 So, I wanted to find the min of a certain row and col without taking their intersection point, as @ParthSindhu said :)
所以,我想找到某一行和一列的最小值,而不用它们的交点,如@ParthSindhu所说的:)
I would like to find the min number from 2d array except the one number. 我想从2d数组中找到一个数字以外的最小数字。 (I'm using numpy array)
(我正在使用numpy数组)
array([[30, 15, 41, 26, 12],
[ 4, 19, 22, 40, 1],
[41, 21, 0, 43, 22],
[ 9, 40, 6, 10, 30],
[24, 49, 22, 8, 41]])
For example, in row 2 and col 2, I would like to find the smallest number in each row and col except 0. 例如,在第2行和第2列中,我想找到除0以外的每行和第一个列中最小的数字。
So, the answer want is 21 in row 2, and 6 in col 2. 因此,答案是第2行的21和第2行的6。
I've tried to implement this code with 1d array, 我试图用1d数组实现此代码,
a = np.arange(9, -1, -1) # a = array([9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
b = a[np.arange(len(a))!=3] # b = array([9, 8, 7, 5, 4, 3, 2, 1, 0])
But, I could only find the one in the row but not in col. 但是,我只能在行中找到一个,而在col中却找不到。
a[np.arange(len(a))!=1].min()
The code just above returns 6
上面的代码返回
6
How could I do the same thing with col? 我如何用col做同样的事情?
Sorry if the question is not so clear. 如果问题不清楚,我们很抱歉。
You Could this in case you are ignoring the intersection point: 如果您忽略了交点,可以这样做:
import numpy as np
a = np.array([[30, 15, 41, 26, 12],
[ 4, 19, 22, 40, 1],
[41, 21, 0, 43, 22],
[ 9, 40, 6, 10, 30],
[24, 49, 22, 8, 41]]
row = 2
col = 2
col_indices = np.delete(np.arange(a.shape[0]), row)
row_indices = np.delete(np.arange(a.shape[1]), col)
col_min = a[col_indices, col].min()
row_min = a[row, row_indices].min()
print(col_min, row_min)
I'm sure there are better ways than this, this is just how i would do it. 我敢肯定有比这更好的方法,这就是我会做的。
You can use np.amin(a, axis = 1)
to get an array with the smallest number in each row. 您可以使用
np.amin(a, axis = 1)
来获取每行编号最小的数组。
a = np.array([[30, 15, 41, 26, 12],
[ 4, 19, 22, 40, 1],
[41, 21, 0, 43, 22],
[ 9, 40, 6, 10, 30],
[24, 49, 22, 8, 41]])
print(np.amin(a, axis = 1))
This result in 这导致
>> [12 1 0 6 8]
Now you can run this again to find the smallest number in this array. 现在,您可以再次运行此命令以找到此数组中的最小数字。
Dummy = np.amin(a, axis = 1)
print(np.amin(Dummy))
And you get the smallest number. 然后您得到最小的数字。
>> 0
You can change the axis if you set axis
to 0
. 如果将
axis
设置为0
则可以更改轴。 So you can perform this operation on each axis of the array. 因此,您可以在数组的每个轴上执行此操作。
a = np.array([[30, 15, 41, 26, 12],
[ 4, 19, 22, 40, 1],
[41, 21, 0, 43, 22],
[ 9, 40, 6, 10, 30],
[24, 49, 22, 8, 41]])
print(np.amin(a, axis = 0))
>> [ 4 15 0 8 1]
You could use masked arrays: 您可以使用掩码数组:
a = np.array([[30, 15, 41, 26, 12],
[ 4, 19, 22, 40, 1],
[41, 21, 0, 43, 22],
[ 9, 40, 6, 10, 30],
[24, 49, 22, 8, 41]])
masked_a = np.ma.masked_array(a, mask=a == 0)
min_cols = masked_a.min(axis=0).data
min_rows = masked_a.min(axis=1).data
print(min_rows)
print(min_cols)
[12 1 21 6 8]
[ 4 15 6 8 1]
One possible way, replace 0
with inf
: 一种可能的方法,将
0
替换为inf
:
a = np.array([[30, 15, 41, 26, 12],
[ 4, 19, 22, 40, 1],
[41, 21, 0, 43, 22],
[ 9, 40, 6, 10, 30],
[24, 49, 22, 8, 41]])
no_zero = np.where(a==0, np.inf, a)
no_zero.min(axis=0) # array([ 4., 15., 6., 8., 1.])
no_zero.min(axis=1) # array([12., 1., 21., 6., 8.])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.