如何删除numpy数组中所有numpy数组中的第n个元素?
与此类似,我很好奇如何从numpy数组中的每个numpy数组中删除特定元素。我的数据以下面的X形式给出。我认为这应该有效:
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)
我认为Y现在应该是:
Y = [[x1 x3 ... xn] [x1 x3 ... xn] ... [x1 x3 ... xn]]
其中Y.shape应该等于(n-1,| y | = | x |),但是不是即可。我没有抓住什么?我的目的是能够删除X中每个数组中的所有x2(低相关变量),以便发送到决策树回归器。如果我能做到这一点会更好:
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)
如果X不是'嵌套'numpy数组,则有效。请参阅链接中的响应:
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])
您需要沿轴应用 np.delete
。请参阅文档中的第三个示例。
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)