[英]how to get the first occurring of an element from every row in a 2D numpy array?
I have a 2d numpy array. 我有一个二维的numpy数组。 I am looking for the indices of the first occurance of a specific element in each row. 我正在寻找每一行中特定元素首次出现的索引。 The output will be an (nx 2) array, where n is the number of rows and every entry contains the x and y coordinate of the first occurance of that specific element. 输出将是一个(nx 2)数组,其中n是行数,每个条目都包含该特定元素第一次出现的x和y坐标。
Thanks a lot. 非常感谢。
If I understand the question correctly, you want something like this: 如果我正确理解了该问题,则需要以下内容:
import numpy as N
#
nrows=5
ncols=10
#
a=N.random.random((nrows,ncols))
b=-99*N.ones((nrows,2))
#
for j in range(nrows):
for i in range(ncols):
if(a[j,i]<0.5):
b[j,0]=i
b[j,1]=j
continue
I'm not sure exactly what you mean by the "s and y coordinate" so I'm assuming you mean the row and column position. 我不确定“ s和y坐标”到底是什么意思,所以我假设您的意思是行和列的位置。
import numpy as np
np.array([(s, list(row).index(your_element)) for s,row in enumerate(your_array)])
Note it will raise ValueError
if your_element
wasn't contained in some row. 注意,如果某行中未包含your_element
,则会引发ValueError
。
The following version will give you an output which may contain fewer rows than the input, but won't raise ValueError
for the case where your_element
was missing from a row. 以下版本将为您提供输出,其中可能包含的行数少于输入的行数,但your_element
中缺少your_element
的情况下,不会引发ValueError
。
np.array([(s, list(row).index(your_element)) for s,row in enumerate(your_array) if your_element in row])
>>> # generate some fake data:
>>> A = NP.random.randint(5, 10, 100).reshape(10, 10)
>>> A
array([[5, 7, 8, 8, 5, 6, 6, 9, 6, 9],
[9, 8, 8, 9, 5, 6, 6, 9, 8, 9],
[8, 5, 6, 7, 8, 9, 5, 8, 6, 7],
[5, 8, 8, 6, 9, 6, 8, 5, 8, 9],
[6, 9, 8, 8, 5, 7, 6, 5, 7, 6],
[7, 8, 6, 7, 6, 6, 7, 8, 6, 8],
[8, 6, 8, 9, 8, 8, 9, 6, 8, 7],
[8, 7, 8, 5, 9, 5, 7, 8, 6, 9],
[9, 6, 5, 9, 9, 8, 8, 9, 8, 8],
[6, 8, 5, 8, 6, 5, 8, 6, 8, 5]])
>>> # sort this 2D array along one axis (i chose row-wise)
>>> A = NP.sort(A, axis=1)
>>> A
array([[5, 5, 6, 6, 6, 7, 8, 8, 9, 9],
[5, 6, 6, 8, 8, 8, 9, 9, 9, 9],
[5, 5, 6, 6, 7, 7, 8, 8, 8, 9],
[5, 5, 6, 6, 8, 8, 8, 8, 9, 9],
[5, 5, 6, 6, 6, 7, 7, 8, 8, 9],
[6, 6, 6, 6, 7, 7, 7, 8, 8, 8],
[6, 6, 7, 8, 8, 8, 8, 8, 9, 9],
[5, 5, 6, 7, 7, 8, 8, 8, 9, 9],
[5, 6, 8, 8, 8, 8, 9, 9, 9, 9],
[5, 5, 5, 6, 6, 6, 8, 8, 8, 8]])
>>> # now diff the sorted array along the same axis
>>> A1 = NP.diff(A ,axis=1)
>>> # A1 contains non-zero values for "first occurrences" and
>>> # zero values for repeat values
>>> A1
array([[0, 1, 0, 0, 1, 1, 0, 1, 0],
[1, 0, 2, 0, 0, 1, 0, 0, 0],
[0, 1, 0, 1, 0, 1, 0, 0, 1],
[0, 1, 0, 2, 0, 0, 0, 1, 0],
[0, 1, 0, 0, 1, 0, 1, 0, 1],
[0, 0, 0, 1, 0, 0, 1, 0, 0],
[0, 1, 1, 0, 0, 0, 0, 1, 0],
[0, 1, 1, 0, 1, 0, 0, 1, 0],
[1, 2, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 1, 0, 0, 2, 0, 0, 0]])
you can reformulate the result, A1, as necessary, eg, as a boolean array having the same shape as A1 in wich each cell is either T/F depending on whether the value in the original matrix represents the first occurrence of that value: 您可以根据需要重新构造结果A1,例如,与原始数组中的A1形状相同的布尔数组 ,取决于原始矩阵中的值是否代表该值的首次出现,每个单元格均为T / F:
>>> ndx = A1==0
>>> ndx
array([[ True, False, True, True, False, False, True, False, True],
[False, True, False, True, True, False, True, True, True],
[ True, False, True, False, True, False, True, True, False],
[ True, False, True, False, True, True, True, False, True],
[ True, False, True, True, False, True, False, True, False],
[ True, True, True, False, True, True, False, True, True],
[ True, False, False, True, True, True, True, False, True],
[ True, False, False, True, False, True, True, False, True],
[False, False, True, True, True, False, True, True, True],
[ True, True, False, True, True, False, True, True, True]], dtype=bool)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.