Closed fcogidi closed 11 months ago
There's a plan to add a guide for this sort of thing to the standard https://github.com/data-apis/array-api/pull/668, although there's nothing there yet for alternatives to integer indexing. Most likely your best bet is to just manually use put
or integer indexing for libraries that you know have that functionality.
See also https://github.com/data-apis/array-api/issues/177 and https://github.com/data-apis/array-api/issues/629
That doesn't mean that partial advanced-indexing isn't very useful in other cases.
But for one-hot, there is a conceptual rewrite available that is probably likely faster anyway as long as num_classes
is relatively small.
def one_hot(array, classes):
classes = np.arange(num_classes, dtype=array.dtype)
return classes == array[..., np.newaxis]
(In NumPy, you could use out=...
to force whichever dtype you want on the result, which should be faster when arrays are large, but also adds a fair bit of overhead.)
Thank you @asmeurer and @seberg for your quick and helpful reply!
On a few occasions while using this library, I've bumped against the issue having to set array values using advanced indexing. Here is an example:
I'm using the
numpy.array_api
namespace because it follows the API standard closely.Is there a different (better) way of setting values of an array using integer (array) indices that adhere to the 2021.12 version of the array API standard?
For the example I gave, I'm aware that I can do something like this (but not with
numpy.array_api
namespace, as it only supports v2021.12):But I have other cases in my codebase that follow the first pattern - looping through array indices and using basic indexing to set array values. For example, using the indices from
xp.argsort
to mark the top-k values. Is there a better way than looping through the indices?