通过Python中的索引删除数组列(Remove column of array via indexing in Python)
我试图通过索引删除数组中的观察。 我所拥有的是:
import numpy as np test = np.ones([1, 1001])我想要做的是返回一个与test相同的数组,但是删除了第5个观察结果(即test [0:4 AND 6:])。 有一个简单的方法吗?
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?
最满意答案
你可以使用切片和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)请注意,您的索引关闭一: test[0:4 AND 6:]会删除两个元素而不是一个元素。
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.
更多推荐
发布评论