如何通过ith字段的值对numpy数组进行切片? [英] How can I slice a numpy array by the value of the ith field?
问题描述
我有一个具有4列和很多行的2D numpy数组(> 10000,此数字是固定的).
I have a 2D numpy array with 4 columns and a lot of rows (>10000, this number is not fixed).
我需要通过其中一列的值创建 n 个子数组;我发现的最接近的问题是如何按列值分割Numpy数组;但是,我不知道该字段中的确切值(它们是浮点型的,它们在我需要的每个文件中都会更改),但是我知道它们最多不超过20个.
I need to create n subarrays by the value of one of the columns; the closest question I found was How slice Numpy array by column value; nevertheless, I dont know the exact values in the field (they're floats and they change in every file I need), but I know they are no more than 20.
我想我可以逐行阅读,记录不同的值然后进行拆分,但是我认为有一种更有效的方法.
I guess I could read line by line, record the different values and then make the split, but I figure there is a more efficient way to do this.
谢谢.
推荐答案
您可以方便地使用多维切片:
You can use multidimensional slicing conveniently:
import numpy as np
# just creating a random 2d array.
a = (np.random.random((10, 5)) * 100).astype(int)
print a
print
# select by the values of the 3rd column, selecting out more than 50.
b = a[a[:, 2] > 50]
# showing the rows for which the 3rd column value is > 50.
print b
另一个例子,更接近您在评论(?)中的要求:
Another example, closer to what you are asking in the comment (?):
import numpy as np
# just creating a random 2d array.
a = np.random.random((10000, 5)) * 100
print a
print
# select by the values of the 3rd column, selecting out more than 50.
b = a[a[:, 2] > 50.0]
b = b[b[:, 2] <= 50.2]
# showing the rows for which the 3rd column value is > 50.
print b
这将选择第三列值为(50,50.2]的行.
This selects out rows for which the 3rd column values are (50, 50.2].
这篇关于如何通过ith字段的值对numpy数组进行切片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!