英文:
Find last column number where condition is true
问题
我有以下的二维数组,其中1始终在左边:
arr = np.array([
[1, 1, 1, 0],
[1, 0, 0, 0],
[1, 1, 0, 0],
[1, 1, 1, 1],
[1, 0, 0, 0]
])
我该如何找到每行中最近出现的1的索引?
我的预期结果应该是:
array([2, 0, 1, 3, 0])
英文:
I have the following 2D array, where ones are always on the left:
arr = np.array([
[1, 1, 1, 0],
[1, 0, 0, 0],
[1, 1, 0, 0],
[1, 1, 1, 1],
[1, 0, 0, 0]
])
How can I find the indices of the most recent occurrence of the 1 in each row?
My expected result would be:
array([2, 0, 1, 3, 0])
答案1
得分: 1
如果每行都有一个1,可以在反转后的数组上使用numpy.argmax
函数:
# 如果只有0和1
out = arr.shape[1] - np.argmax(arr[:, ::-1], axis=1) - 1
# 否则
out = arr.shape[1] - np.argmax(arr[:, ::-1] == 1, axis=1) - 1
输出结果为:array([2, 0, 1, 3, 0])
如果有些行没有1:
arr = np.array([
[1, 1, 1, 0],
[1, 0, 0, 0],
[1, 1, 0, 0],
[1, 1, 1, 1],
[1, 0, 0, 0],
[0, 0, 0, 0]
])
m = arr[:, ::-1] == 1
out = arr.shape[1] - np.argmax(arr[:, ::-1], axis=1) - 1
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:
# if only 0/1
out = arr.shape[1] - np.argmax(arr[:, ::-1], axis=1) -1
# else
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:
arr = np.array([
[1, 1, 1, 0],
[1, 0, 0, 0],
[1, 1, 0, 0],
[1, 1, 1, 1],
[1, 0, 0, 0],
[0, 0, 0, 0]
])
m = arr[:, ::-1] == 1
out = arr.shape[1] - np.argmax(arr[:, ::-1], axis=1) -1
out = np.where(m.any(1), out, np.nan)
Output: array([ 2., 0., 1., 3., 0., nan])
答案2
得分: 1
使用2个循环,你可以实现你所需的功能...
result = []
for row in arr:
last = -1 # 如果没有1
for i in range(len(row)):
if row[i] == 1:
last = i
result.append(last)
结果将是一个列表,但你可以将结果类型更改为任何你喜欢的类型,例如NumPy数组等。
英文:
With 2 loops, you can achieve what you need...
result = []
for row in arr:
last = -1 # if there is no 1
for i in range(len(row)):
if row[i] == 1:
last = i
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的索引:
k = np.where(arr == 1)
这将返回一个元组,其中包含两个数组:
(array([0, 0, 0, 1, 2, 2, 3, 3, 3, 3, 4], dtype=int64), # 这是行号
array([0, 1, 2, 0, 0, 1, 0, 1, 2, 3, 0], dtype=int64)) # 这是索引
要找到每行中1的最后出现位置的索引,可以使用以下代码:
[k[1][x] for x in range(len(k[0])-1) if k[0][x]!= k[0][x+1]]
输出结果为:
[2, 0, 1, 3]
英文:
You can get all the indices where 1 is present by:
k = np.where(arr == 1)
(array([0, 0, 0, 1, 2, 2, 3, 3, 3, 3, 4], dtype=int64), #This gives the row number
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:
[k[1][x] for x in range(len(k[0])-1) if k[0][x]!= k[0][x+1]]
#output
[2, 0, 1, 3]
答案4
得分: 1
你可以使用numpy.apply_along_axis来实现一个简洁的一行代码。我认为使用np.max()和np.where()也非常直观。
arr = np.array([
[1, 1, 1, 0],
[1, 0, 0, 0],
[1, 1, 0, 0],
[1, 1, 1, 1],
[1, 0, 0, 0]
])
indices = np.apply_along_axis(lambda row: np.max(np.where(row == 1)), axis=1, arr=arr)
print(indices)
结果为:
[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().
arr = np.array([
[1, 1, 1, 0],
[1, 0, 0, 0],
[1, 1, 0, 0],
[1, 1, 1, 1],
[1, 0, 0, 0]
])
indices = np.apply_along_axis(lambda row: np.max(np.where(row == 1)),axis=1,arr=arr)
print(indices)
Result:
[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
我们可以使用 cumsum
和 argmax
函数:
np.argmax(np.cumsum(arr, axis=1), axis=1)
输出结果:
array([2, 0, 1, 3, 0])
英文:
We can use cumsum
and argmax
:
np.argmax(np.cumsum(arr, axis=1), axis=1)
Ouput:
array([2, 0, 1, 3, 0])
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论