简体   繁体   English

如何从二维numpy数组的每一行中获取元素的首次出现?

[英]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.

相关问题 Python:如何在使用 2D 布尔掩码时从 2D numpy 数组逐行获取所有第一个值 - Python: how to get all the first values row-wise from a 2D numpy array when using a 2D boolean mask 如何使用numpy从二元组的三元组中的三元组的第一个元素中提取数组 - How to extract array from the first element of triples in 2d array of triples using numpy numpy 3d数组从每一行中删除第一个条目 - Numpy 3d array delete first entry from every row 在 2d numpy 数组的每一行中选择前“n”个非重复元素的有效方法 - Efficient way to pick first 'n' non-repeating elements in every row of a 2d numpy array 如何将一行2d numpy数组作为2d数组 - How do I get a row of a 2d numpy array as 2d array 如何在numpy 2d数组中添加或删除特定元素? - How to add or remove a specific element from a numpy 2d array? 如果行的第一个元素为 0,如何在不使用 python 中的 numpy 的情况下删除二维矩阵行 - how to delete a 2D matrix row if first element of the row is 0 without using numpy in python 如何按元素将一维数组添加到二维数组以在 numpy 中获取 3d 数组 - How to add a 1d array to a 2d array element-wise to get a 3d array in numpy 从二维数组中获取索引,其中第一个元素是索引 - Get index from 2d array where first element is the index 对2D numpy数组中的每一行应用相同的排列 - Apply same permutation for every row in a 2D numpy array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM