3

I'm trying to remove an observation in an array via indexing. What I have is:

import numpy as np
test = np.ones([1, 1001])

What I want to do is return an array that is the same as test, but having removed the 5th observation (ie, test[0:4 AND 6:]). Is there a simple way to do this?

3 Answers 3

4

You could use slicing and hstack:

In [18]: test_ex5 = np.hstack((test[:,:5],test[:,6:]))

In [19]: test.shape
Out[19]: (1, 1001)

In [20]: test_ex5.shape
Out[20]: (1, 1000)

Note that your indexing is off by one: test[0:4 AND 6:] would delete two elements instead of one.

Sign up to request clarification or add additional context in comments.

Comments

3

Numpy has delete, see http://docs.scipy.org/doc/numpy/reference/generated/numpy.delete.html

In your case numpy.delete(test, 5, axis=1) should do it. BUT: the elements are not deleted in place, the function returns a new array without the 5th column.

Comments

1

An alternative is to use masked arrays which, depending on your application, can improve speed as you don't have to delete entries and/or create new ndarrays which are, AFAIK, pretty expensive operations in numpy.

An example:

import numpy as np

test = np.ones([1, 1001])
mask = np.zeros((1, 1001))
mask[:,4] = 1
result = np.ma.masked_array(test, mask)

The fifth element is now masked away and various operations can be performed on result, like the methods sum() or mean(). More info in the link I gave you. If you want to have a real ndarray, just call result.compressed(). However, that will perform the expensive work of allocating new memory and copying data to it.

Masked arrays might not be of benefit for this particular problem, but it is good to know that they exist.

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.