7

Similar to this, I am curious how to remove specific elements from every numpy array in a numpy array. My data is given in form of X below. I think this should work:

X = [[x1 x2 ... xn] [x1 x2 ... xn] ... [x1 x2 ... xn]]
X.shape
(n,|x|)

Y=numpy.delete(X[:],1)

I would think that Y should now be:

Y = [[x1 x3 ... xn] [x1 x3 ... xn] ... [x1 x3 ... xn]]

where Y.shape should equal (n-1,|y|=|x|), but it is not. What am I failing to grasp? My intention is to be able to remove all x2's (low correlation variable) in every array in X in order to send to a decision tree regressor. It would be even better if I could do this:

index = [ 1 3 7]
Y=numpy.delete(X[:],index)

which works if X is not a 'nested' numpy array. refer to response in link for:

array([1, 2, 3, 4, 5, 6, 7, 8, 9])

index
[2, 3, 6]

new_a = np.delete(a, index)

new_a
array([1, 2, 5, 6, 8, 9])
Community
  • 1
  • 1
leonard
  • 795
  • 1
  • 9
  • 18

1 Answers1

9

You need to apply np.delete along an axis. Please refer to the third example in the documentation.

Y = np.delete(X, 1, axis=1)
Daniel
  • 19,179
  • 7
  • 60
  • 74
  • Sweet! To ensure I understand the idea of axis. If my example was an array in an array in an array, then would I declare axis=2? – leonard Sep 30 '14 at 02:24
  • downwards across rows (axis 0), horizontally across columns (axis 1). – PlagTag Sep 30 '14 at 14:01
  • In `numpy` we talk about multidimensional arrays, i.e. 2D or 3D arrays. They can be converted into lists of lists (of lists). But 'arrays of arrays' is distinct, which arises only if the inner arrays vary in size (i.e. ragged arrays). – hpaulj Oct 01 '14 at 05:13