如何以相反的顺序获取 NumPy 多维数组的索引?
在本文中,我们将看到如何以相反的顺序获取 NumPy 多维数组的索引。
方法
- 首先,我们导入 NumPy 库并初始化必要的参数,其中包括我们需要处理的矩阵和其他必需的参数。
- 现在我们将遍历每一行,翻转它并以相反的顺序在每一行中找到所需元素的索引,并将其存储在我们创建的列表(结果)中。
- 现在我们从数组中行的反向获取了所有索引。我们现在需要转换适合从左到右读取的索引,因此,我们减去每个索引所在行的长度,并进一步将其减一,以使我们的数组适合零索引。
Final Index= Total Elements In Rows – Current Index-1
例子:
Input:
[[1,2,3,4,2],
[2,3,4,1,5],
[2,2,4,3,2],
[1,3,4,2,4]]
Output:
[4 0 4 3]
Explanation: In the above example, we are trying to find the first occurrence indexing of ‘2’ in reverse order and we had 5 elements in each row and after backward indexing we get an array like [0, 4, 0, 1] now using our formula above.
final_list[0] => Total Elements In Rows – Current Index- 1 => 5-0-1 =>4
final_list[1] => Total Elements In Rows – Current Index- 1 => 5-4-1 =>0
final_list[2] => Total Elements In Rows – Current Index- 1 => 5-0-1 =>4
final_list[3] => Total Elements In Rows – Current Index- 1 => 5-1-1 =>3
示例 1:
Python3
#import Modules
import numpy as np
# initialize parameters
x = np.array([[1, 2, 3, 4, 2],
[2, 3, 4, 1, 5],
[2, 2, 4, 3, 2],
[1, 3, 4, 2, 4]])
num_cols = len(x[0])
result = []
# loop over each row
for row in x:
row = np.flip(row)
index = np.where(row == 2)
result.append(index[0][0])
# get the final indexes
# Store the result as of the initial arrays
final_list = num_cols-np.array(result)-1
# print
print(final_list)
Python3
#import Modules
import numpy as np
# initialize parameters
x = np.array([["Sam", "John", "Lilly"],
["Sam", "Sam", "Kate"],
["Jack", "John", "Sam"],
["Sam", "Jack", "Rose"]])
num_cols = len(x[0])
result = []
# loop over each row
for row in x:
row = np.flip(row)
index = np.where(row == "Sam")
result.append(index[0][0])
# get the final indexes
# Store the result as of the initial arrays
final_list = num_cols-np.array(result)-1
# print
print(final_list)
Python3
# import Modules
import numpy as np
# initialize parameters
a = np.array([[True, False, True, True],
[False, False, True, False],
[False, True, True, True],
[True, False, False, True]])
reversed_array = a[:, ::-1]
max_val = np.argmax(reversed_array, axis=1)
num_rows = a.shape[1]
final_list = num_rows-1-max_val
print(final_list)
输出:
[4 0 4 3]
示例 2:
上述方法也适用于字符串。在下面的示例中,我们试图以相反的顺序找到“Sam”的第一次出现索引。
蟒蛇3
#import Modules
import numpy as np
# initialize parameters
x = np.array([["Sam", "John", "Lilly"],
["Sam", "Sam", "Kate"],
["Jack", "John", "Sam"],
["Sam", "Jack", "Rose"]])
num_cols = len(x[0])
result = []
# loop over each row
for row in x:
row = np.flip(row)
index = np.where(row == "Sam")
result.append(index[0][0])
# get the final indexes
# Store the result as of the initial arrays
final_list = num_cols-np.array(result)-1
# print
print(final_list)
输出:
[0 1 2 ]
示例 3:
对于布尔数据,我们有相同的方法,但由于它只有 0 或 1 作为值,我们可以使用 argmax() 它将找到最高值的索引(对于轴 = 1 的每一行)。由于 True 等于 1,False 等于 0,因此它会记录第一个 True 值的索引。
蟒蛇3
# import Modules
import numpy as np
# initialize parameters
a = np.array([[True, False, True, True],
[False, False, True, False],
[False, True, True, True],
[True, False, False, True]])
reversed_array = a[:, ::-1]
max_val = np.argmax(reversed_array, axis=1)
num_rows = a.shape[1]
final_list = num_rows-1-max_val
print(final_list)
输出:
[3 2 3 3]