【发布时间】:2021-02-03 11:24:22
【问题描述】:
我有两个 numpy 数组“元素”和“节点”。我的目标是收集这些数组的一些数据。 我需要用两个坐标替换最后两列的“元素”数据包含 在“节点”数组中。这两个数组非常庞大,我必须自动化它。
此帖指的是旧帖:Replace data of an array by 2 values of a second array
不同的是,数组非常庞大(元素:(3342558,5) 和节点:(581589,4)),而之前的方法不起作用。
一个例子:
import numpy as np
Elements = np.array([[1.,11.,14.],[2.,12.,13.]])
nodes = np.array([[11.,0.,0.],[12.,1.,1.],[13.,2.,2.],[14.,3.,3.]])
results = np.array([[1., 0., 0., 3., 3.],
[2., 1., 1., 2., 2.]])
之前hpaulj提出的出路
e = Elements[:,1:].ravel().astype(int)
n=nodes[:,0].astype(int)
I, J = np.where(e==n[:,None])
results = np.zeros((e.shape[0],2),nodes.dtype)
results[J] = nodes[I,:1]
results = results.reshape(2,4)
但是对于巨大的数组,这个脚本不起作用:DepreciationWarning: elementwise comparison failed; this will raise an error in the future...
【问题讨论】:
-
我不明白为什么更大的尺寸应该是一个问题。我收到了
e==[]的警告。可能还有其他不匹配会触发警告。开始验证问题行的数组形状。 -
e.shape : (13370232,) 和 n.shape: (581589,) 出现这个错误是因为我尝试增加数组直到出现错误
-
对于这些数字,我预计会出现内存错误,因为
e==n[:,None]会产生一个 (13370232, 581589) 形状的数组。我什至不会尝试测试。 -
对不起,我很忙。我保留了您的第一种方法 Divakar。它非常高效,效果很好,持续时间不到 5 秒!!!感谢您的帮助!
标签: python arrays numpy indexing