我有一个多维 numpy 数组,我想根据特定列拆分它。
例。 [[1,0,2,3],[1,2,3,4],[2,3,4,5]]
假设我想用表达式 x <=2
将这个数组按第二列拆分.然后我会得到两个数组 [[1,0,2,3],[1,2,3,4]]
和 [[2,3,4,5]]
.
我目前正在使用这个声明,我认为这是不正确的。
splits = np.split(S, np.where(S[:, a] <= t)[0][:1]) #splits S based on t
#a is the column number
最佳答案
>>> import numpy as np
>>> a = np.asarray([[1,0,2,3],[1,2,3,4],[2,3,4,5]])
>>> a
array([[1, 0, 2, 3],
[1, 2, 3, 4],
[2, 3, 4, 5]])
>>> split1 = a[a[:,1] <= 2, :]
array([[1, 0, 2, 3],
[1, 2, 3, 4]])
>>> split2 = a[a[:,1] > 2, :]
array([[2, 3, 4, 5]])
关于python - Numpy 拆分多维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35497234/