In numpy, one can delete an element in an array by using numpy.delete(). Now I use mxnet ndarray to calculate data, but I have got a problem.
How can I delete an element for the mxnet ndarray ?
In numpy, one can delete an element in an array by using numpy.delete(). Now I use mxnet ndarray to calculate data, but I have got a problem.
How can I delete an element for the mxnet ndarray ?
There is no built-in method to remove a single element from the array, because it is not usually needed. What is your case? Why would you want to remove an element?
You can write your custom code to do that. Here is the example of how to do it if you have 1 dimensional array and you are fine that your code won't be hybridizable:
import mxnet as mx
def remove_element_by_index(data, index):
split_data = mx.nd.split(data, num_outputs=data.shape[0], axis=0)
data_no_element = split_data[:index] + split_data[index + 1:]
return mx.nd.concat(*data_no_element, dim=0)
data = mx.nd.array([1, 2, 3])
print(remove_element_by_index(data, 0))
print(remove_element_by_index(data, 1))
print(remove_element_by_index(data, 2))
Basically, this code splits the array into multiple 1-item arrays, and then concatenates it back without the one that you need to remove.