0

Say I do the following:

my_array = np.array([[1,2,3]])
my_new_array = np.delete(my_array, [])

The last line will convert my_array to:

my_new_array = np.array([1,2,3]) # It flattens the array one level

which is not what I expected.

If instead I had had:

my_array = np.array([[1,2,3], [4,5,6]])
my_new_array = np.delete(my_array, [])

I would get:

my_new_array = np.array([[1,2,3], [4,5,6]])

which is what I expect. How can I make sure a call to np.delete(my_array, []) does not flattens my array?

4

1 回答 1

6

From the documentation:

numpy.delete(arr, obj, axis=None)

axis : int, optional

The axis along which to delete the subarray defined by obj. If axis is None, obj is applied to the flattened array

Because you are not supplying anything to axis, it is flattening the array. You could do the following:

>>> print np.delete(my_array, [], axis=0)
array([[1, 2, 3],
       [4, 5, 6]])

Which seems to be the result that you want. However, it is unclear why you want to apply a transformation that gives you the same array.

于 2012-12-12T23:43:47.793 回答