# What is the equivalent of numpy where and argmin in pytorch

Hello,

l have a following 2d numpy array called coordinates. for instance

``````coordinates[:5]
[[86 11]
[74  3]
[90  9]
[84  8]
[98 88]]

``````

l process coordinates in order to return an (x,y) vector as follow ; get min x then (min_x, look min y). From a list of (x_min,y) vectors we choose (x_min, y) where is y is min. To do so in numpy l did the following :

``````            coordinates=np.asarray(coordinates)
xmin_value=np.min(coordinates[:,0])
min_x_vectors = coordinates[np.where(coordinates[:, 0] == xmin_value)]
minimum = np.argmin(min_x_vectors[:, 1])
x0,y0=min_x_vectors[minimum]
``````

My question how to do the same in pytorch ?

``````     coordinates=coord_train.astype(np.int64)
coordinates=torch.LongTensor(coord_train).type(dtypeLong)
``````

coordinates
Variable containing:
86 11
74 3
90 9
84 8
98 88
[torch.cuda.LongTensor of size 5x2 (GPU 0)]

l can retrieve the min as follow :
`xmin_value=coordinates[:,0].min()`
However l’m not sure how to deal with argmin and np.where in pytorch for :

``````          min_x_vectors = coordinates[np.where(coordinates[:, 0] == xmin_value)]
minimum = np.argmin(min_x_vectors[:, 1])
x0,y0=min_x_vectors[minimum]
``````

Temporarily l did the following :

``````          coordinates=coordinates.cpu()
coordinates=coordinates.data
coordinates=coordinates.numpy()
coordinates=np.asarray(coordinates)
``````

Thank you

Hi,
In the current master branch (not sure if it is in 0.3), there is a `torch.where` that corresponds to `np.where`.
You can get argmin from the min operation when you specify a dimension: `min_val, min_ind = tensor.min(dim=0)`. If you want to get the 2D coordinate of the minimum of a 2D tensor, you’ll have to apply the `min` function twice.

1 Like