[英]Tracing indices to original matrix in Python
I am trying to trace the indices of A2
with respect to A1
.我正在尝试追踪
A2
相对于A1
的索引。 But I get an error.但是我得到一个错误。 The desired output is attached.
随附所需的 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])
Error:错误:
File "<__array_function__ internals>", line 5, in unravel_index
TypeError: only int indices permitted
Desired output:所需的 output:
[(0,0),(0,1)]
This returns the desired list of tuples:这将返回所需的元组列表:
A = [tuple(np.array(np.where(A1 == a)).ravel().tolist()) for a in A2]
For each element a
in A2
, it finds the index in A1
using np.where
.对于
A2
中的每个元素a
,它使用np.where
查找A1
中的索引。 Then it converts the index to a tuple using ravel
, tolist
and tuple
.然后它使用
ravel
、 tolist
和tuple
将索引转换为元组。 Each index is automatically in the same list.每个索引自动位于同一个列表中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.