找到最后一列满足条件的列号。

huangapple go评论108阅读模式
英文:

Find last column number where condition is true

问题

我有以下的二维数组,其中1始终在左边:

  1. arr = np.array([
  2. [1, 1, 1, 0],
  3. [1, 0, 0, 0],
  4. [1, 1, 0, 0],
  5. [1, 1, 1, 1],
  6. [1, 0, 0, 0]
  7. ])

我该如何找到每行中最近出现的1的索引?

我的预期结果应该是:

  1. array([2, 0, 1, 3, 0])
英文:

I have the following 2D array, where ones are always on the left:

  1. arr = np.array([
  2. [1, 1, 1, 0],
  3. [1, 0, 0, 0],
  4. [1, 1, 0, 0],
  5. [1, 1, 1, 1],
  6. [1, 0, 0, 0]
  7. ])

How can I find the indices of the most recent occurrence of the 1 in each row?

My expected result would be:

  1. array([2, 0, 1, 3, 0])

答案1

得分: 1

如果每行都有一个1,可以在反转后的数组上使用numpy.argmax函数:

  1. # 如果只有0和1
  2. out = arr.shape[1] - np.argmax(arr[:, ::-1], axis=1) - 1
  3. # 否则
  4. out = arr.shape[1] - np.argmax(arr[:, ::-1] == 1, axis=1) - 1

输出结果为:array([2, 0, 1, 3, 0])

如果有些行没有1:

  1. arr = np.array([
  2. [1, 1, 1, 0],
  3. [1, 0, 0, 0],
  4. [1, 1, 0, 0],
  5. [1, 1, 1, 1],
  6. [1, 0, 0, 0],
  7. [0, 0, 0, 0]
  8. ])
  9. m = arr[:, ::-1] == 1
  10. out = arr.shape[1] - np.argmax(arr[:, ::-1], axis=1) - 1
  11. out = np.where(m.any(1), out, np.nan)

输出结果为:array([ 2., 0., 1., 3., 0., nan])

英文:

If you always have a 1 in each row use numpy.argmax on the reversed array:

  1. # if only 0/1
  2. out = arr.shape[1] - np.argmax(arr[:, ::-1], axis=1) -1
  3. # else
  4. out = arr.shape[1] - np.argmax(arr[:, ::-1] == 1, axis=1) -1

Output: array([2, 0, 1, 3, 0])

If you can have rows without 1:

  1. arr = np.array([
  2. [1, 1, 1, 0],
  3. [1, 0, 0, 0],
  4. [1, 1, 0, 0],
  5. [1, 1, 1, 1],
  6. [1, 0, 0, 0],
  7. [0, 0, 0, 0]
  8. ])
  9. m = arr[:, ::-1] == 1
  10. out = arr.shape[1] - np.argmax(arr[:, ::-1], axis=1) -1
  11. out = np.where(m.any(1), out, np.nan)

Output: array([ 2., 0., 1., 3., 0., nan])

答案2

得分: 1

使用2个循环,你可以实现你所需的功能...

  1. result = []
  2. for row in arr:
  3. last = -1 # 如果没有1
  4. for i in range(len(row)):
  5. if row[i] == 1:
  6. last = i
  7. result.append(last)

结果将是一个列表,但你可以将结果类型更改为任何你喜欢的类型,例如NumPy数组等。

英文:

With 2 loops, you can achieve what you need...

  1. result = []
  2. for row in arr:
  3. last = -1 # if there is no 1
  4. for i in range(len(row)):
  5. if row[i] == 1:
  6. last = i
  7. result.append(last)

Result will be a list, but you can change the result type to anything you like, for example, the NumPy array or etc

答案3

得分: 1

你可以通过以下方式获取所有值为1的索引:

  1. k = np.where(arr == 1)

这将返回一个元组,其中包含两个数组:

  1. (array([0, 0, 0, 1, 2, 2, 3, 3, 3, 3, 4], dtype=int64), # 这是行号
  2. array([0, 1, 2, 0, 0, 1, 0, 1, 2, 3, 0], dtype=int64)) # 这是索引

要找到每行中1的最后出现位置的索引,可以使用以下代码:

  1. [k[1][x] for x in range(len(k[0])-1) if k[0][x]!= k[0][x+1]]

输出结果为:

  1. [2, 0, 1, 3]
英文:

You can get all the indices where 1 is present by:

  1. k = np.where(arr == 1)
  2. (array([0, 0, 0, 1, 2, 2, 3, 3, 3, 3, 4], dtype=int64), #This gives the row number
  3. array([0, 1, 2, 0, 0, 1, 0, 1, 2, 3, 0], dtype=int64)) #This gives the index

To find the latest occurrence of 1 in each row:

  1. [k[1][x] for x in range(len(k[0])-1) if k[0][x]!= k[0][x+1]]
  2. #output
  3. [2, 0, 1, 3]

答案4

得分: 1

你可以使用numpy.apply_along_axis来实现一个简洁的一行代码。我认为使用np.max()和np.where()也非常直观。

  1. arr = np.array([
  2. [1, 1, 1, 0],
  3. [1, 0, 0, 0],
  4. [1, 1, 0, 0],
  5. [1, 1, 1, 1],
  6. [1, 0, 0, 0]
  7. ])
  8. indices = np.apply_along_axis(lambda row: np.max(np.where(row == 1)), axis=1, arr=arr)
  9. print(indices)

结果为:

  1. [2 0 1 2 3 0]

你也可以反转数组并使用np.min,然后检查它们是否匹配,以确定是否存在类似[1, 1, 0, 1]的情况。请查阅numpy文档,因为它们有很多使用这些函数的示例。

英文:

You can use numpy.apply_along_axis for a dirty one-liner. I think it's also very intuitive to use np.max() and np.where().

  1. arr = np.array([
  2. [1, 1, 1, 0],
  3. [1, 0, 0, 0],
  4. [1, 1, 0, 0],
  5. [1, 1, 1, 1],
  6. [1, 0, 0, 0]
  7. ])
  8. indices = np.apply_along_axis(lambda row: np.max(np.where(row == 1)),axis=1,arr=arr)
  9. print(indices)

Result:

  1. [2 0 1 2 3 0]

You can also reverse the array and use np.min instead and check if they match or not to determine if there are any case like [1, 1, 0, 1]. Please check the numpy documentation since they have a lot of examples of using these functions.

答案5

得分: 1

我们可以使用 cumsumargmax 函数:

  1. np.argmax(np.cumsum(arr, axis=1), axis=1)

输出结果:

  1. array([2, 0, 1, 3, 0])
英文:

We can use cumsum and argmax:

  1. np.argmax(np.cumsum(arr, axis=1), axis=1)

Ouput:

  1. array([2, 0, 1, 3, 0])

huangapple
  • 本文由 发表于 2023年7月27日 17:50:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76778521.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定