1

I have an 3d Numpy Array e.g.:

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

I want to remove the elements in the same column, e.g. column 1. So the result should be:

=[[[1]
   [3]]
  [[0]
   [6]]]

I tried the np.delete function:

print np.delete(array[:][:],1,axis=1)

But it dosen't produce the desired result, instead I get:

=[[[1 2]]
  [[0 5]]]

I found this in the np doc (Link):

np.delete(array, np.s_[::2], 1)

But this seems to only remove the uneven Numbers and I don't know how to modify it. Also the doc on the np.s Function did not take me any further.

Any Help would be highly appreciated!

1
  • 1
    Since this is a 3d-array, you should delete axis=2. Commented Jan 27, 2018 at 15:54

3 Answers 3

2

Your array is a three dimensional array, so in case you want to delete the items with index 1 of the last index, you should use:

np.delete(array[:][:], 1, axis=2)

Note that you do not need the [:][:] part: this will have no effect, so you can use:

np.delete(array, 1, axis=2)

This will then generate:

>>> np.delete(array, 1, axis=2)
array([[[1],
        [3]],

       [[0],
        [6]]])
Sign up to request clarification or add additional context in comments.

3 Comments

How would I delete just one element from each 2D array?
@mLstudent33: A numpy array can only store "rectangular" data, so how would you remove one element from a 2d array?
Van Onsen, thanks, I made a copy to copy into by deleting the column index for a row in a for loop instead.
2

Deleting using axis= [0, 1] works while your working with 2d arrays. As the dimension increases, accordingly the axis value should also be increased. This should work.

np.delete(array[:][:], 1, axis=2)

Comments

0

You can very well use basic slicing since np.delete() doesn't actually delete anything from the original array. Instead it returns a copy (of what you're requesting).

In [242]: arr
Out[242]: 
array([[[1, 2],
        [3, 4]],

       [[0, 5],
        [6, 7]]])

# select both slices, select all rows but only first column
In [243]: arr[:, :, :1]
Out[243]: 
array([[[1],
        [3]],

       [[0],
        [6]]])

# same case as above but you can omit the `:`s with a single `...`
In [244]: arr[..., :1]
Out[244]: 
array([[[1],
        [3]],

       [[0],
        [6]]])

# sanity check for no copy made
In [245]: (arr[..., :1]).flags
Out[245]: 
  C_CONTIGUOUS : False
  F_CONTIGUOUS : False
  OWNDATA : False   # <=========== NO copy
  WRITEABLE : True
  ALIGNED : True
  UPDATEIFCOPY : False

However, as already mentioned, np.delete() returns the result as a copy.

In [246]: np.delete(arr[:][:], 1, axis=2).flags
Out[246]: 
  C_CONTIGUOUS : True
  F_CONTIGUOUS : False
  OWNDATA : True    # <========= Its own copy
  WRITEABLE : True
  ALIGNED : True
  UPDATEIFCOPY : False

However, if you're slicing from original array, you've to be careful while you make any changes to the result so that you don't accidentally change the original array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.