简体   繁体   English

通过Python中的索引删除数组的列

[英]Remove column of array via indexing in Python

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:]). 我想做的是返回一个与test相同的数组,但是删除了第5个观察值(即test [0:4 AND 6:])。 Is there a simple way to do this? 有没有简单的方法可以做到这一点?

You could use slicing and hstack : 您可以使用切片和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. 请注意,索引的位置为1: test[0:4 AND 6:]将删除两个元素,而不是一个。

Numpy has delete , see http://docs.scipy.org/doc/numpy/reference/generated/numpy.delete.html numpy已delete ,请参阅http://docs.scipy.org/doc/numpy/reference/genic/numpy.delete.html

In your case numpy.delete(test, 5, axis=1) should do it. 在您的情况下, numpy.delete(test, 5, axis=1)应该这样做。 BUT : the elements are not deleted in place, the function returns a new array without the 5th column. 但是 :元素不会被删除,该函数将返回不包含第5列的新数组。

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. 一种替代方法是使用掩码数组 ,这取决于您的应用程序,可以提高速度,因为您不必删除条目和/或创建新的ndarrays ,而AFAIK在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() . 现在,第五个元素被屏蔽掉了,可以对result执行各种操作,例如sum()mean() More info in the link I gave you. 我给您的链接中有更多信息。 If you want to have a real ndarray , just call result.compressed() . 如果您想拥有一个真正的ndarray ,只需调用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. 掩码数组可能对这个特定问题没有好处,但是很高兴知道它们的存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM