[英]Tracing indices to original matrix in Python
我正在尝试追踪A2
相对于A1
的索引。 但是我得到一个错误。 随附所需的 output。
import numpy as np
A1 = np.array([[0.3, 1.2, 2],
[3, 4, 5]]) # Shape 2 rows & 3 columns
A2 = np.array([0.3,1.2])
A=list(zip(*np.unravel_index(A2, np.array(A1).shape)))
print([A])
错误:
File "<__array_function__ internals>", line 5, in unravel_index
TypeError: only int indices permitted
所需的 output:
[(0,0),(0,1)]
这将返回所需的元组列表:
A = [tuple(np.array(np.where(A1 == a)).ravel().tolist()) for a in A2]
对于A2
中的每个元素a
,它使用np.where
查找A1
中的索引。 然后它使用ravel
、 tolist
和tuple
将索引转换为元组。 每个索引自动位于同一个列表中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.