开发者

Remove column of array via indexing in Python

I'm trying to remove an observation in an array via indexing. What I h开发者_开发知识库ave 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?


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜